@@ -10,27 +10,158 @@ const AUTH_BASE_URL = "/api/auth";
10
10
11
11
faker . seed ( 0 ) ;
12
12
13
- const insertUser = async ( ) => {
14
- const username = faker . internet . userName ( ) ;
15
- const firstName = faker . person . firstName ( ) ;
16
- const lastName = faker . person . lastName ( ) ;
17
- const email = faker . internet . email ( ) ;
18
- const password = "strongPassword@123" ;
19
- const hashedPassword = bcrypt . hashSync ( password , bcrypt . genSaltSync ( 10 ) ) ;
13
+ const username = faker . internet . userName ( ) ;
14
+ const firstName = faker . person . firstName ( ) ;
15
+ const lastName = faker . person . lastName ( ) ;
16
+ const email = faker . internet . email ( ) ;
17
+ const password = "strongPassword@123" ;
18
+ const hashedPassword = bcrypt . hashSync ( password , bcrypt . genSaltSync ( 10 ) ) ;
19
+
20
+ const insertAdminUser = async ( ) => {
21
+ await new UserModel ( {
22
+ username,
23
+ firstName,
24
+ lastName,
25
+ email,
26
+ password : hashedPassword ,
27
+ isAdmin : true ,
28
+ } ) . save ( ) ;
29
+
30
+ return { email, password } ;
31
+ } ;
32
+
33
+ const insertNonAdminUser = async ( ) => {
20
34
await new UserModel ( {
21
35
username,
22
36
firstName,
23
37
lastName,
24
38
email,
25
39
password : hashedPassword ,
26
40
} ) . save ( ) ;
41
+
27
42
return { email, password } ;
28
43
} ;
29
44
30
45
describe ( "Auth routes" , ( ) => {
31
46
it ( "Login" , async ( ) => {
32
- const credentials = await insertUser ( ) ;
47
+ const credentials = await insertNonAdminUser ( ) ;
48
+
33
49
const res = await request . post ( `${ AUTH_BASE_URL } /login` ) . send ( credentials ) ;
50
+
34
51
expect ( res . status ) . toBe ( 200 ) ;
35
52
} ) ;
53
+
54
+ it ( "Login with invalid password" , async ( ) => {
55
+ const { email } = await insertNonAdminUser ( ) ;
56
+
57
+ const res = await request
58
+ . post ( `${ AUTH_BASE_URL } /login` )
59
+ . send ( { email, password : "blahblah" } ) ;
60
+
61
+ expect ( res . status ) . toBe ( 401 ) ;
62
+ } ) ;
63
+
64
+ it ( "Login with invalid email" , async ( ) => {
65
+ const { password } = await insertNonAdminUser ( ) ;
66
+
67
+ const res = await request
68
+ . post ( `${ AUTH_BASE_URL } /login` )
69
+ . send ( { email : "blahblah" , password } ) ;
70
+
71
+ expect ( res . status ) . toBe ( 401 ) ;
72
+ } ) ;
73
+
74
+ it ( "Login with missing email and/or password" , async ( ) => {
75
+ const res = await request . post ( `${ AUTH_BASE_URL } /login` ) . send ( { } ) ;
76
+
77
+ expect ( res . status ) . toBe ( 400 ) ;
78
+ } ) ;
79
+
80
+ it ( "Catch server error when login" , async ( ) => {
81
+ const loginSpy = jest . spyOn ( UserModel , "findOne" ) . mockImplementation ( ( ) => {
82
+ throw new Error ( ) ;
83
+ } ) ;
84
+
85
+ const res = await request
86
+ . post ( `${ AUTH_BASE_URL } /login` )
87
+ . send ( { email, password } ) ;
88
+
89
+ expect ( res . status ) . toBe ( 500 ) ;
90
+
91
+ loginSpy . mockRestore ( ) ;
92
+ } ) ;
93
+
94
+ it ( "Verify token with missing token" , async ( ) => {
95
+ const res = await request . get ( `${ AUTH_BASE_URL } /verify-token` ) ;
96
+
97
+ expect ( res . status ) . toBe ( 401 ) ;
98
+ } ) ;
99
+
100
+ it ( "Verify token but users not found" , async ( ) => {
101
+ // TODO
102
+ } ) ;
103
+
104
+ it ( "Verify token" , async ( ) => {
105
+ const { email, password } = await insertNonAdminUser ( ) ;
106
+
107
+ const loginRes = await request
108
+ . post ( `${ AUTH_BASE_URL } /login` )
109
+ . send ( { email, password } ) ;
110
+
111
+ const token = loginRes . body . data . accessToken ;
112
+
113
+ const res = await request
114
+ . get ( `${ AUTH_BASE_URL } /verify-token` )
115
+ . set ( "Authorization" , `Bearer ${ token } ` ) ;
116
+
117
+ expect ( res . status ) . toBe ( 200 ) ;
118
+ expect ( res . body . data . email ) . toBe ( email ) ;
119
+ expect ( res . body . data . isAdmin ) . toBe ( false ) ;
120
+ } ) ;
121
+
122
+ it ( "Verify invalid token" , async ( ) => {
123
+ const res = await request
124
+ . get ( `${ AUTH_BASE_URL } /verify-token` )
125
+ . set ( "Authorization" , `Bearer blahblah` ) ;
126
+
127
+ expect ( res . status ) . toBe ( 401 ) ;
128
+ } ) ;
129
+
130
+ it ( "Verify admin token" , async ( ) => {
131
+ const { email, password } = await insertAdminUser ( ) ;
132
+
133
+ const loginRes = await request
134
+ . post ( `${ AUTH_BASE_URL } /login` )
135
+ . send ( { email, password } ) ;
136
+
137
+ const token = loginRes . body . data . accessToken ;
138
+
139
+ const res = await request
140
+ . get ( `${ AUTH_BASE_URL } /verify-admin-token` )
141
+ . set ( "Authorization" , `Bearer ${ token } ` ) ;
142
+
143
+ expect ( res . status ) . toBe ( 200 ) ;
144
+ expect ( res . body . data . email ) . toBe ( email ) ;
145
+ expect ( res . body . data . isAdmin ) . toBe ( true ) ;
146
+ } ) ;
147
+
148
+ it ( "Verify admin token with non-admin user" , async ( ) => {
149
+ const { email, password } = await insertNonAdminUser ( ) ;
150
+
151
+ const loginRes = await request
152
+ . post ( `${ AUTH_BASE_URL } /login` )
153
+ . send ( { email, password } ) ;
154
+
155
+ const token = loginRes . body . data . accessToken ;
156
+
157
+ const res = await request
158
+ . get ( `${ AUTH_BASE_URL } /verify-admin-token` )
159
+ . set ( "Authorization" , `Bearer ${ token } ` ) ;
160
+
161
+ expect ( res . status ) . toBe ( 403 ) ;
162
+ } ) ;
163
+
164
+ it ( "Verify if user is owner or admin" , async ( ) => {
165
+ // TODO
166
+ } ) ;
36
167
} ) ;
0 commit comments