Skip to content

Commit d45c845

Browse files
committed
projects. e2e tests added
1 parent 5d800f4 commit d45c845

File tree

2 files changed

+103
-11
lines changed

2 files changed

+103
-11
lines changed

test/preconditions.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { INestApplication } from '@nestjs/common';
22
import { UsersService } from 'src/users/users.service';
33
import { UserLoginRequestDto } from 'src/users/dto/user-login-request.dto';
44
import uuidAPIKey from 'uuid-apikey';
5-
import request, { Test } from 'supertest';
5+
import request, { Test, CallbackHandler, Request } from 'supertest';
66
import { CreateUserDto } from 'src/users/dto/user-create.dto';
77

8-
export const generateUser = (password: string = '123456'): { email: string, password: string, firstName: string, lastName: string } => ({
8+
export const generateUser = (password: string): { email: string, password: string, firstName: string, lastName: string } => ({
99
email: `${uuidAPIKey.create().uuid}@example.com'`,
1010
password,
1111
firstName: 'fName',
@@ -18,13 +18,12 @@ export const requestWithAuth = (app: INestApplication, method: 'post' | 'get' |
1818
.set('Authorization', 'Bearer ' + token)
1919
.send(body)
2020

21-
// export const haveUserCreated = (usersService: UsersService, password: string = '123456') => {
22-
// return usersService.create({
23-
// email: `${uuidAPIKey.create().uuid}@example.com'`,
24-
// password,
25-
// firstName: 'fName',
26-
// lastName: 'lName',
27-
// })
28-
// }
21+
export const haveUserLogged = async (usersService: UsersService) => {
22+
const password = '123456'
23+
const user = await usersService.create(generateUser(password))
2924

30-
// export const haveUserLogged = (usersService: UsersService, user: UserLoginRequestDto) => usersService.login(user)
25+
return usersService.login({
26+
email: user.email,
27+
password
28+
})
29+
}

test/projects.e2e-spec.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { INestApplication } from '@nestjs/common';
3+
import { AppModule } from '../src/app.module';
4+
import { UsersService } from '../src/users/users.service';
5+
import { requestWithAuth, haveUserLogged } from './preconditions';
6+
import { ProjectsService } from '../src/projects/projects.service';
7+
import uuidAPIKey from 'uuid-apikey';
8+
9+
const project = {
10+
id: uuidAPIKey.create().uuid,
11+
name: 'Test project'
12+
}
13+
14+
const projectServiceMock = {
15+
findAll: () => ['test'],
16+
create: () => project,
17+
remove: () => project,
18+
};
19+
20+
describe('Projects (e2e)', () => {
21+
let app: INestApplication;
22+
let usersService: UsersService;
23+
let loggedUser
24+
25+
beforeAll(async () => {
26+
const moduleFixture: TestingModule = await Test.createTestingModule({
27+
imports: [AppModule],
28+
})
29+
.overrideProvider(ProjectsService)
30+
.useValue(projectServiceMock)
31+
.compile();
32+
33+
app = moduleFixture.createNestApplication();
34+
usersService = moduleFixture.get<UsersService>(UsersService)
35+
36+
await app.init();
37+
loggedUser = await haveUserLogged(usersService)
38+
});
39+
40+
afterAll(async () => {
41+
await app.close();
42+
});
43+
44+
describe('POST /', () => {
45+
it('200', () => {
46+
return requestWithAuth(app, 'post', '/projects', project, loggedUser.token)
47+
.expect(201)
48+
.expect(res => {
49+
expect(res.body.name).toBe(project.name);
50+
})
51+
});
52+
53+
it('401', () => {
54+
return requestWithAuth(app, 'post', '/projects', project, '')
55+
.expect(401)
56+
});
57+
})
58+
59+
describe('GET /', () => {
60+
it('200', async () => {
61+
const res = await requestWithAuth(app, 'get', '/projects', {}, loggedUser.token)
62+
.expect(200)
63+
64+
expect(res.body).toEqual(expect.arrayContaining(projectServiceMock.findAll()))
65+
});
66+
67+
it('401', async () => {
68+
await requestWithAuth(app, 'get', '/projects', {}, '')
69+
.expect(401)
70+
});
71+
})
72+
73+
74+
describe('DELETE /', () => {
75+
76+
it('can delete', async () => {
77+
const res = await requestWithAuth(app, 'delete', `/projects/${project.id}`, {}, loggedUser.token)
78+
.expect(200)
79+
80+
expect(res.body).toStrictEqual(projectServiceMock.remove())
81+
})
82+
83+
it('not valid UUID', async () => {
84+
await requestWithAuth(app, 'delete', `/projects/123`, {}, loggedUser.token)
85+
.expect(400)
86+
})
87+
88+
it('not valid token', async () => {
89+
await requestWithAuth(app, 'delete', `/projects/${project.id}`, {}, 'asd')
90+
.expect(401)
91+
})
92+
})
93+
});

0 commit comments

Comments
 (0)