1- import { it , expect , describe , afterAll , beforeAll } from 'vitest' ;
1+ import { it , expect , describe , afterAll , beforeAll , vi } from 'vitest' ;
22import { SALT } from '../../../lib/config' ;
33import {
44 AppErrorResponse ,
@@ -16,6 +16,7 @@ import { User } from '../../../../prisma/generated/client';
1616describe ( 'Authentication endpoint' , ( ) => {
1717 const BASE_URL = '/api/v1/auth' ;
1818 const SIGNIN_URL = `${ BASE_URL } /signin` ;
19+ const VERIFY_URL = `${ BASE_URL } /verify` ;
1920
2021 const userData : NewDefaultUser = {
2122 fullname : 'Clark Kent/Kal-El' ,
@@ -94,6 +95,43 @@ describe('Authentication endpoint', () => {
9495 expect ( resJwtPayload . id ) . toBeTypeOf ( 'string' ) ;
9596 expect ( resJwtPayload . username ) . toBe ( userData . username ) ;
9697 expect ( resJwtPayload . fullname ) . toBe ( userData . fullname ) ;
98+ expect ( resJwtPayload . password ) . toBeUndefined ( ) ;
99+ expect ( resJwtPayload . isAdmin ) . toBeUndefined ( ) ;
100+ } ) ;
101+ } ) ;
102+
103+ describe ( `GET ${ VERIFY_URL } ` , ( ) => {
104+ it ( 'should verify a valid, fresh token and respond with `true`' , async ( ) => {
105+ const signinResBody = ( await api . post ( SIGNIN_URL ) . send ( signInData ) )
106+ . body as AuthResponse ;
107+ const res = await api
108+ . get ( VERIFY_URL )
109+ . set ( 'Authorization' , signinResBody . token ) ;
110+ expect ( res . type ) . toMatch ( / j s o n / ) ;
111+ expect ( res . statusCode ) . toBe ( 200 ) ;
112+ expect ( res . body ) . toBe ( true ) ;
113+ } ) ;
114+
115+ it ( 'should not verify an invalid token and respond 401' , async ( ) => {
116+ const signinResBody = ( await api . post ( SIGNIN_URL ) . send ( signInData ) )
117+ . body as AuthResponse ;
118+ const res = await api
119+ . get ( VERIFY_URL )
120+ . set ( 'Authorization' , signinResBody . token . replace ( / \. ./ , '.x' ) ) ;
121+ expect ( res . statusCode ) . toBe ( 401 ) ;
122+ } ) ;
123+
124+ it ( 'should not verify an expired token and respond 401' , async ( ) => {
125+ const signinResBody = ( await api . post ( SIGNIN_URL ) . send ( signInData ) )
126+ . body as AuthResponse ;
127+ vi . useFakeTimers ( ) ;
128+ const now = new Date ( ) ;
129+ const future = new Date ( now . setFullYear ( now . getFullYear ( ) + 3 ) ) ;
130+ vi . setSystemTime ( future ) ;
131+ const res = await api
132+ . get ( VERIFY_URL )
133+ . set ( 'Authorization' , signinResBody . token ) ;
134+ expect ( res . statusCode ) . toBe ( 401 ) ;
97135 } ) ;
98136 } ) ;
99137} ) ;
0 commit comments