generated from HenriqueAmorim20/GEROcuidadoAPITemplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from fga-eps-mds/feature/#137-criptografia_senh…
…a_e_verificacao_email Feature/#137 criptografia senha e verificacao email
- Loading branch information
Showing
5 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,7 @@ DB_USERNAME=postgres | |
DB_PASS=postgres | ||
DB_DATABASE=gerocuidado-usuario-db | ||
DB_PORT=5001 | ||
|
||
#BCRYPT | ||
HASH_SALT=10 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ DB_USERNAME=postgres | |
DB_PASS=postgres | ||
DB_DATABASE=gerocuidado-usuario-db-test | ||
DB_PORT=5001 | ||
|
||
#BCRYPT | ||
HASH_SALT=10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import bcrypt from 'bcrypt'; | ||
import { Repository } from 'typeorm'; | ||
import { OrderParams, Ordering } from '../shared/decorators/ordenate.decorator'; | ||
import { | ||
|
@@ -12,11 +15,13 @@ import { UsuarioService } from './usuario.service'; | |
describe('UsuarioService', () => { | ||
let service: UsuarioService; | ||
let repository: Repository<Usuario>; | ||
let configService: ConfigService; | ||
|
||
const mockRepository = { | ||
save: jest.fn(), | ||
findOneOrFail: jest.fn(), | ||
remove: jest.fn(), | ||
findOne: jest.fn(), | ||
createQueryBuilder: jest.fn(() => ({ | ||
where: jest.fn().mockReturnThis(), | ||
limit: jest.fn().mockReturnThis(), | ||
|
@@ -26,19 +31,28 @@ describe('UsuarioService', () => { | |
})), | ||
}; | ||
|
||
const mockConfigService = { | ||
get: jest.fn(), | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
UsuarioService, | ||
{ | ||
provide: getRepositoryToken(Usuario), | ||
useValue: mockRepository, | ||
}, | ||
UsuarioService, | ||
{ | ||
provide: ConfigService, | ||
useValue: mockConfigService, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<UsuarioService>(UsuarioService); | ||
repository = module.get<Repository<Usuario>>(getRepositoryToken(Usuario)); | ||
configService = module.get<ConfigService>(ConfigService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
|
@@ -48,11 +62,27 @@ describe('UsuarioService', () => { | |
it('should create Usuario', async () => { | ||
const user = { nome: 'Henrique' } as any; | ||
jest.spyOn(repository, 'save').mockReturnValue({ id: 1 } as any); | ||
|
||
jest.spyOn(repository, 'findOne').mockReturnValue(undefined as any); | ||
jest.spyOn(configService, 'get').mockReturnValue(10 as any); | ||
jest | ||
.spyOn(bcrypt, 'hash') | ||
.mockImplementation((pass: string | Buffer, salt: string | number) => | ||
Promise.resolve('senha'), | ||
); | ||
const created = await service.create(user); | ||
expect(created.id).toEqual(1); | ||
}); | ||
|
||
it('should not create Usuario', async () => { | ||
const user = { nome: 'Henrique' } as any; | ||
jest | ||
.spyOn(repository, 'findOne') | ||
.mockReturnValue({ email: '[email protected]' } as any); | ||
expect(service.create(user)).rejects.toThrow( | ||
new BadRequestException('Este email já está cadastrado!'), | ||
); | ||
}); | ||
|
||
it('should find Usuario', async () => { | ||
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({ id: 1 } as any); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters