1+ import { BadRequestException , NotFoundException } from '@nestjs/common' ;
2+ import { ConfigService } from '@nestjs/config' ;
13import { Test , TestingModule } from '@nestjs/testing' ;
24import type { Request , Response } from 'express' ;
35
6+ import { UserService } from '@server/user/user.service' ;
7+
48import { AuthController } from './auth.controller' ;
59import { AuthService } from './auth.service' ;
610import { MagicLinkEmailStrategy } from './strategies/magicLinkEmail.strategy' ;
@@ -11,6 +15,16 @@ const mockAuthService = {
1115 discordLogin : jest . fn ( ) ,
1216 verifyToken : jest . fn ( ) ,
1317 loginWithEmail : jest . fn ( ) ,
18+ issueSessionTokensForUser : jest . fn ( ) ,
19+ } ;
20+
21+ const mockUserService = {
22+ findByEmail : jest . fn ( ) ,
23+ findByID : jest . fn ( ) ,
24+ } ;
25+
26+ const mockConfigService = {
27+ get : jest . fn ( ) ,
1428} ;
1529
1630const mockMagicLinkEmailStrategy = {
@@ -36,6 +50,8 @@ describe('AuthController', () => {
3650 provide : MagicLinkEmailStrategy ,
3751 useValue : mockMagicLinkEmailStrategy ,
3852 } ,
53+ { provide : UserService , useValue : mockUserService } ,
54+ { provide : ConfigService , useValue : mockConfigService } ,
3955 ] ,
4056 } ) . compile ( ) ;
4157
@@ -192,4 +208,120 @@ describe('AuthController', () => {
192208 expect ( authService . verifyToken ) . toHaveBeenCalledWith ( req , res ) ;
193209 } ) ;
194210 } ) ;
211+
212+ describe ( 'e2eSession' , ( ) => {
213+ it ( 'returns 404 when not in development' , async ( ) => {
214+ mockConfigService . get . mockImplementation ( ( key : string ) => {
215+ if ( key === 'NODE_ENV' ) return 'production' ;
216+ if ( key === 'E2E_AUTH_SECRET' ) return 'secret' ;
217+ return undefined ;
218+ } ) ;
219+
220+ await expect (
221+ controller . e2eSession ( 'secret' , { email :
'[email protected] ' } ) , 222+ ) . rejects . toBeInstanceOf ( NotFoundException ) ;
223+ } ) ;
224+
225+ it ( 'returns 404 when E2E_AUTH_SECRET is empty' , async ( ) => {
226+ mockConfigService . get . mockImplementation ( ( key : string ) => {
227+ if ( key === 'NODE_ENV' ) return 'development' ;
228+ if ( key === 'E2E_AUTH_SECRET' ) return '' ;
229+ return undefined ;
230+ } ) ;
231+
232+ await expect (
233+ controller . e2eSession ( 'secret' , { email :
'[email protected] ' } ) , 234+ ) . rejects . toBeInstanceOf ( NotFoundException ) ;
235+ } ) ;
236+
237+ it ( 'returns 404 when header secret is wrong' , async ( ) => {
238+ mockConfigService . get . mockImplementation ( ( key : string ) => {
239+ if ( key === 'NODE_ENV' ) return 'development' ;
240+ if ( key === 'E2E_AUTH_SECRET' ) return 'good' ;
241+ return undefined ;
242+ } ) ;
243+
244+ await expect (
245+ controller . e2eSession ( 'bad' , { email :
'[email protected] ' } ) , 246+ ) . rejects . toBeInstanceOf ( NotFoundException ) ;
247+ } ) ;
248+
249+ it ( 'returns 400 when both email and userId are provided' , async ( ) => {
250+ mockConfigService . get . mockImplementation ( ( key : string ) => {
251+ if ( key === 'NODE_ENV' ) return 'development' ;
252+ if ( key === 'E2E_AUTH_SECRET' ) return 's' ;
253+ return undefined ;
254+ } ) ;
255+
256+ await expect (
257+ controller . e2eSession ( 's' , { email :
'[email protected] ' , userId :
'id' } ) , 258+ ) . rejects . toBeInstanceOf ( BadRequestException ) ;
259+ } ) ;
260+
261+ it ( 'returns 400 when neither email nor userId' , async ( ) => {
262+ mockConfigService . get . mockImplementation ( ( key : string ) => {
263+ if ( key === 'NODE_ENV' ) return 'development' ;
264+ if ( key === 'E2E_AUTH_SECRET' ) return 's' ;
265+ return undefined ;
266+ } ) ;
267+
268+ await expect ( controller . e2eSession ( 's' , { } ) ) . rejects . toBeInstanceOf (
269+ BadRequestException ,
270+ ) ;
271+ } ) ;
272+
273+ it ( 'returns tokens for existing user by email' , async ( ) => {
274+ mockConfigService . get . mockImplementation ( ( key : string ) => {
275+ if ( key === 'NODE_ENV' ) return 'development' ;
276+ if ( key === 'E2E_AUTH_SECRET' ) return 's' ;
277+ return undefined ;
278+ } ) ;
279+ const user = { _id :
'u1' , email :
'[email protected] ' , username :
'u' } ; 280+ mockUserService . findByEmail . mockResolvedValueOnce ( user ) ;
281+ mockAuthService . issueSessionTokensForUser . mockResolvedValueOnce ( {
282+ access_token : 'a' ,
283+ refresh_token : 'r' ,
284+ } ) ;
285+
286+ const out = await controller . e2eSession ( 's' , { email :
'[email protected] ' } ) ; 287+
288+ expect ( out ) . toEqual ( { access_token : 'a' , refresh_token : 'r' } ) ;
289+ expect ( mockUserService . findByEmail ) . toHaveBeenCalledWith ( '[email protected] ' ) ; 290+ expect ( mockAuthService . issueSessionTokensForUser ) . toHaveBeenCalledWith (
291+ user ,
292+ ) ;
293+ } ) ;
294+
295+ it ( 'returns tokens for existing user by userId' , async ( ) => {
296+ mockConfigService . get . mockImplementation ( ( key : string ) => {
297+ if ( key === 'NODE_ENV' ) return 'development' ;
298+ if ( key === 'E2E_AUTH_SECRET' ) return 's' ;
299+ return undefined ;
300+ } ) ;
301+ const user = { _id :
'u1' , email :
'[email protected] ' , username :
'u' } ; 302+ mockUserService . findByID . mockResolvedValueOnce ( user ) ;
303+ mockAuthService . issueSessionTokensForUser . mockResolvedValueOnce ( {
304+ access_token : 'a' ,
305+ refresh_token : 'r' ,
306+ } ) ;
307+
308+ const out = await controller . e2eSession ( 's' , { userId : 'abc' } ) ;
309+
310+ expect ( out ) . toEqual ( { access_token : 'a' , refresh_token : 'r' } ) ;
311+ expect ( mockUserService . findByID ) . toHaveBeenCalledWith ( 'abc' ) ;
312+ } ) ;
313+
314+ it ( 'returns 404 when user is not found' , async ( ) => {
315+ mockConfigService . get . mockImplementation ( ( key : string ) => {
316+ if ( key === 'NODE_ENV' ) return 'development' ;
317+ if ( key === 'E2E_AUTH_SECRET' ) return 's' ;
318+ return undefined ;
319+ } ) ;
320+ mockUserService . findByEmail . mockResolvedValueOnce ( null ) ;
321+
322+ await expect (
323+ controller . e2eSession ( 's' , { email :
'[email protected] ' } ) , 324+ ) . rejects . toBeInstanceOf ( NotFoundException ) ;
325+ } ) ;
326+ } ) ;
195327} ) ;
0 commit comments