Skip to content

Commit e7151ff

Browse files
authored
Merge pull request #11 from Visual-Regression-Tracker/build-e2e-tests
build e2e tests added
2 parents f0a724e + bbbb178 commit e7151ff

File tree

5 files changed

+176
-56
lines changed

5 files changed

+176
-56
lines changed

src/auth/guards/api.guard.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
import {
2-
ExecutionContext,
3-
Injectable,
4-
CanActivate,
5-
UnauthorizedException,
6-
} from '@nestjs/common';
1+
import { ExecutionContext, Injectable, CanActivate, UnauthorizedException } from '@nestjs/common';
72
import { Request } from 'express';
83
import { PrismaService } from '../../prisma/prisma.service';
94

105
@Injectable()
116
export class ApiGuard implements CanActivate {
12-
constructor(private readonly prismaService: PrismaService) { }
7+
constructor(private readonly prismaService: PrismaService) {}
138

149
async canActivate(context: ExecutionContext): Promise<boolean> {
1510
const request: Request = context.switchToHttp().getRequest();
1611
try {
1712
const user = await this.prismaService.user.findOne({
18-
where: { apiKey: request.header('apiKey') }
13+
where: { apiKey: request.header('apiKey') },
1914
});
2015
return !!user;
2116
} catch {

src/users/users.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export class UsersService {
4444
return this.prismaService.user.findOne({ where: { id } });
4545
}
4646

47+
async delete(id: string): Promise<User> {
48+
return this.prismaService.user.delete({ where: { id } });
49+
}
50+
4751
async get(id: string): Promise<UserDto> {
4852
const user = await this.findOne(id);
4953
return new UserDto(user);

test/builds.e2e-spec.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 { haveUserLogged, requestWithApiKey, requestWithAuth } from './preconditions';
6+
import { BuildsService } from '../src/builds/builds.service';
7+
import { CreateBuildDto } from '../src/builds/dto/build-create.dto';
8+
import { UserLoginResponseDto } from '../src/users/dto/user-login-response.dto';
9+
import { Project } from '@prisma/client';
10+
import { ProjectsService } from '../src/projects/projects.service';
11+
12+
describe('Builds (e2e)', () => {
13+
let app: INestApplication;
14+
let buildsService: BuildsService;
15+
let projecstService: ProjectsService;
16+
let usersService: UsersService;
17+
let user: UserLoginResponseDto;
18+
let project: Project;
19+
20+
beforeAll(async () => {
21+
const moduleFixture: TestingModule = await Test.createTestingModule({
22+
imports: [AppModule],
23+
}).compile();
24+
25+
app = moduleFixture.createNestApplication();
26+
buildsService = moduleFixture.get<BuildsService>(BuildsService);
27+
usersService = moduleFixture.get<UsersService>(UsersService);
28+
projecstService = moduleFixture.get<ProjectsService>(ProjectsService);
29+
30+
await app.init();
31+
});
32+
33+
beforeEach(async () => {
34+
user = await haveUserLogged(usersService);
35+
project = await projecstService.create({ name: 'E2E test' });
36+
});
37+
38+
afterEach(async () => {
39+
await projecstService.remove(project.id);
40+
await usersService.delete(user.id);
41+
});
42+
43+
afterAll(async () => {
44+
await app.close();
45+
});
46+
47+
describe('POST /', () => {
48+
it('201', () => {
49+
const createBuildDto: CreateBuildDto = {
50+
branchName: 'branchName',
51+
projectId: project.id,
52+
};
53+
return requestWithApiKey(app, 'post', '/builds', createBuildDto, user.apiKey)
54+
.expect(201)
55+
.expect(res => {
56+
expect(res.body.projectId).toBe(project.id);
57+
expect(res.body.branchName).toBe(createBuildDto.branchName);
58+
expect(res.body.failedCount).toBe(0);
59+
expect(res.body.passedCount).toBe(0);
60+
expect(res.body.unresolvedCount).toBe(0);
61+
});
62+
});
63+
64+
it('403', () => {
65+
const createBuildDto: CreateBuildDto = {
66+
branchName: 'branchName',
67+
projectId: project.id,
68+
};
69+
return requestWithApiKey(app, 'post', '/builds', createBuildDto, '').expect(403);
70+
});
71+
});
72+
73+
describe('GET /', () => {
74+
it('200', async () => {
75+
const build = await buildsService.create({ projectId: project.id, branchName: 'develop' });
76+
77+
return requestWithAuth(app, 'get', `/builds?projectId=${project.id}`, {}, user.token)
78+
.expect(200)
79+
.expect(res => {
80+
expect(JSON.stringify(res.body)).toEqual(JSON.stringify([build]));
81+
});
82+
});
83+
84+
it('401', async () => {
85+
return requestWithAuth(app, 'get', `/builds?projectId=${project.id}`, {}, '').expect(401);
86+
});
87+
});
88+
89+
describe('DELETE /', () => {
90+
it('200', async () => {
91+
const build = await buildsService.create({ projectId: project.id, branchName: 'develop' });
92+
93+
return requestWithAuth(app, 'delete', `/builds/${build.id}`, {}, user.token).expect(200);
94+
});
95+
96+
it('401', async () => {
97+
const build = await buildsService.create({ projectId: project.id, branchName: 'develop' });
98+
99+
return requestWithAuth(app, 'delete', `/builds/${build.id}`, {}, '').expect(401);
100+
});
101+
});
102+
});

test/preconditions.ts

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
11
import { INestApplication } from '@nestjs/common';
22
import { UsersService } from 'src/users/users.service';
3-
import { UserLoginRequestDto } from 'src/users/dto/user-login-request.dto';
43
import uuidAPIKey from 'uuid-apikey';
5-
import request, { Test, CallbackHandler, Request } from 'supertest';
6-
import { CreateUserDto } from 'src/users/dto/user-create.dto';
4+
import request, { Test } from 'supertest';
75

8-
export const generateUser = (password: string): { email: string, password: string, firstName: string, lastName: string } => ({
9-
email: `${uuidAPIKey.create().uuid}@example.com'`,
10-
password,
11-
firstName: 'fName',
12-
lastName: 'lName',
13-
})
6+
export const generateUser = (
7+
password: string
8+
): { email: string; password: string; firstName: string; lastName: string } => ({
9+
email: `${uuidAPIKey.create().uuid}@example.com'`,
10+
password,
11+
firstName: 'fName',
12+
lastName: 'lName',
13+
});
14+
15+
export const requestWithAuth = (
16+
app: INestApplication,
17+
method: 'post' | 'get' | 'put' | 'delete',
18+
url: string,
19+
body = {},
20+
token: string
21+
): Test =>
22+
request(app.getHttpServer())
23+
[method](url)
24+
.set('Authorization', 'Bearer ' + token)
25+
.send(body);
1426

15-
export const requestWithAuth = (app: INestApplication, method: 'post' | 'get' | 'put' | 'delete', url: string, body = {}, token: string): Test =>
16-
request(app.getHttpServer())
27+
export const requestWithApiKey = (
28+
app: INestApplication,
29+
method: 'post' | 'get' | 'put' | 'delete',
30+
url: string,
31+
body = {},
32+
apiKey: string
33+
): Test =>
34+
request(app.getHttpServer())
1735
[method](url)
18-
.set('Authorization', 'Bearer ' + token)
19-
.send(body)
36+
.set('apiKey', apiKey)
37+
.send(body);
2038

2139
export const haveUserLogged = async (usersService: UsersService) => {
22-
const password = '123456'
23-
const user = await usersService.create(generateUser(password))
40+
const password = '123456';
41+
const user = await usersService.create(generateUser(password));
2442

25-
return usersService.login({
26-
email: user.email,
27-
password
28-
})
29-
}
43+
return usersService.login({
44+
email: user.email,
45+
password,
46+
});
47+
};

test/projects.e2e-spec.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { UsersService } from '../src/users/users.service';
55
import { requestWithAuth, haveUserLogged } from './preconditions';
66
import { ProjectsService } from '../src/projects/projects.service';
77
import uuidAPIKey from 'uuid-apikey';
8+
import { UserLoginResponseDto } from 'src/users/dto/user-login-response.dto';
89

910
const project = {
1011
id: uuidAPIKey.create().uuid,
11-
name: 'Test project'
12-
}
12+
name: 'Test project',
13+
};
1314

1415
const projectServiceMock = {
1516
findAll: () => ['test'],
@@ -20,7 +21,7 @@ const projectServiceMock = {
2021
describe('Projects (e2e)', () => {
2122
let app: INestApplication;
2223
let usersService: UsersService;
23-
let loggedUser
24+
let loggedUser: UserLoginResponseDto;
2425

2526
beforeAll(async () => {
2627
const moduleFixture: TestingModule = await Test.createTestingModule({
@@ -31,10 +32,18 @@ describe('Projects (e2e)', () => {
3132
.compile();
3233

3334
app = moduleFixture.createNestApplication();
34-
usersService = moduleFixture.get<UsersService>(UsersService)
35+
usersService = moduleFixture.get<UsersService>(UsersService);
36+
usersService = moduleFixture.get<UsersService>(UsersService);
3537

3638
await app.init();
37-
loggedUser = await haveUserLogged(usersService)
39+
});
40+
41+
beforeEach(async () => {
42+
loggedUser = await haveUserLogged(usersService);
43+
});
44+
45+
afterEach(async () => {
46+
await usersService.delete(loggedUser.id);
3847
});
3948

4049
afterAll(async () => {
@@ -47,47 +56,39 @@ describe('Projects (e2e)', () => {
4756
.expect(201)
4857
.expect(res => {
4958
expect(res.body.name).toBe(project.name);
50-
})
59+
});
5160
});
5261

5362
it('401', () => {
54-
return requestWithAuth(app, 'post', '/projects', project, '')
55-
.expect(401)
63+
return requestWithAuth(app, 'post', '/projects', project, '').expect(401);
5664
});
57-
})
65+
});
5866

5967
describe('GET /', () => {
6068
it('200', async () => {
61-
const res = await requestWithAuth(app, 'get', '/projects', {}, loggedUser.token)
62-
.expect(200)
69+
const res = await requestWithAuth(app, 'get', '/projects', {}, loggedUser.token).expect(200);
6370

64-
expect(res.body).toEqual(expect.arrayContaining(projectServiceMock.findAll()))
71+
expect(res.body).toEqual(expect.arrayContaining(projectServiceMock.findAll()));
6572
});
6673

6774
it('401', async () => {
68-
await requestWithAuth(app, 'get', '/projects', {}, '')
69-
.expect(401)
75+
await requestWithAuth(app, 'get', '/projects', {}, '').expect(401);
7076
});
71-
})
72-
77+
});
7378

7479
describe('DELETE /', () => {
75-
7680
it('can delete', async () => {
77-
const res = await requestWithAuth(app, 'delete', `/projects/${project.id}`, {}, loggedUser.token)
78-
.expect(200)
81+
const res = await requestWithAuth(app, 'delete', `/projects/${project.id}`, {}, loggedUser.token).expect(200);
7982

80-
expect(res.body).toStrictEqual(projectServiceMock.remove())
81-
})
83+
expect(res.body).toStrictEqual(projectServiceMock.remove());
84+
});
8285

8386
it('not valid UUID', async () => {
84-
await requestWithAuth(app, 'delete', `/projects/123`, {}, loggedUser.token)
85-
.expect(400)
86-
})
87+
await requestWithAuth(app, 'delete', `/projects/123`, {}, loggedUser.token).expect(400);
88+
});
8789

8890
it('not valid token', async () => {
89-
await requestWithAuth(app, 'delete', `/projects/${project.id}`, {}, 'asd')
90-
.expect(401)
91-
})
92-
})
91+
await requestWithAuth(app, 'delete', `/projects/${project.id}`, {}, 'asd').expect(401);
92+
});
93+
});
9394
});

0 commit comments

Comments
 (0)