Skip to content

Commit 03da984

Browse files
(#129) adiciona testes e2e usuario
1 parent d15611e commit 03da984

File tree

3 files changed

+188
-4
lines changed

3 files changed

+188
-4
lines changed

e2e/app.e2e-spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { INestApplication, ValidationPipe } from '@nestjs/common';
22
import { Test, TestingModule } from '@nestjs/testing';
33
import request from 'supertest';
4-
import { AppModule } from './../src/app.module';
5-
import { AllExceptionsFilter } from './../src/shared/filters/all-exceptions.filter';
6-
import { ModelNotFoundExceptionFilter } from './../src/shared/filters/model-not-found.exception-filter';
7-
import { DataTransformInterceptor } from './../src/shared/interceptors/data-transform.interceptor';
4+
import { AppModule } from '../src/app.module';
5+
import { AllExceptionsFilter } from '../src/shared/filters/all-exceptions.filter';
6+
import { ModelNotFoundExceptionFilter } from '../src/shared/filters/model-not-found.exception-filter';
7+
import { DataTransformInterceptor } from '../src/shared/interceptors/data-transform.interceptor';
88

99
describe('App (e2e)', () => {
1010
let app: INestApplication;

e2e/usuario.e2e-spec.ts

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import { INestApplication, ValidationPipe } from '@nestjs/common';
2+
import { Test, TestingModule } from '@nestjs/testing';
3+
import { getRepositoryToken } from '@nestjs/typeorm';
4+
import request from 'supertest';
5+
import { Repository } from 'typeorm';
6+
import { AppModule } from '../src/app.module';
7+
import { AllExceptionsFilter } from '../src/shared/filters/all-exceptions.filter';
8+
import { ModelNotFoundExceptionFilter } from '../src/shared/filters/model-not-found.exception-filter';
9+
import { DataTransformInterceptor } from '../src/shared/interceptors/data-transform.interceptor';
10+
import { Usuario } from '../src/usuario/entities/usuario.entity';
11+
12+
describe('E2E - Usuario', () => {
13+
let app: INestApplication;
14+
let repository: Repository<Usuario>;
15+
16+
const user: Partial<Usuario> = {
17+
id: undefined,
18+
nome: 'Henrique',
19+
email: 'hacmelo@gmail.com',
20+
senha: '123',
21+
admin: false,
22+
};
23+
24+
beforeAll(async () => {
25+
const moduleFixture: TestingModule = await Test.createTestingModule({
26+
imports: [AppModule],
27+
}).compile();
28+
29+
app = moduleFixture.createNestApplication();
30+
31+
app.useGlobalPipes(
32+
new ValidationPipe({
33+
whitelist: true,
34+
transform: true,
35+
}),
36+
);
37+
app.useGlobalInterceptors(new DataTransformInterceptor());
38+
app.useGlobalFilters(
39+
new AllExceptionsFilter(),
40+
new ModelNotFoundExceptionFilter(),
41+
);
42+
43+
await app.startAllMicroservices();
44+
await app.init();
45+
46+
repository = app.get<Repository<Usuario>>(getRepositoryToken(Usuario));
47+
});
48+
49+
describe('POST - /api/usuario', () => {
50+
it('should successfully add a new "usuario"', async () => {
51+
const res = await request(app.getHttpServer())
52+
.post('')
53+
.set('Content-Type', 'application/json')
54+
.send(user);
55+
56+
expect(res.statusCode).toEqual(201);
57+
expect(res.body.message).toEqual('Salvo com sucesso!');
58+
expect(res.body.data).toMatchObject({ ...user, id: res.body.data.id });
59+
60+
Object.assign(user, res.body.data);
61+
delete user.senha;
62+
});
63+
64+
it('should not add a new "usuario" when validations are incorrect', async () => {
65+
const res = await request(app.getHttpServer())
66+
.post('')
67+
.set('Content-Type', 'application/json')
68+
.send({});
69+
70+
expect(res.statusCode).toEqual(400);
71+
expect(res.body.message).toBeInstanceOf(Array);
72+
expect(res.body.message).toEqual([
73+
'nome must be longer than or equal to 5 characters',
74+
'nome must be shorter than or equal to 60 characters',
75+
'nome should not be empty',
76+
'nome must be a string',
77+
'email must be shorter than or equal to 100 characters',
78+
'email must be an email',
79+
'email should not be empty',
80+
'email must be a string',
81+
'senha must be shorter than or equal to 100 characters',
82+
'senha should not be empty',
83+
'senha must be a string',
84+
]);
85+
expect(res.body.data).toBeNull();
86+
});
87+
});
88+
89+
describe('GET - /api/usuario/:id', () => {
90+
it('should successfully get "usuario" by id', async () => {
91+
const res = await request(app.getHttpServer())
92+
.get(`/${user.id}`)
93+
.set('Content-Type', 'application/json')
94+
.send();
95+
96+
expect(res.statusCode).toEqual(200);
97+
expect(res.body.message).toBeNull();
98+
expect(res.body.data).toMatchObject(user);
99+
});
100+
101+
it('should return status 400 when id is invalid', async () => {
102+
const wrongId = 'NaN';
103+
const res = await request(app.getHttpServer())
104+
.get(`/${wrongId}`)
105+
.set('Content-Type', 'application/json')
106+
.send();
107+
108+
expect(res.statusCode).toEqual(400);
109+
expect(res.body.message).toBeInstanceOf(Array);
110+
expect(res.body.message).toEqual(['ID inválido']);
111+
expect(res.body.data).toBeNull();
112+
});
113+
114+
it('should return status 404 when no "usuario" is found', async () => {
115+
const res = await request(app.getHttpServer())
116+
.get('/9999')
117+
.set('Content-Type', 'application/json')
118+
.send();
119+
120+
expect(res.statusCode).toEqual(404);
121+
expect(res.body.message).toEqual('Registro(s) não encontrado(s)!');
122+
expect(res.body.data).toBeNull();
123+
});
124+
});
125+
126+
describe('GET - /api/usuario/', () => {
127+
it('should successfully findAll "usuario"', async () => {
128+
const filter = JSON.stringify({
129+
nome: user.nome,
130+
id: user.id,
131+
email: user.email,
132+
});
133+
134+
const res = await request(app.getHttpServer())
135+
.get('?filter=' + JSON.stringify(filter))
136+
.set('Content-Type', 'application/json')
137+
.send();
138+
139+
expect(res.statusCode).toEqual(200);
140+
expect(res.body.message).toBeNull();
141+
expect(res.body.data).toEqual([user]);
142+
});
143+
});
144+
145+
describe('PATCH - /api/usuario/:id', () => {
146+
it('should successfully update "usuario" by id', async () => {
147+
const update = { nome: 'Jose da Silva' };
148+
149+
const res = await request(app.getHttpServer())
150+
.patch(`/${user.id}`)
151+
.set('Content-Type', 'application/json')
152+
.send(update);
153+
154+
user.nome = update.nome;
155+
156+
expect(res.statusCode).toEqual(200);
157+
expect(res.body.message).toBe('Atualizado com sucesso!');
158+
expect(res.body.data).toMatchObject(user);
159+
});
160+
});
161+
162+
describe('DELETE - /api/usuario/:id', () => {
163+
it('should successfully delete "usuario" by id', async () => {
164+
const res = await request(app.getHttpServer())
165+
.delete(`/${user.id}`)
166+
.set('Content-Type', 'application/json')
167+
.send();
168+
169+
delete user.id;
170+
171+
expect(res.statusCode).toEqual(200);
172+
expect(res.body.message).toBe('Excluído com sucesso!');
173+
expect(res.body.data).toMatchObject(user);
174+
});
175+
});
176+
177+
afterAll(async () => {
178+
await repository.query('TRUNCATE usuario CASCADE');
179+
await repository.delete({});
180+
await app.close();
181+
});
182+
});

src/usuario/dto/create-usuario.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
} from 'class-validator';
1010

1111
export class CreateUsuarioDto {
12+
// TODO colocar mensagens customizadas "user friendly" em todos os validators
13+
1214
@IsString()
1315
@IsNotEmpty()
1416
@MaxLength(60)

0 commit comments

Comments
 (0)