-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.integration.spec.ts
172 lines (144 loc) · 4.73 KB
/
auth.integration.spec.ts
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
import { PrismaService, RoleService, UserService } from '@snipcode/domain';
import { isValidUUIDV4 } from '@snipcode/utils';
import request from 'supertest';
import { TestHelper } from '../../../utils/tests/helpers';
import { TestServer, startTestServer } from '../../../utils/tests/server';
const graphqlEndpoint = '/graphql';
describe('Test Authentication', () => {
let server: TestServer;
let testHelper: TestHelper;
let prismaService: PrismaService;
let roleService: RoleService;
let userService: UserService;
beforeAll(async () => {
server = await startTestServer();
prismaService = server.app.get<PrismaService>(PrismaService);
userService = server.app.get<UserService>(UserService);
roleService = server.app.get<RoleService>(RoleService);
testHelper = new TestHelper(prismaService, roleService, userService);
});
beforeEach(async () => {
await testHelper.cleanDatabase();
});
afterAll(async () => {
await server.close();
});
test('Register a user', async () => {
const query = `
mutation SignupUser($input: SignupUserInput!) {
signupUser(input: $input) {
__typename
message
}
}
`;
const variables = {
input: {
email: '[email protected]',
name: 'John Doe',
password: 'password',
},
};
const response = await request(server.app.getHttpServer())
.post(graphqlEndpoint)
.send({ query, variables })
.expect(200);
expect(response.body.data.signupUser).toMatchObject({
__typename: 'SignupUserResult',
message: 'Account created successfully!',
});
});
test('Register a user with an existing email address will returns an error', async () => {
const query = `
mutation SignupUser($input: SignupUserInput!) {
signupUser(input: $input) {
__typename
message
}
}
`;
const variables = {
input: {
email: '[email protected]',
name: 'John Doe',
password: 'password',
},
};
await testHelper.createTestUser({ email: variables.input.email });
const response = await request(server.app.getHttpServer())
.post(graphqlEndpoint)
.send({ query, variables })
.expect(200);
expect(response.body.errors[0].extensions.code).toEqual('EMAIL_ALREADY_TAKEN');
expect(response.body.errors[0].message).toEqual('The email address is already taken');
});
test('Returns an error when authenticating with bad credentials', async () => {
const query = `
mutation LoginUser($email: String!, $password: String!) {
loginUser(email: $email, password: $password) {
token
}
}
`;
const variables = {
email: '[email protected]',
password: '123456',
};
const response = await request(server.app.getHttpServer())
.post(graphqlEndpoint)
.send({ query, variables })
.expect(200);
expect(response.body.errors[0].extensions.code).toEqual('LOGIN_FAILED');
expect(response.body.errors[0].message).toEqual('Invalid email address or password.');
});
test('Returns a token when authenticating with correct credentials', async () => {
const query = `
mutation LoginUser($email: String!, $password: String!) {
loginUser(email: $email, password: $password) {
token
}
}
`;
await testHelper.createTestUser({
email: '[email protected]',
isEnabled: true,
password: 'password',
role: 'user',
});
const variables = {
email: '[email protected]',
password: 'password',
};
const response = await request(server.app.getHttpServer())
.post(graphqlEndpoint)
.send({ query, variables })
.expect(200);
expect(response.body.data.loginUser.token).toBeDefined();
expect(isValidUUIDV4(response.body.data.loginUser.token)).toBe(true);
});
test('Returns an error message when trying to authenticate with a disabled account', async () => {
const query = `
mutation LoginUser($email: String!, $password: String!) {
loginUser(email: $email, password: $password) {
token
}
}
`;
await testHelper.createTestUser({
email: '[email protected]',
isEnabled: false,
password: 'password',
role: 'user',
});
const variables = {
email: '[email protected]',
password: 'password',
};
const response = await request(server.app.getHttpServer())
.post(graphqlEndpoint)
.send({ query, variables })
.expect(200);
expect(response.body.errors[0].extensions.code).toEqual('ACCOUNT_DISABLED');
expect(response.body.errors[0].message).toEqual('Your account is disabled!');
});
});