1+ import express from 'express' ;
2+ import request from 'supertest' ;
3+
4+ jest . mock ( '../../../src/api/controllers/authController' , ( ) => ( {
5+ handleCallback : jest . fn ( ( req , res ) => res . status ( 200 ) . json ( { route : 'callback' } ) ) ,
6+ handleLoginProcess : jest . fn ( ( req , res ) => res . status ( 200 ) . json ( { route : 'loginProcess' } ) ) ,
7+ handleLogout : jest . fn ( ( req , res ) => res . status ( 200 ) . json ( { route : 'logout' } ) ) ,
8+ handleGitHubLogin : jest . fn ( ( req , res ) => res . status ( 200 ) . json ( { route : 'githubLogin' } ) ) ,
9+ } ) ) ;
10+
11+ import authRoutes from '../../../src/api/routes/authRoutes' ;
12+ import {
13+ handleCallback ,
14+ handleGitHubLogin ,
15+ handleLoginProcess ,
16+ handleLogout ,
17+ } from '../../../src/api/controllers/authController' ;
18+
19+ const mockedHandleCallback = handleCallback as jest . MockedFunction < typeof handleCallback > ;
20+ const mockedHandleLoginProcess = handleLoginProcess as jest . MockedFunction < typeof handleLoginProcess > ;
21+ const mockedHandleLogout = handleLogout as jest . MockedFunction < typeof handleLogout > ;
22+ const mockedHandleGitHubLogin = handleGitHubLogin as jest . MockedFunction < typeof handleGitHubLogin > ;
23+
24+ describe ( 'auth routes' , ( ) => {
25+ const API_PREFIX = `/api/${ process . env . API_VERSION || 'v1' } ` ;
26+
27+ const app = express ( ) ;
28+ app . use ( `${ API_PREFIX } /auth` , authRoutes ) ;
29+
30+ beforeEach ( ( ) => {
31+ jest . clearAllMocks ( ) ;
32+ } ) ;
33+
34+ it ( 'routes GET /callback to handleCallback' , async ( ) => {
35+ const response = await request ( app ) . get ( `${ API_PREFIX } /auth/callback` ) ;
36+
37+ expect ( response . status ) . toBe ( 200 ) ;
38+ expect ( response . body ) . toEqual ( { route : 'callback' } ) ;
39+ expect ( mockedHandleCallback ) . toHaveBeenCalledTimes ( 1 ) ;
40+ } ) ;
41+
42+ it ( 'routes GET /login/process to handleLoginProcess' , async ( ) => {
43+ const response = await request ( app ) . get ( `${ API_PREFIX } /auth/login/process` ) ;
44+
45+ expect ( response . status ) . toBe ( 200 ) ;
46+ expect ( response . body ) . toEqual ( { route : 'loginProcess' } ) ;
47+ expect ( mockedHandleLoginProcess ) . toHaveBeenCalledTimes ( 1 ) ;
48+ } ) ;
49+
50+ it ( 'routes GET /logout to handleLogout' , async ( ) => {
51+ const response = await request ( app ) . get ( `${ API_PREFIX } /auth/logout` ) ;
52+
53+ expect ( response . status ) . toBe ( 200 ) ;
54+ expect ( response . body ) . toEqual ( { route : 'logout' } ) ;
55+ expect ( mockedHandleLogout ) . toHaveBeenCalledTimes ( 1 ) ;
56+ } ) ;
57+
58+ it ( 'routes GET /github/login to handleGitHubLogin' , async ( ) => {
59+ const response = await request ( app ) . get ( `${ API_PREFIX } /auth/github/login` ) ;
60+
61+ expect ( response . status ) . toBe ( 200 ) ;
62+ expect ( response . body ) . toEqual ( { route : 'githubLogin' } ) ;
63+ expect ( mockedHandleGitHubLogin ) . toHaveBeenCalledTimes ( 1 ) ;
64+ } ) ;
65+ } ) ;
0 commit comments