1+ import { PrismaClient } from '@prisma/client' ;
2+ import { isJWT } from 'class-validator' ;
3+ import { v4 } from 'uuid' ;
4+ import { confirmEmail , refresh , signIn , signUp } from './endpoints/auth.controller.endpoints' ;
5+
16describe ( 'Auth Controller' , ( ) => {
2- it ( 'no test' , ( ) => {
3- expect ( true ) . toBeTruthy ( ) ;
4- } )
5- } ) ;
7+ let prisma : PrismaClient ;
8+
9+ beforeAll ( async ( ) => {
10+ prisma = new PrismaClient ( ) ;
11+ await prisma . $connect ( ) ;
12+ } ) ;
13+
14+ afterAll ( async ( ) => {
15+ await prisma . $disconnect ( ) ;
16+ } ) ;
17+
18+ describe ( 'POST /api/auth/sign-up' , ( ) => {
19+ it ( 'should return 400 on invalid body' , async ( ) => {
20+ await signUp . spec ( ) . withBody ( { } ) . expectStatus ( 400 ) . toss ( ) ;
21+ } ) ;
22+
23+ it ( 'should return 400 on invalid email' , async ( ) => {
24+ await signUp
25+ . spec ( )
26+ . withBody ( { password : '12345678' , email : 'invalid' } )
27+ . expectStatus ( 400 )
28+ . toss ( ) ;
29+ } ) ;
30+
31+ it ( 'should success sign up user' , async ( ) => {
32+ await signUp . send ( { email : 'test@example.com' , password : '12345678' } ) ;
33+
34+ expect (
35+ ( await prisma . user . findFirst ( { where : { email : 'test@example.com' } } ) )
36+ ?. emailConfirmed ,
37+ ) . toBeFalsy ( ) ;
38+ } ) ;
39+
40+ it ( 'should return error if user already exist' , async ( ) => {
41+ await signUp
42+ . spec ( )
43+ . withBody ( { email : 'test@example.com' , password : '12345678' } )
44+ . expectStatus ( 200 )
45+ . expectJsonLike ( { data : { error : 'userAlreadyExist' } } ) ;
46+ } ) ;
47+ } ) ;
48+
49+ describe ( 'POST /api/auth/confirm-email' , ( ) => {
50+ it ( 'should return 400 on invalid body' , async ( ) => {
51+ await confirmEmail . spec ( ) . withBody ( { } ) . expectStatus ( 400 ) . toss ( ) ;
52+ } ) ;
53+
54+ it ( 'should return 400 on invalid uuid' , async ( ) => {
55+ await confirmEmail . spec ( ) . withBody ( { confirmUuid : 'invalid' } ) . expectStatus ( 400 ) . toss ( ) ;
56+ } ) ;
57+
58+ it ( 'should return error if email confirm not found' , async ( ) => {
59+ await confirmEmail
60+ . spec ( )
61+ . withBody ( { confirmUuid : v4 ( ) } )
62+ . expectStatus ( 200 )
63+ . expectJsonLike ( { data : { error : 'cannotFindEmailConfirm' } } ) ;
64+ } ) ;
65+
66+ it ( 'should success confirm email' , async ( ) => {
67+ const user = await prisma . user . findFirst ( {
68+ where : { email : 'test@example.com' } ,
69+ include : { emailConfirm : true } ,
70+ } ) ;
71+
72+ expect (
73+ ( await prisma . user . findFirst ( { where : { email : 'test@example.com' } } ) )
74+ ?. emailConfirmed ,
75+ ) . toBeFalsy ( ) ;
76+
77+ await confirmEmail . send ( user ?. emailConfirm ?. confirmUuid as string ) ;
78+
79+ expect (
80+ ( await prisma . user . findFirst ( { where : { email : 'test@example.com' } } ) )
81+ ?. emailConfirmed ,
82+ ) . toBeTruthy ( ) ;
83+ } ) ;
84+
85+ it ( 'should return error if user already confirmed email' , async ( ) => {
86+ const user = await prisma . user . findFirst ( {
87+ where : { email : 'test@example.com' } ,
88+ include : { emailConfirm : true } ,
89+ } ) ;
90+
91+ await confirmEmail
92+ . spec ( )
93+ . withBody ( { confirmUuid : user ?. emailConfirm ?. confirmUuid } )
94+ . expectStatus ( 200 )
95+ . expectJsonLike ( { data : { error : 'emailAlreadyConfirmed' } } ) ;
96+ } ) ;
97+ } ) ;
98+
99+ describe ( 'POST /api/auth/sign-in' , ( ) => {
100+ it ( 'should return 400 on invalid body' , async ( ) => {
101+ await signIn . spec ( ) . withBody ( { } ) . expectStatus ( 400 ) . toss ( ) ;
102+ await signIn . spec ( ) . withBody ( { email : 'test@example.com' } ) . expectStatus ( 400 ) . toss ( ) ;
103+ await signIn
104+ . spec ( )
105+ . withBody ( { email : 'invalid' , password : '12345678' } )
106+ . expectStatus ( 400 )
107+ . toss ( ) ;
108+ } ) ;
109+
110+ it ( 'should return error if user not confirm email' , async ( ) => {
111+ await signUp . send ( { email : 'test1@example.com' , password : '12345678' } ) ;
112+
113+ await signIn
114+ . spec ( )
115+ . withBody ( { email : 'test1@example.com' , password : '12345678' } )
116+ . expectStatus ( 200 )
117+ . expectJsonLike ( { data : { error : 'emailNotConfirmed' } } ) ;
118+ } ) ;
119+
120+ it ( 'should return error if email or password incorrect' , async ( ) => {
121+ await signIn
122+ . spec ( )
123+ . withBody ( { email : 'test@example.com' , password : '123456789' } )
124+ . expectStatus ( 200 )
125+ . expectJsonLike ( { data : { error : 'emailOrPasswordIncorrect' } } ) ;
126+ await signIn
127+ . spec ( )
128+ . withBody ( { email : 'test2@example.com' , password : '12345678' } )
129+ . expectStatus ( 200 )
130+ . expectJsonLike ( { data : { error : 'emailOrPasswordIncorrect' } } ) ;
131+ } ) ;
132+
133+ it ( 'should success return tokens' , async ( ) => {
134+ await signIn . send ( { email : 'test@example.com' , password : '12345678' } ) ;
135+ } ) ;
136+ } ) ;
137+
138+ describe ( 'GET /api/auth/refresh' , ( ) => {
139+ it ( 'should return 401' , async ( ) => {
140+ await refresh . spec ( ) . expectStatus ( 401 ) . toss ( ) ;
141+ } ) ;
142+
143+ it ( 'should return new access token and refresh token in cookie' , async ( ) => {
144+ const tokens = await signIn . send ( { email : 'test@example.com' , password : '12345678' } ) ;
145+
146+ const result = await refresh . send ( tokens . refreshToken ) ;
147+ expect ( isJWT ( result . accessToken ) ) . toEqual ( true ) ;
148+ expect ( isJWT ( result . refreshToken ) ) . toEqual ( true ) ;
149+ } ) ;
150+ } ) ;
151+ } ) ;
0 commit comments