Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#170 crud idoso backend #7

Merged
merged 9 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ DB_PASS=postgres
DB_DATABASE=gerocuidado-saude-db
DB_PORT=5003

## TCP
AUTH_HOST=gerocuidado-usuario-api
AUTH_PORT=4001
#TCP
USUARIO_HOST=gerocuidado-usuario-api
USUARIO_PORT=4001
6 changes: 3 additions & 3 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ DB_PASS=postgres
DB_DATABASE=gerocuidado-saude-db-test
DB_PORT=5003

## TCP
AUTH_HOST=gerocuidado-usuario-api
AUTH_PORT=4001
#TCP
USUARIO_HOST=0.0.0.0
USUARIO_PORT=8001
14 changes: 1 addition & 13 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GIT_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

test-e2e:
Expand All @@ -43,15 +43,3 @@ jobs:
docker-compose -f docker-compose.test.yml up -V --force-recreate --build --abort-on-container-exit --exit-code-from gerocuidado-saude-api-test
env:
TEST: e2e
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
50 changes: 0 additions & 50 deletions .github/workflows/check.yml

This file was deleted.

4 changes: 2 additions & 2 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ services:
- DB_PASS=postgres
- DB_DATABASE=gerocuidado-saude-db
- DB_PORT=5003
- AUTH_HOST=gerocuidado-usuario-api-prod
- AUTH_PORT=4001
- USUARIO_HOST=gerocuidado-usuario-api-prod
- USUARIO_PORT=4001
ports:
- '3003:3003'
depends_on:
Expand Down
244 changes: 244 additions & 0 deletions e2e/idoso.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
import { Controller, INestApplication, ValidationPipe } from '@nestjs/common';
import {
ClientProxy,
ClientsModule,
MessagePattern,
Transport,
} from '@nestjs/microservices';
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import request from 'supertest';
import { Repository } from 'typeorm';
import { AppModule } from '../src/app.module';
import { ETipoSanguineo } from '../src/idoso/classes/tipo-sanguineo.enum';
import { Idoso } from '../src/idoso/entities/idoso.entity';
import { AllExceptionsFilter } from '../src/shared/filters/all-exceptions.filter';
import { ModelNotFoundExceptionFilter } from '../src/shared/filters/model-not-found.exception-filter';
import { DataTransformInterceptor } from '../src/shared/interceptors/data-transform.interceptor';

@Controller()
class AutenticacaoController {
@MessagePattern({ role: 'auth', cmd: 'check' })
async validateToken(data: { jwt: string }) {
return true;
}
}

describe('E2E - Idoso', () => {
let app: INestApplication;
let client: ClientProxy;
let repository: Repository<Idoso>;
let token: string =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5ceyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';

const idoso: Partial<Idoso> = {
id: undefined,
nome: 'Henrique',
foto: '1' as any,
idUsuario: 1,
dataNascimento: new Date().toISOString() as any,
tipoSanguineo: ETipoSanguineo.AB_Negativo,
telefoneResponsavel: '123456789',
descricao: 'desc',
};

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
AppModule,
ClientsModule.register([
{
name: 'USUARIO_CLIENT',
transport: Transport.TCP,
options: {
host: '0.0.0.0',
port: 8001,
},
},
]),
],
controllers: [AutenticacaoController],
}).compile();

app = moduleFixture.createNestApplication();

app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
}),
);
app.useGlobalInterceptors(new DataTransformInterceptor());
app.useGlobalFilters(
new AllExceptionsFilter(),
new ModelNotFoundExceptionFilter(),
);

app.connectMicroservice({
transport: Transport.TCP,
options: {
host: '0.0.0.0',
port: 8001,
},
});

await app.startAllMicroservices();
await app.init();

client = app.get('USUARIO_CLIENT');
await client.connect();

repository = app.get<Repository<Idoso>>(getRepositoryToken(Idoso));
});

describe('POST - /api/saude/idoso', () => {
it('should successfully add a new "idoso"', async () => {
const res = await request(app.getHttpServer())
.post('/idoso')
.set('Content-Type', 'application/json')
.send(idoso);

expect(res.statusCode).toEqual(201);
expect(res.body.message).toEqual('Salvo com sucesso!');
expect(res.body.data).toMatchObject({
...idoso,
id: res.body.data.id,
});

Object.assign(idoso, res.body.data);
delete idoso.foto;
});

it('should not add a new "idoso" when validations are incorrect', async () => {
const res = await request(app.getHttpServer())
.post('/idoso')
.set('Content-Type', 'application/json')
.send({});

expect(res.statusCode).toEqual(400);
expect(res.body.message).toBeInstanceOf(Array);
expect(res.body.message).toEqual([
'idUsuario should not be empty',
'idUsuario must be a number conforming to the specified constraints',
'nome must be longer than or equal to 5 characters',
'nome must be shorter than or equal to 60 characters',
'nome should not be empty',
'nome must be a string',
'dataNascimento should not be empty',
'dataNascimento must be a valid ISO 8601 date string',
'telefoneResponsavel must be shorter than or equal to 11 characters',
'telefoneResponsavel must be longer than or equal to 9 characters',
'telefoneResponsavel should not be empty',
'telefoneResponsavel must be a string',
'descricao should not be empty',
'descricao must be shorter than or equal to 500 characters',
'descricao must be a string',
]);
expect(res.body.data).toBeNull();
});
});

