forked from authorizerdev/authorizer-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
274 lines (241 loc) · 6.48 KB
/
Copy pathindex.test.js
File metadata and controls
274 lines (241 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
const { Authorizer } = require('../lib/cjs');
const authRef = new Authorizer({
authorizerURL: 'http://localhost:8080',
redirectURL: 'http://localhost:8080/app',
clientID: 'eebf7546-93a1-4924-8e02-34b781131b7e',
});
const adminSecret = 'admin';
const password = `Test@123#`;
const email = `uo5vbgg93p@yopmail.com`;
describe('signup success', () => {
it(`should signup with email verification enabled`, async () => {
const signupRes = await authRef.signup({
email: email,
password: password,
confirm_password: password,
});
expect(signupRes.message.length).not.toEqual(0);
});
it(`should verify email`, async () => {
const verificationRequestsRes = await authRef.graphqlQuery({
query: `
query {
_verification_requests {
verification_requests {
id
token
email
expires
identifier
}
}
}
`,
headers: {
'x-authorizer-admin-secret': adminSecret,
},
});
const requests =
verificationRequestsRes._verification_requests.verification_requests;
const item = requests.find((i) => i.email === email);
expect(item).not.toBeNull();
const verifyEmailRes = await authRef.verifyEmail({ token: item.token });
expect(verifyEmailRes.access_token.length).not.toEqual(0);
});
});
describe('login failures', () => {
it('should throw password invalid error', async () => {
try {
await authRef.login({
email: email,
password: password + 'test',
});
} catch (e) {
expect(e.message).toMatch('invalid password');
}
});
it('should throw password invalid role', async () => {
try {
await authRef.login({
email: email,
password: password,
roles: ['admin'],
});
} catch (e) {
expect(e.message).toMatch('invalid role');
}
});
});
describe(`forgot password success`, () => {
it(`should create forgot password request`, async () => {
const forgotPasswordRes = await authRef.forgotPassword({
email: email,
});
expect(forgotPasswordRes.message.length).not.toEqual(0);
});
it(`should reset password`, async () => {
const verificationRequestsRes = await authRef.graphqlQuery({
query: `
query {
_verification_requests {
verification_requests {
id
token
email
expires
identifier
}
}
}
`,
headers: {
'x-authorizer-admin-secret': adminSecret,
},
});
const requests =
verificationRequestsRes._verification_requests.verification_requests;
const item = requests.find(
(i) => i.email === email && i.identifier === 'forgot_password',
);
expect(item).not.toBeNull();
if (item) {
const resetPasswordRes = await authRef.resetPassword({
token: item.token,
password: password,
confirm_password: password,
});
expect(resetPasswordRes.message.length).not.toEqual(0);
}
});
});
describe('login success', () => {
let loginRes = null;
let headers = null;
it('should log in successfully', async () => {
loginRes = await authRef.login({
email: email,
password: password,
scope: ['openid', 'profile', 'email', 'offline_access'],
});
expect(loginRes.access_token.length).not.toEqual(0);
expect(loginRes.refresh_token.length).not.toEqual(0);
expect(loginRes.expires_in).not.toEqual(0);
expect(loginRes.id_token.length).not.toEqual(0);
headers = {
Authorization: `Bearer ${loginRes.access_token}`,
};
});
it('should validate jwt token', async () => {
const validateRes = await authRef.validateJWTToken({
token_type: 'access_token',
token: loginRes.access_token,
});
expect(validateRes.is_valid).toEqual(true);
});
it(`should validate get token`, async () => {
const tokenRes = await authRef.getToken({
grant_type: `refresh_token`,
refresh_token: loginRes.refresh_token,
});
expect(tokenRes.access_token.length).not.toEqual(0);
});
// it('should fetch the session successfully', async () => {
// const sessionRes = await authRef.getSession(headers);
// expect(loginRes.access_token).toMatch(sessionRes.access_token);
// });
// it('should validate role with session', async () => {
// const sessionRes = await authRef.getSession(headers, ['user']);
// expect(loginRes.access_token).toMatch(sessionRes.access_token);
// });
it('should update profile successfully', async () => {
const updateProfileRes = await authRef.updateProfile(
{
given_name: 'bob',
},
headers,
);
expect(updateProfileRes.message.length).not.toEqual(0);
});
it('should fetch profile successfully', async () => {
const profileRes = await authRef.getProfile(headers);
expect(profileRes.given_name).toMatch(`bob`);
});
it('should logout successfully', async () => {
// const logoutRes = await authRef.logout(headers);
// in future if message changes we don't want to take risk of this test failing
// expect(logoutRes.message.length).not.toEqual(0);
await authRef.graphqlQuery({
query: `
mutation {
_delete_user(params: {
email: "${email}"
}) {
message
}
}
`,
headers: {
'x-authorizer-admin-secret': adminSecret,
},
});
});
});
describe('magic login success', () => {
let headers = null;
it(`should login with magic link`, async () => {
const magicLinkLoginRes = await authRef.magicLinkLogin({
email: email,
});
expect(magicLinkLoginRes.message.length).not.toEqual(0);
});
it(`should verify email`, async () => {
const verificationRequestsRes = await authRef.graphqlQuery({
query: `
query {
_verification_requests {
verification_requests {
id
token
email
expires
identifier
}
}
}
`,
headers: {
'x-authorizer-admin-secret': adminSecret,
},
});
const requests =
verificationRequestsRes._verification_requests.verification_requests;
const item = requests.find((i) => i.email === email);
expect(item).not.toBeNull();
const verifyEmailRes = await authRef.verifyEmail({
token: item.token,
});
expect(verifyEmailRes.user.signup_methods).toContain('magic_link_login');
headers = {
Authorization: `Bearer ${verifyEmailRes.access_token}`,
};
});
it('should logout successfully', async () => {
// const logoutRes = await authRef.logout(headers);
// in future if message changes we don't want to take risk of this test failing
// expect(logoutRes.message.length).not.toEqual(0);
await authRef.graphqlQuery({
query: `
mutation {
_delete_user(params: {
email: "${email}"
}) {
message
}
}
`,
headers: {
'x-authorizer-admin-secret': adminSecret,
},
});
});
});