Skip to content

Commit b19c201

Browse files
Merge pull request #2 from AlexAlexandreAlves/devBase2
Atualizando o uso do token
2 parents 7d6f761 + 1c03203 commit b19c201

File tree

5 files changed

+70
-26
lines changed

5 files changed

+70
-26
lines changed

constants/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export const BASE_URL = 'https://fakerestapi.azurewebsites.net/'
1+
export const BASE_URL = 'https://fakerestapi.azurewebsites.net/'
2+
export const TOKEN = 'YOUR_TOKEN_HERE'

integration/data/author-data.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ export const validaRetornoAutorInvalido = {
3939
type: 'https://tools.ietf.org/html/rfc7231#section-6.5.1',
4040
title: 'One or more validation errors occurred.',
4141
status: 400,
42+
}
43+
44+
export const atualizaAutor = {
45+
firstName: "Han",
46+
lastName: "Solo"
4247
}

integration/entity/entities.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { expect } from '@jest/globals';
22
import * as request from 'supertest';
33
import { BASE_URL } from '../../constants/constants';
4+
// import { TOKEN } from '../../constants/constants'; // Descomente para usar token global
45

56
export class EntityService {
67

78
public async getList(route: string, statusCode: number, content?: any, checkResponseMessage?: string) {
8-
const response = await request(BASE_URL)
9-
.get(route)
9+
let req = request(BASE_URL).get(route);
10+
// req = req.set('Authorization', `Bearer ${TOKEN}`); // Descomente para usar token global
11+
const response = await req
1012

1113
expect(response.statusCode).toBe(statusCode)
1214

@@ -23,8 +25,9 @@ export class EntityService {
2325
};
2426

2527
public async getById(route: string, id: number, statusCode: number, content?: any, checkResponseMessage?: string) {
26-
const response = await request(BASE_URL)
27-
.get(`${route}` + `${id}`)
28+
let req = request(BASE_URL).get(`${route}${id}`);
29+
// req = req.set('Authorization', `Bearer ${TOKEN}`);
30+
const response = await req
2831

2932
expect(response.statusCode).toBe(statusCode)
3033

@@ -40,9 +43,9 @@ export class EntityService {
4043
};
4144

4245
public async create(route: string, data: object, statusCode: number, content?: any, checkResponseMessage?: string) {
43-
const response = await request(BASE_URL)
44-
.post(route)
45-
.send(data)
46+
let req = request(BASE_URL).post(route).send(data);
47+
// req = req.set('Authorization', `Bearer ${TOKEN}`);
48+
const response = await req
4649

4750
expect(response.statusCode).toBe(statusCode)
4851

@@ -57,10 +60,10 @@ export class EntityService {
5760
return response
5861
};
5962

60-
public async update(route: string, data: object, statusCode: number, content?: any, checkResponseMessage?: string) {
61-
const response = await request(BASE_URL)
62-
.put(route)
63-
.send(data)
63+
public async update(route: string, id: number, data: object, statusCode: number, content?: any, checkResponseMessage?: string) {
64+
let req = request(BASE_URL).put(`${route}${id}`).send(data);
65+
// req = req.set('Authorization', `Bearer ${TOKEN}`);
66+
const response = await req
6467

6568
expect(response.statusCode).toBe(statusCode)
6669

@@ -75,9 +78,10 @@ export class EntityService {
7578
return response
7679
};
7780

78-
public async delete(route: string, id: number, statusCode: number, content?: any, checkResponseMessage?: string) {
79-
const response = await request(BASE_URL)
80-
.delete(`${route}` + `${id}`)
81+
public async delete(route: string, id: number, statusCode: number, content?: any, checkResponseMessage?: string) {
82+
let req = request(BASE_URL).delete(`${route}${id}`);
83+
// req = req.set('Authorization', `Bearer ${TOKEN}`);
84+
const response = await req
8185

8286
expect(response.statusCode).toBe(statusCode)
8387

integration/tests/exemplo-usando-entities.test.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@ import {
55
dadoEsperadoGetById,
66
getByIdNaoEncontrado,
77
dadoEsperadoGetList,
8-
tentaCriarUmAutorComDadosInvalidos
8+
tentaCriarUmAutorComDadosInvalidos,
9+
atualizaAutor
910
} from '../data/author-data';
1011
import { EntityService } from '../entity/entities';
1112
import { authorsRoute } from '../routes/author-route';
1213

1314

14-
const getList = authorsRoute.getListAuthors
15-
const getById = authorsRoute.getAuthorsById
16-
const create = authorsRoute.criaAutors
15+
const getListRoute = authorsRoute.getListAuthors;
16+
const getByIdRoute = authorsRoute.getAuthorsById;
17+
const createRoute = authorsRoute.criaAutors;
18+
const updateRoute = authorsRoute.updateAuthors;
19+
const deleteRoute = authorsRoute.deleteAuthors;
1720

1821
const entity = new EntityService();
1922

2023
describe('Exemplos de teste usando o Get list', () => {
2124

2225
it('Deve encontrar o registro e retornar o código de status 200', async () => {
2326
await entity.getList(
24-
getList, // Rota para listar autores
27+
getListRoute, // Rota para listar autores
2528
200, // Código de status esperado
2629
dadoEsperadoGetList // Dados esperados na resposta
2730
)
@@ -33,7 +36,7 @@ describe('Exemplos de teste usando o Get by id', () => {
3336

3437
it('Deve encontrar o registro pelo id e retornar o código de status 200', async () => {
3538
await entity.getById(
36-
getById,
39+
getByIdRoute,
3740
2, // ID do autor a ser buscado
3841
200,
3942
dadoEsperadoGetById // Dados esperados na resposta
@@ -42,7 +45,7 @@ describe('Exemplos de teste usando o Get by id', () => {
4245

4346
it('Deve tentar encontrar o registro pelo id, mas não encontrado e retornar o código de status 404', async () => {
4447
await entity.getById(
45-
getById,
48+
getByIdRoute,
4649
1000,
4750
404,
4851
getByIdNaoEncontrado
@@ -54,7 +57,7 @@ describe('Exemplos de teste usando o Create', () => {
5457

5558
it('Deve criar um autor e verificar os dados da resposta e o código de status 200', async () => {
5659
await entity.create(
57-
create, // Rota para criar autor
60+
createRoute, // Rota para criar autor
5861
criaAutor, // Dados do autor a ser criado
5962
200,
6063
criaAutor
@@ -63,10 +66,36 @@ describe('Exemplos de teste usando o Create', () => {
6366

6467
it('Deve tentar criar um autor com dados inválidos e verificar a resposta e o código de status 400', async () => {
6568
await entity.create(
66-
create, // Rota para criar autor
69+
createRoute, // Rota para criar autor
6770
tentaCriarUmAutorComDadosInvalidos, // Dados do autor a ser criado
6871
400, // Código de status esperado
6972
validaRetornoAutorInvalido
7073
)
7174
})
75+
76+
});
77+
78+
describe('Exemplos de teste usando o Update', () => {
79+
80+
it('Deve criar um autor e verificar os dados da resposta e o código de status 200', async () => {
81+
await entity.update(
82+
updateRoute, // Rota para atualizar autor
83+
1, // ID do autor a ser atualizado
84+
atualizaAutor, // Dados do autor a ser atualizado
85+
200,
86+
atualizaAutor // Dados esperados na resposta
87+
)
88+
})
89+
});
90+
91+
92+
describe('Exemplos de teste usando o delete', () => {
93+
94+
it('Deve deletar o registro pelo id e retornar o código de status 200', async () => {
95+
await entity.delete(
96+
deleteRoute,
97+
1, // ID do autor a ser deletado
98+
200
99+
)
100+
})
72101
});

integration/tests/exemplo-uso-padrao.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { expect } from '@jest/globals';
22
import * as request from 'supertest';
33
import { BASE_URL } from '../../constants/constants';
4+
import { TOKEN } from '../../constants/constants';
45

56
describe('Exemplos de teste usando o Get list', () => {
67

78
it('Deve encontrar o registro e retornar o código de status 200', async () => {
8-
const response = await request(BASE_URL).get('api/v1/Authors');
9+
const response = await request(BASE_URL).get('api/v1/Authors')
10+
// .set('Authorization', `Bearer ${TOKEN}`); Configura o TOKEN aqui
911

1012
expect(response.status).toBe(200);
1113
expect(response.body[0]).toEqual(expect.objectContaining(
@@ -22,6 +24,7 @@ describe('Exemplos de teste usando o Get list', () => {
2224

2325
it('Deve encontrar o registro pelo id e retornar o código de status 200', async () => {
2426
const response = await request(BASE_URL).get('api/v1/Authors/2');
27+
// .set('Authorization', `Bearer ${TOKEN}`);
2528

2629
expect(response.status).toBe(200);
2730
expect(response.body).toEqual(
@@ -36,6 +39,7 @@ describe('Exemplos de teste usando o Get list', () => {
3639

3740
it('Deve tentar encontrar o registro pelo id, mas não encontrado e retornar o código de status 404', async () => {
3841
const response = await request(BASE_URL).get('api/v1/Authors/1000');
42+
// .set('Authorization', `Bearer ${TOKEN}`);
3943

4044
expect(response.status).toBe(404);
4145
expect(response.body).toEqual(expect.objectContaining(
@@ -60,6 +64,7 @@ describe('Exemplos de teste usando o Get list', () => {
6064
};
6165

6266
const response = await request(BASE_URL)
67+
// .set('Authorization', `Bearer ${TOKEN}`);
6368
.post('/api/v1/Authors')
6469
.send(novoAutor);
6570

@@ -73,7 +78,6 @@ describe('Exemplos de teste usando o Get list', () => {
7378

7479
}
7580
));
76-
7781
});
7882

7983
it('Deve tentar criar um autor com dados inválidos e verificar a resposta e o código de status 400', async () => {
@@ -85,6 +89,7 @@ describe('Exemplos de teste usando o Get list', () => {
8589
};
8690

8791
const response = await request(BASE_URL)
92+
// .set('Authorization', `Bearer ${TOKEN}`);
8893
.post('/api/v1/Authors')
8994
.send(invalidAuthor);
9095

0 commit comments

Comments
 (0)