describe('GET - /api/saude/idoso/:id', () => {
it('should successfully get "idoso" by id', async () => {
const res = await request(app.getHttpServer())
.get(`/idoso/${idoso.id}`)
.set('Content-Type', 'application/json')
.set('Authorization', 'bearer ' + token)
.send();

expect(res.statusCode).toEqual(200);
expect(res.body.message).toBeNull();
const data = res.body.data;
delete data.foto;
expect(data).toMatchObject(idoso);
});

it('should return status 400 when id is invalid', async () => {
const wrongId = 'NaN';
const res = await request(app.getHttpServer())
.get(`/idoso/${wrongId}`)
.set('Content-Type', 'application/json')
.set('Authorization', 'bearer ' + token)
.send();

expect(res.statusCode).toEqual(400);
expect(res.body.message).toBeInstanceOf(Array);
expect(res.body.message).toEqual(['ID inválido']);
expect(res.body.data).toBeNull();
});

it('should return status 404 when no "idoso" is found', async () => {
const res = await request(app.getHttpServer())
.get('/idoso/9999')
.set('Content-Type', 'application/json')
.set('Authorization', 'bearer ' + token)
.send();

expect(res.statusCode).toEqual(404);
expect(res.body.message).toEqual('Registro(s) não encontrado(s)!');
expect(res.body.data).toBeNull();
});
});

describe('GET - /api/saude/idoso/', () => {
it('should successfully findAll "idoso"', async () => {
const filter = JSON.stringify({
nome: idoso.nome,
id: idoso.id,
});

const res = await request(app.getHttpServer())
.get('/idoso?filter=' + JSON.stringify(filter))
.set('Content-Type', 'application/json')
.set('Authorization', 'bearer ' + token)
.send();

expect(res.statusCode).toEqual(200);
expect(res.body.message).toBeNull();
expect(res.body.data.length).toEqual(1);
});
});

describe('PATCH - /api/saude/idoso/:id', () => {
it('should successfully update "idoso" by id', async () => {
const update = { nome: 'Jose da Silva' };

const res = await request(app.getHttpServer())
.patch(`/idoso/${idoso.id}`)
.set('Content-Type', 'application/json')
.set('Authorization', 'bearer ' + token)
.send(update);

idoso.nome = update.nome;

expect(res.statusCode).toEqual(200);
expect(res.body.message).toBe('Atualizado com sucesso!');
const data = res.body.data;
delete data.foto;
expect(data).toMatchObject(idoso);
});
});

describe('DELETE - /api/saude/idoso/:id', () => {
it('should successfully delete "idoso" by id', async () => {
const res = await request(app.getHttpServer())
.delete(`/idoso/${idoso.id}`)
.set('Content-Type', 'application/json')
.set('Authorization', 'bearer ' + token)
.send();

delete idoso.id;

expect(res.statusCode).toEqual(200);
expect(res.body.message).toBe('Excluído com sucesso!');
expect(res.body.data).toMatchObject(idoso);
});
});

afterAll(async () => {
await repository.query('TRUNCATE idoso CASCADE');
await repository.delete({});
await app.close();
await client.close();
});
});
29 changes: 23 additions & 6 deletions e2e/jest-e2e.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"moduleFileExtensions": ["ts", "tsx", "js", "json"],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
Expand All @@ -11,10 +16,22 @@
"!**/node_modules/**",
"!**/vendor/**",
"!src/**/main.{js,jsx,tsx,ts}",
"!src/**/ormconfig.{js,jsx,tsx,ts}"
"!src/**/migrations.{js,jsx,tsx,ts}",
"!src/**/ormconfig.{js,jsx,tsx,ts}",
"!src/migration/**",
"!src/shared/**",
"!src/config/**"
],
"testPathIgnorePatterns": [
"/node_modules/",
"/src/"
],
"coverageReporters": [
"json",
"lcov"
],
"testPathIgnorePatterns": ["/node_modules/", "/src/"],
"coverageReporters": ["json", "lcov"],
"testEnvironment": "node",
"coveragePathIgnorePatterns": [".spec.ts$"]
}
"coveragePathIgnorePatterns": [
".spec.ts$"
]
}
Loading