-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.service.test.ts
268 lines (215 loc) · 8.76 KB
/
user.service.test.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
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
import { Test, TestingModule } from '@nestjs/testing';
import { SnipcodeError, errors, generateRandomId } from '@snipcode/utils';
import { User } from './user.entity';
import { UserService } from './user.service';
import { TestHelper } from '../../../tests/helpers';
import { DomainModule } from '../../domain.module';
import { PrismaService } from '../prisma.service';
import { RoleService } from '../roles/role.service';
describe('Test User service', () => {
let userService: UserService;
let roleService: RoleService;
let testHelper: TestHelper;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
DomainModule.forRootAsync({
databaseUrl: process.env.TEST_DATABASE_URL,
}),
],
providers: [UserService, RoleService],
}).compile();
userService = module.get<UserService>(UserService);
roleService = module.get<RoleService>(RoleService);
const prismaService = module.get<PrismaService>(PrismaService);
testHelper = new TestHelper(prismaService);
await roleService.loadRoles();
});
it('should load users in the database', async () => {
const [roleAdmin] = await roleService.findAll();
const adminPassword = 'VerStrongPassword';
await userService.loadAdminUser(roleAdmin, adminPassword);
const adminUser = await userService.findByEmail('[email protected]');
expect(adminUser).toBeDefined();
await testHelper.deleteTestUsersById([adminUser?.id]);
});
it('should create a user', async () => {
// GIVEN
const role = await testHelper.findTestRole('user');
const createUserInput = TestHelper.createTestUserInput({ roleId: role.id });
// WHEN
const createdUser = await userService.create(createUserInput);
// THEN
expect(createdUser).toMatchObject<User>({
createdAt: expect.any(Date),
email: createUserInput.email,
id: createUserInput.toUser().id,
isEnabled: createUserInput.toUser().isEnabled,
name: createUserInput.toUser().name,
oauthProvider: createUserInput.toUser().oauthProvider,
password: null,
pictureUrl: createUserInput.toUser().pictureUrl,
roleId: createUserInput.toUser().roleId,
timezone: createUserInput.toUser().timezone,
updatedAt: expect.any(Date),
username: createUserInput.toUser().username,
});
await testHelper.deleteTestUsersById([createdUser.id]);
});
it('should create a user with no username', async () => {
// GIVEN
const role = await testHelper.findTestRole('user');
const createUserInput = TestHelper.createTestUserInput({ roleId: role.id, username: null });
// WHEN
const createdUser = await userService.create(createUserInput);
// THEN
expect(createdUser).toMatchObject<User>({
createdAt: expect.any(Date),
email: createUserInput.email,
id: createUserInput.toUser().id,
isEnabled: createUserInput.toUser().isEnabled,
name: createUserInput.toUser().name,
oauthProvider: createUserInput.toUser().oauthProvider,
password: null,
pictureUrl: createUserInput.toUser().pictureUrl,
roleId: createUserInput.toUser().roleId,
timezone: createUserInput.toUser().timezone,
updatedAt: expect.any(Date),
username: expect.any(String),
});
await testHelper.deleteTestUsersById([createdUser.id]);
});
it('should create a user with a username that already exists', async () => {
// GIVEN
const role = await testHelper.findTestRole('user');
const user = await testHelper.createTestUser({ username: 'roloto' });
const createUserInput = await TestHelper.createTestUserInput({ roleId: role.id, username: 'roloto' });
// WHEN
const createdUser = await userService.create(createUserInput);
// THEN
expect(createdUser.username).not.toEqual('roloto');
await testHelper.deleteTestUsersById([user.id, createdUser.id]);
});
it('should create a user - validation check', async () => {
// GIVEN
const role = await testHelper.findTestRole('user');
const createUserInput = TestHelper.createTestUserInput({ roleId: role.id });
createUserInput.isEnabled = true;
// WHEN
const createdUser = await userService.create(createUserInput);
// THEN
expect(createdUser).toMatchObject<User>({
createdAt: expect.any(Date),
email: createUserInput.email,
id: createUserInput.toUser().id,
isEnabled: createUserInput.toUser().isEnabled,
name: createUserInput.toUser().name,
oauthProvider: createUserInput.toUser().oauthProvider,
password: null,
pictureUrl: createUserInput.toUser().pictureUrl,
roleId: createUserInput.toUser().roleId,
timezone: createUserInput.toUser().timezone,
updatedAt: expect.any(Date),
username: createUserInput.toUser().username,
});
await testHelper.deleteTestUsersById([createdUser.id]);
});
it('should fail create a user because the email address already exists', async () => {
// GIVEN
const user = await testHelper.createTestUser({ email: '[email protected]' });
const role = await testHelper.findTestRole('user');
const createUserInput = TestHelper.createTestUserInput({ email: '[email protected]', roleId: role.id });
// WHEN
// THEN
await expect(async () => {
await userService.create(createUserInput);
}).rejects.toThrow(new SnipcodeError(errors.EMAIL_ALREADY_TAKEN, 'EMAIL_ALREADY_TAKEN'));
await testHelper.deleteTestUsersById([user.id]);
});
it('should update user information', async () => {
// GIVEN
const currentUser = await testHelper.createTestUser({});
const role = await testHelper.findTestRole('admin');
const updateUserInput = TestHelper.updateTestUserInput(role.id);
// WHEN
const updatedUser = await userService.update(currentUser, updateUserInput);
// THEN
expect(updatedUser).toMatchObject<User>({
createdAt: expect.any(Date),
email: currentUser.email,
id: currentUser.id,
isEnabled: updateUserInput.toUser(currentUser).isEnabled,
name: updateUserInput.toUser(currentUser).name,
oauthProvider: updateUserInput.toUser(currentUser).oauthProvider,
password: null,
pictureUrl: updateUserInput.toUser(currentUser).pictureUrl,
roleId: updateUserInput.toUser(currentUser).roleId,
timezone: updateUserInput.toUser(currentUser).timezone,
updatedAt: expect.any(Date),
username: currentUser.username,
});
await testHelper.deleteTestUsersById([currentUser.id]);
});
it("should fail to authenticate the user because the email doesn't exists", async () => {
// GIVEN
const userEmail = '[email protected]';
const userPassword = 'strongPassword';
// WHEN
// THEN
await expect(() => userService.login(userEmail, userPassword)).rejects.toThrow(
new SnipcodeError(errors.LOGIN_FAILED, 'LOGIN_FAILED'),
);
});
it('should fail to authenticate the user because the password is not correct', async () => {
// GIVEN
const userPassword = 'strongPassword';
const userBadPassword = 'badPassword';
const user = await testHelper.createTestUser({ oauthProvider: 'email', password: userPassword });
// WHEN
// THEN
await expect(() => userService.login(user.email, userBadPassword)).rejects.toThrow(
new SnipcodeError(errors.LOGIN_FAILED, 'LOGIN_FAILED'),
);
await testHelper.deleteTestUsersById([user.id]);
});
it('should fail to authenticate the user because the user is disabled', async () => {
// GIVEN
const userPassword = 'strongPassword';
const user = await testHelper.createTestUser({ oauthProvider: 'email', password: userPassword });
// WHEN
// THEN
await expect(() => userService.login(user.email, userPassword)).rejects.toThrow(
new SnipcodeError(errors.ACCOUNT_DISABLED, 'ACCOUNT_DISABLED'),
);
await testHelper.deleteTestUsersById([user.id]);
});
it('should successfully authenticate the user', async () => {
// GIVEN
const userPassword = 'strongPassword';
const user = await testHelper.createTestUser({
isEnabled: true,
oauthProvider: 'email',
password: userPassword,
});
// WHEN
const authenticatedUser = await userService.login(user.email, userPassword);
// THEN
expect(user).toMatchObject({
email: authenticatedUser.email,
id: authenticatedUser.id,
name: authenticatedUser.name,
oauthProvider: authenticatedUser.oauthProvider,
roleId: authenticatedUser.roleId,
username: authenticatedUser.username,
});
await testHelper.deleteTestUsersById([user.id]);
});
it('should found no user given the ID provided', async () => {
// GIVEN
const snippetId = generateRandomId();
// WHEN
const user = await userService.findById(snippetId);
// THEN
expect(user).toBeNull();
});
});