1+ import request from 'supertest' ;
2+ import express from 'express' ;
3+ import type { Request , Response , NextFunction } from 'express' ;
4+ import { listasRouter } from '../../src/routes/lista.routes' ;
5+ import { middlewareAutenticacao } from '@/shared/middlewares/autenticacao.middleware' ;
6+ import { criarProxyHandler } from '@/shared/middlewares/proxy.middleware' ;
7+ import { quizClient } from '@/shared/clients/quiz.client' ;
8+
9+ jest . mock ( '@/shared/middlewares/autenticacao.middleware' , ( ) => ( {
10+ middlewareAutenticacao : jest . fn ( ( req : Request , res : Response , next : NextFunction ) => {
11+ next ( ) ;
12+ } ) ,
13+ } ) ) ;
14+
15+ jest . mock ( '@/shared/middlewares/proxy.middleware' , ( ) => ( {
16+ criarProxyHandler : jest . fn ( ) . mockReturnValue ( ( req : Request , res : Response ) => {
17+ res . status ( 200 ) . json ( { mensagem : 'Proxy alcançado' } ) ;
18+ } ) ,
19+ } ) ) ;
20+
21+ jest . mock ( '@/shared/clients/quiz.client' , ( ) => ( {
22+ quizClient : { baseURL : 'http://mock-quiz-service' } ,
23+ } ) ) ;
24+
25+ describe ( 'Listas Router (BFF Proxy)' , ( ) => {
26+ let app : express . Application ;
27+
28+ beforeAll ( ( ) => {
29+ app = express ( ) ;
30+ app . use ( '/api/v1/listas' , listasRouter ) ;
31+ } ) ;
32+
33+ beforeEach ( ( ) => {
34+ ( middlewareAutenticacao as jest . Mock ) . mockClear ( ) ;
35+ } ) ;
36+
37+ it ( 'deve inicializar o proxy handler injetando o quizClient' , ( ) => {
38+ expect ( criarProxyHandler ) . toHaveBeenCalledWith ( quizClient ) ;
39+ } ) ;
40+
41+ it ( 'deve interceptar requisições GET, validar autenticação e repassar ao proxy' , async ( ) => {
42+ const resposta = await request ( app ) . get ( '/api/v1/listas' ) ;
43+
44+ expect ( middlewareAutenticacao ) . toHaveBeenCalled ( ) ;
45+ expect ( resposta . status ) . toBe ( 200 ) ;
46+ expect ( resposta . body ) . toEqual ( { mensagem : 'Proxy alcançado' } ) ;
47+ } ) ;
48+
49+ it ( 'deve interceptar qualquer outro método (POST/DELETE) devido ao router.all(/.*)' , async ( ) => {
50+ const resposta = await request ( app ) . post ( '/api/v1/listas/123/estatisticas/turma/456' ) ;
51+
52+ expect ( middlewareAutenticacao ) . toHaveBeenCalled ( ) ;
53+ expect ( resposta . status ) . toBe ( 200 ) ;
54+ expect ( resposta . body ) . toEqual ( { mensagem : 'Proxy alcançado' } ) ;
55+ } ) ;
56+ } ) ;
0 commit comments