Skip to content

Commit 89a7036

Browse files
committed
moreee tests
1 parent b22560a commit 89a7036

File tree

4 files changed

+144
-5
lines changed

4 files changed

+144
-5
lines changed

packages/graphql-authentication/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"^.+\\.tsx?$": "ts-jest"
7272
},
7373
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
74+
"setupTestFrameworkScriptFile": "<rootDir>/src/__tests__/setup.ts",
7475
"moduleFileExtensions": [
7576
"ts",
7677
"js",

packages/graphql-authentication/src/__tests__/mutations.ts

Lines changed: 120 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ test('signup - a new user', async () => {
2424
});
2525

2626
test('signup - with existent user', async () => {
27-
const req = client(await startServer());
2827
expect.assertions(1);
28+
const req = client(await startServer());
2929

3030
try {
3131
await req.request(`mutation {
@@ -39,8 +39,8 @@ test('signup - with existent user', async () => {
3939
});
4040

4141
test('signup - with weak password', async () => {
42-
const req = client(await startServer());
4342
expect.assertions(1);
43+
const req = client(await startServer());
4444

4545
try {
4646
await req.request(`mutation {
@@ -92,8 +92,8 @@ test('login - non-existent user', async () => {
9292
});
9393

9494
test('login - wrong password', async () => {
95-
const req = client(await startServer());
9695
expect.assertions(1);
96+
const req = client(await startServer());
9797

9898
try {
9999
await req.request(`mutation {
@@ -123,8 +123,8 @@ test('update current user data - correct', async () => {
123123
});
124124

125125
test('update current user data - wrong old passwd', async () => {
126-
const req = clientWithAuth(await startServer());
127126
expect.assertions(1);
127+
const req = clientWithAuth(await startServer());
128128

129129
try {
130130
await req.request(`mutation {
@@ -165,8 +165,8 @@ test('update user password', async () => {
165165
});
166166

167167
test('trigger password reset - correct', async () => {
168-
const req = clientWithAuth(await startServer());
169168
expect.assertions(6);
169+
const req = clientWithAuth(await startServer());
170170
const spy = jest.spyOn(FakeAdapter.prototype, 'updateUserResetToken');
171171

172172
const result = await req.request(`mutation {
@@ -220,3 +220,118 @@ test('trigger password reset - correct', async () => {
220220

221221
spy.mockRestore();
222222
});
223+
224+
test('invite user - correct', async () => {
225+
expect.assertions(6);
226+
const req = clientWithAuth(await startServer());
227+
const spy = jest.spyOn(FakeAdapter.prototype, 'createUserByInvite');
228+
229+
const result = await req.request(`mutation {
230+
inviteUser(data: {email: "[email protected]"}) {
231+
id
232+
}
233+
}`);
234+
235+
expect(spy).toHaveBeenCalled();
236+
237+
expect((result as any).inviteUser).toEqual({
238+
id: '3'
239+
});
240+
241+
const { inviteToken } = await spy.mock.results[0].value;
242+
// Verify the resetToken is a UUID
243+
expect(inviteToken.length).toBe(36);
244+
245+
const SIGNUP_INVITE = `mutation {
246+
signupByInvite(data:{name: "Roger", email: "[email protected]", password: "testtest4", inviteToken: "${inviteToken}"}) {
247+
user {
248+
id
249+
}
250+
}
251+
}`;
252+
253+
const result2 = await req.request(SIGNUP_INVITE);
254+
255+
expect((result2 as any).signupByInvite.user).toEqual({
256+
id: '3'
257+
});
258+
259+
const result3 = await req.request(`mutation {
260+
login(email: "[email protected]", password: "testtest4") {
261+
user {
262+
id
263+
}
264+
}
265+
}`);
266+
267+
expect((result3 as any).login.user).toEqual({
268+
id: '3'
269+
});
270+
271+
// Now verify that the inviteToken is now invalid
272+
try {
273+
await req.request(SIGNUP_INVITE);
274+
} catch (e) {
275+
expect(String(e)).toMatch(/inviteToken is invalid/);
276+
}
277+
278+
spy.mockRestore();
279+
});
280+
281+
test('confirm email - correct', async () => {
282+
expect.assertions(6);
283+
const req = clientWithAuth(await startServer());
284+
const spy = jest.spyOn(FakeAdapter.prototype, 'createUserBySignup');
285+
286+
const result = await req.request(`mutation {
287+
signup(data:{name: "Roger", email: "[email protected]", password: "testtest4"}) {
288+
user {
289+
id
290+
}
291+
}
292+
}`);
293+
294+
expect(spy).toHaveBeenCalled();
295+
296+
expect((result as any).signup.user).toEqual({
297+
id: '3'
298+
});
299+
300+
const { emailConfirmToken } = await spy.mock.results[0].value;
301+
// Verify the emailConfirmToken is a UUID
302+
expect(emailConfirmToken.length).toBe(36);
303+
304+
const CONFIRM_EMAIL = `mutation {
305+
confirmEmail(email: "[email protected]", emailConfirmToken: "${emailConfirmToken}") {
306+
user {
307+
id
308+
}
309+
}
310+
}`;
311+
const result2 = await req.request(CONFIRM_EMAIL);
312+
313+
expect((result2 as any).confirmEmail.user).toEqual({
314+
id: '3'
315+
});
316+
317+
const result3 = await req.request(`mutation {
318+
login(email: "[email protected]", password: "testtest4") {
319+
user {
320+
id
321+
}
322+
}
323+
}`);
324+
325+
expect((result3 as any).login.user).toEqual({
326+
id: '3'
327+
});
328+
329+
// Now verify that the emailConfirmToken is now invalid
330+
try {
331+
await req.request(CONFIRM_EMAIL);
332+
} catch (e) {
333+
expect(String(e)).toMatch(/emailConfirmToken is invalid/);
334+
}
335+
336+
spy.mockRestore();
337+
});

packages/graphql-authentication/src/__tests__/setup.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ export class FakeAdapter implements GraphqlAuthenticationAdapter {
4040
this.users.push(user);
4141
return user;
4242
}
43+
createUserByInvite(ctx: any, data: any) {
44+
const lastUser = this.users[this.users.length - 1];
45+
const user = { id: this._generateId(), ...data };
46+
this.users.push(user);
47+
return user;
48+
}
4349
async updateUserLastLogin(ctx: any, userId: string, data: any) {
4450
const user = await this.findUserById(ctx, userId);
4551
Object.assign(user, data); // iel
@@ -60,6 +66,16 @@ export class FakeAdapter implements GraphqlAuthenticationAdapter {
6066
Object.assign(user, data); // iel
6167
return Promise.resolve(user);
6268
}
69+
async updateUserCompleteInvite(ctx: any, userId: string, data: any) {
70+
const user = await this.findUserById(ctx, userId);
71+
Object.assign(user, data); // iel
72+
return Promise.resolve(user);
73+
}
74+
async updateUserConfirmToken(ctx: any, userId: string, data: any) {
75+
const user = await this.findUserById(ctx, userId);
76+
Object.assign(user, data); // iel
77+
return Promise.resolve(user);
78+
}
6379
}
6480

6581
// In nodejs run `require('jsonwebtoken').sign({ userId: '2' }, 'wherearemyshoes')`
@@ -98,6 +114,12 @@ export async function startServer() {
98114
return `http://localhost:${port}/`;
99115
}
100116

117+
afterAll(async () => {
118+
if (http) {
119+
await http.close();
120+
}
121+
});
122+
101123
export const clientWithAuth = uri =>
102124
new GraphQLClient(uri, {
103125
headers: {

packages/graphql-authentication/src/mutations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export const mutations = {
106106
password: hashedPassword,
107107
emailConfirmToken,
108108
emailConfirmed: false,
109+
inviteAccepted: true,
109110
joinedAt: new Date().toISOString()
110111
}
111112
);

0 commit comments

Comments
 (0)