-
Notifications
You must be signed in to change notification settings - Fork 82
/
user.e2e-spec.ts.ejs
120 lines (99 loc) · 3.69 KB
/
user.e2e-spec.ts.ejs
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
import { Test, TestingModule } from '@nestjs/testing';
import request = require('supertest');
import { AppModule } from '../src/app.module';
import { INestApplication } from '@nestjs/common';
import { AuthGuard } from '../src/security/guards/auth.guard';
import { RolesGuard } from '../src/security/guards/roles.guard';
import { UserDTO } from '../src/service/dto/user.dto';
import { UserService } from '../src/service/user.service';
describe('User', () => {
let app: INestApplication;
let service: UserService;
const authGuardMock = { canActivate: (): any => true };
const rolesGuardMock = { canActivate: (): any => true };
const testUserRequestObject: any = {
login: 'userTestLogin',
firstName: 'UserTest',
lastName: 'UserTest',
email: '[email protected]'
};
const testUserDTO: UserDTO = {
login: 'userTestLogin',
firstName: 'UserTest',
lastName: 'UserTest',
email: '[email protected]',
password: 'userTestLogin'
};
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideGuard(AuthGuard)
.useValue(authGuardMock)
.overrideGuard(RolesGuard)
.useValue(rolesGuardMock)
.compile();
app = moduleFixture.createNestApplication();
await app.init();
service = moduleFixture.get<UserService>(UserService);
});
it('/POST create user', async () => {
const createdUser: UserDTO = (
await request(app.getHttpServer())
.post('/api/admin/users')
.send(testUserRequestObject)
.expect(201)
).body;
expect(createdUser.login).toEqual(testUserRequestObject.login);
await service.delete(createdUser);
});
it('/GET all users', () => {
request(app.getHttpServer())
.get('/api/admin/users')
.expect(200);
});
it('/PUT update user', async () => {
testUserDTO.login = 'TestUserUpdate';
const savedUser: UserDTO = await service.save(testUserDTO);
savedUser.firstName = 'Updated Name';
const updatedUser: UserDTO = (
await request(app.getHttpServer())
.put('/api/admin/users')
.send(savedUser)
.expect(200)
).body;
<%_ if (databaseType !== 'mongodb') { _%>
expect(updatedUser).toEqual(savedUser);
<%_ } else { _%>
expect(updatedUser.firstName).toEqual(savedUser.firstName);
<%_ } _%>
await service.delete(savedUser);
});
it('/GET user with a login name', async () => {
testUserDTO.login = 'TestUserGet';
const savedUser: UserDTO = await service.save(testUserDTO);
const getUser: UserDTO = (
await request(app.getHttpServer())
.get('/api/admin/users/' + savedUser.login)
.expect(200)
).body;
<%_ if (databaseType !== 'mongodb') { _%>
expect(getUser).toEqual(savedUser);
<%_ } else { _%>
expect(getUser.login).toEqual(savedUser.login);
<%_ } _%>
await service.delete(savedUser);
});
it('/DELETE user', async () => {
testUserDTO.login = 'TestUserDelete';
const savedUser: UserDTO = await service.save(testUserDTO);
await request(app.getHttpServer())
.delete('/api/admin/users/' + savedUser.login)
.expect(204);
const userUndefined = await service.findById(savedUser.id);
expect(userUndefined).toBeUndefined();
});
afterEach(async () => {
await app.close();
});
});