Skip to content

Commit dc08220

Browse files
committed
Add more tests
1 parent 27e2e90 commit dc08220

File tree

4 files changed

+146
-11
lines changed

4 files changed

+146
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
dist
33
*.log
4+
coverage
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { client, startServer } from './setup';
2+
3+
test('signup - a new user', async () => {
4+
const req = client(await startServer());
5+
6+
const result = await req.request(`mutation {
7+
signup(data: {name: "Roger", email: "[email protected]", password: "testtest2"}) {
8+
token
9+
user {
10+
id
11+
name
12+
}
13+
}
14+
}`);
15+
16+
expect((result as any).signup).toEqual({
17+
// Poorly check for a JWT token
18+
token: expect.stringContaining('.'),
19+
user: {
20+
id: '3',
21+
name: 'Roger'
22+
}
23+
});
24+
});
25+
26+
test('signup - with existent user', async () => {
27+
const req = client(await startServer());
28+
29+
try {
30+
await req.request(`mutation {
31+
signup(data: {name: "Kees", email: "[email protected]", password: "testtest2"}) {
32+
token
33+
}
34+
}`);
35+
} catch (e) {
36+
expect(String(e)).toMatch(/User already exists with this email/);
37+
}
38+
});
39+
40+
test('signup - with weak password', async () => {
41+
const req = client(await startServer());
42+
43+
try {
44+
await req.request(`mutation {
45+
signup(data: {name: "Roger", email: "[email protected]", password: "test"}) {
46+
token
47+
}
48+
}`);
49+
} catch (e) {
50+
expect(String(e)).toMatch(/Password is too short/);
51+
}
52+
});
53+
54+
test('login - correct', async () => {
55+
const req = client(await startServer());
56+
57+
const result = await req.request(`mutation {
58+
login(email: "[email protected]", password: "testtest2") {
59+
token
60+
user {
61+
id
62+
name
63+
}
64+
}
65+
}`);
66+
67+
expect((result as any).login).toEqual({
68+
// Poorly check for a JWT token
69+
token: expect.stringContaining('.'),
70+
user: {
71+
id: '2',
72+
name: 'Kees'
73+
}
74+
});
75+
});
76+
77+
test('login - non-existent user', async () => {
78+
const req = client(await startServer());
79+
80+
try {
81+
await req.request(`mutation {
82+
login(email: "[email protected]", password: "testtest2") {
83+
token
84+
}
85+
}`);
86+
} catch (e) {
87+
expect(String(e)).toMatch(/No user found/);
88+
}
89+
});
90+
91+
test('login - wrong password', async () => {
92+
const req = client(await startServer());
93+
94+
try {
95+
await req.request(`mutation {
96+
login(email: "[email protected]", password: "testtest1") {
97+
token
98+
}
99+
}`);
100+
} catch (e) {
101+
expect(String(e)).toMatch(/No user found/);
102+
}
103+
});

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

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
11
import { GraphQLServer } from 'graphql-yoga';
22
import { GraphQLClient } from 'graphql-request';
33
import { graphqlAuthenticationConfig, authQueries, authMutations } from '..';
4+
import { GraphqlAuthenticationAdapter, User, ID } from '..';
45

5-
const KEES_USER = {
6-
id: '2',
7-
name: 'Kees',
8-
9-
};
10-
11-
export class FakeAdapter {
12-
findUserById(ctx: any, id: any, info?: any) {
13-
if (id === '2') {
14-
return KEES_USER;
6+
export class FakeAdapter implements GraphqlAuthenticationAdapter {
7+
users: User[] = [
8+
{
9+
id: '2',
10+
name: 'Kees',
11+
password: '$2a$10$3dcRen7qMwJmzUzgj7cjUukHYlPTTCAjFhfF00.5WAFhhClTp6H4y', // testtest2
12+
13+
inviteAccepted: true,
14+
emailConfirmed: true,
15+
joinedAt: '2018-06-29T14:26:57+00:00',
16+
isSuper: false,
17+
lastLogin: ''
1518
}
19+
];
20+
21+
// If you'd use a database you wouldn't need this
22+
_generateId() {
23+
const lastUser = this.users[this.users.length - 1];
24+
return String(parseInt(lastUser.id) + 1);
25+
}
26+
findUserById(ctx: object, id: ID, info?: any) {
27+
return Promise.resolve(this.users.find(user => user.id === id) || null);
28+
}
29+
findUserByEmail(ctx: any, email: string) {
30+
return Promise.resolve(
31+
this.users.find(user => user.email === email) || null
32+
);
33+
}
34+
userExistsByEmail(ctx: any, email: string) {
35+
return Promise.resolve(this.users.some(user => user.email === email));
36+
}
37+
createUserBySignup(ctx: any, data: any) {
38+
const lastUser = this.users[this.users.length - 1];
39+
const user = { id: this._generateId(), ...data };
40+
this.users.push(user);
41+
return user;
42+
}
43+
async updateUserLastLogin(ctx: any, userId: string, data: any) {
44+
const user = await this.findUserById(ctx, userId);
45+
Object.assign(user, data); // iel
46+
return Promise.resolve(user);
1647
}
1748
}
1849

packages/graphql-authentication/src/mutations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as bcrypt from 'bcryptjs';
22
import * as jwt from 'jsonwebtoken';
33
import * as validator from 'validator';
4-
import * as uuidv4 from 'uuid/v4';
4+
import uuidv4 from 'uuid/v4';
55
import { getUser, Context } from './utils';
66
import { User } from './Adapter';
77
import {

0 commit comments

Comments
 (0)