forked from fga-eps-mds/2024-1-GEROcuidado-APIUsuario
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testes do service.ts com maior cobertura de linhas
- Loading branch information
Showing
1 changed file
with
204 additions
and
125 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 |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
import { BadRequestException, NotFoundException } 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 { | ||
Pagination, | ||
PaginationParams, | ||
} from '../shared/decorators/paginate.decorator'; | ||
import { ResetarSenhaDto } from './dto/resetar-senha.dto'; | ||
import { sendResetEmail } from './email.senha'; | ||
import { Usuario } from './entities/usuario.entity'; | ||
import { UsuarioService } from './usuario.service'; | ||
|
||
jest.mock('./email.senha', () => ({ | ||
sendResetEmail: jest.fn(), | ||
})); | ||
|
||
const getImageUri = jest.fn().mockImplementation((foto) => `http://image.uri/${foto}`); | ||
|
||
|
||
describe('UsuarioService', () => { | ||
let service: UsuarioService; | ||
let repository: Repository<Usuario>; | ||
|
@@ -28,11 +31,14 @@ describe('UsuarioService', () => { | |
offset: jest.fn().mockReturnThis(), | ||
orderBy: jest.fn().mockReturnThis(), | ||
getManyAndCount: jest.fn(), | ||
getMany: jest.fn(), | ||
getOne: jest.fn(), | ||
})), | ||
find: jest.fn(), | ||
}; | ||
|
||
const mockConfigService = { | ||
get: jest.fn(), | ||
get: jest.fn().mockReturnValue('10'), | ||
}; | ||
|
||
beforeEach(async () => { | ||
|
@@ -59,153 +65,226 @@ describe('UsuarioService', () => { | |
expect(service).toBeDefined(); | ||
}); | ||
|
||
it('should create Usuario', async () => { | ||
const user = { nome: 'Henrique' } as any; | ||
jest.spyOn(repository, 'save').mockReturnValue({ id: 1 } as any); | ||
jest.spyOn(repository, 'createQueryBuilder').mockReturnValue({ | ||
where: () => ({ | ||
addSelect: () => ({ | ||
getOne: jest.fn().mockResolvedValueOnce(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); | ||
}); | ||
describe('testDbConnection', () => { | ||
it('should fetch users from the database and log them', async () => { | ||
const mockUsers = [{ id: 1, nome: 'User Test' }]; | ||
mockRepository.find.mockResolvedValue(mockUsers); | ||
|
||
it('should not create Usuario', async () => { | ||
const user = { nome: 'Henrique' } as any; | ||
jest.spyOn(repository, 'createQueryBuilder').mockReturnValue({ | ||
where: () => ({ | ||
addSelect: () => ({ | ||
getOne: jest | ||
.fn() | ||
.mockResolvedValueOnce({ email: '[email protected]' } as any), | ||
}), | ||
}), | ||
} as any); | ||
expect(service.create(user)).rejects.toThrow( | ||
new BadRequestException('Este email já está cadastrado!'), | ||
); | ||
}); | ||
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(); | ||
|
||
await service.testDbConnection(); | ||
|
||
it('should find Usuario', async () => { | ||
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({ id: 1 } as any); | ||
expect(repository.find).toHaveBeenCalled(); | ||
expect(consoleSpy).toHaveBeenCalledWith(mockUsers); | ||
|
||
const found = await service.findOne(1); | ||
expect(found.id).toEqual(1); | ||
consoleSpy.mockRestore(); | ||
}); | ||
}); | ||
|
||
it('should find Usuario with foto', async () => { | ||
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({ | ||
id: 1, | ||
foto: Buffer.from('/9j/4AAQSkZJRgABAQAAAQABAAD', 'utf-8'), | ||
} as any); | ||
describe('getImageUri transformation', () => { | ||
it('should transform user.foto with getImageUri', () => { | ||
const user = { foto: 'image.png' } as any; | ||
user.foto = getImageUri(user.foto) as unknown as Buffer; | ||
|
||
const found = await service.findOne(1, true); | ||
expect(found.id).toEqual(1); | ||
}); | ||
expect(getImageUri).toHaveBeenCalledWith('image.png'); | ||
expect(user.foto).toBe('http://image.uri/image.png'); | ||
}); | ||
|
||
it('should remove Usuario', async () => { | ||
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({ id: 1 } as any); | ||
jest.spyOn(repository, 'remove').mockReturnValue({ id: 1 } as any); | ||
it('should transform updated.foto with getImageUri', () => { | ||
const updated = { foto: 'image.png' } as any; | ||
updated.foto = getImageUri(updated.foto) as unknown as Buffer & string; | ||
|
||
const removed = await service.remove(1); | ||
expect(removed.id).toEqual(1); | ||
expect(getImageUri).toHaveBeenCalledWith('image.png'); | ||
expect(updated.foto).toBe('http://image.uri/image.png'); | ||
}); | ||
}); | ||
|
||
it('should update Usuario', async () => { | ||
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({ id: 1 } as any); | ||
jest | ||
.spyOn(repository, 'save') | ||
.mockReturnValue({ id: 1, nome: 'Henrique' } as any); | ||
describe('update with password hashing', () => { | ||
it('should hash password if provided', async () => { | ||
const body = { senha: 'senha123' }; | ||
const hashedPassword = 'hashedSenha123'; | ||
jest.spyOn(service, 'hashPassword').mockResolvedValue(hashedPassword); | ||
|
||
const found = await service.update(1, { nome: 'Henrique' }); | ||
expect(found).toEqual({ id: 1, nome: 'Henrique' }); | ||
}); | ||
let newBody = body; | ||
|
||
it('should update Usuario with photo', async () => { | ||
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({ id: 1 } as any); | ||
jest | ||
.spyOn(repository, 'save') | ||
.mockReturnValue({ id: 1, nome: 'Henrique', foto: '1' } as any); | ||
if (body.senha) { | ||
const hashSenha = await service.hashPassword(body.senha); | ||
newBody = { ...body, senha: hashSenha }; | ||
} | ||
|
||
const found = await service.update(1, { nome: 'Henrique' }); | ||
expect(found).toEqual({ | ||
id: 1, | ||
nome: 'Henrique', | ||
foto: 'data:image/png;base64,1', | ||
expect(service.hashPassword).toHaveBeenCalledWith('senha123'); | ||
expect(newBody.senha).toBe(hashedPassword); | ||
}); | ||
}); | ||
|
||
describe('findAll', () => { | ||
const usuario = { | ||
id: 1, | ||
nome: 'Henrique', | ||
email: '[email protected]', | ||
}; | ||
|
||
const order: OrderParams = { | ||
column: 'id', | ||
dir: 'ASC', | ||
}; | ||
const ordering: Ordering = new Ordering(JSON.stringify(order)); | ||
|
||
const paginate: PaginationParams = { | ||
limit: 10, | ||
offset: 0, | ||
}; | ||
const pagination: Pagination = new Pagination(paginate); | ||
|
||
it('should findAll Usuario', async () => { | ||
|
||
it('should get the config value', () => { | ||
const value = configService.get('SOME_CONFIG_KEY'); | ||
expect(value).toBe('10'); | ||
}); | ||
|
||
describe('create', () => { | ||
it('should create a user', async () => { | ||
const user = { nome: 'Henrique', email: '[email protected]', senha: '123456' } as any; | ||
jest.spyOn(repository, 'save').mockResolvedValue(user); | ||
jest.spyOn(repository, 'createQueryBuilder').mockReturnValue({ | ||
where: () => ({ | ||
limit: () => ({ | ||
offset: () => ({ | ||
orderBy: () => ({ | ||
getManyAndCount: jest | ||
.fn() | ||
.mockResolvedValueOnce([[usuario], 1]), | ||
}), | ||
}), | ||
addSelect: () => ({ | ||
getOne: jest.fn().mockResolvedValueOnce(null), | ||
}), | ||
}), | ||
} as any); | ||
|
||
const { data, count } = await service.findAll({}, ordering, pagination); | ||
expect(count).toEqual(1); | ||
expect((data as Usuario[])[0]).toEqual(usuario); | ||
const created = await service.create(user); | ||
expect(created).toEqual(user); | ||
}); | ||
}); | ||
|
||
describe('findAllToPublicacao', () => { | ||
const usuario = { | ||
id: 1, | ||
nome: 'Henrique', | ||
email: '[email protected]', | ||
foto: '1', | ||
}; | ||
|
||
it('should findAllToPublicacao', async () => { | ||
it('should throw error if email already exists', async () => { | ||
jest.spyOn(repository, 'createQueryBuilder').mockReturnValue({ | ||
where: () => ({ | ||
getMany: jest.fn().mockResolvedValueOnce([usuario]), | ||
addSelect: () => ({ | ||
getOne: jest.fn().mockResolvedValueOnce({ email: '[email protected]' }), | ||
}), | ||
}), | ||
} as any); | ||
await expect(service.create({ email: '[email protected]' } as any)).rejects.toThrow( | ||
new BadRequestException('Este email já está cadastrado!'), | ||
); | ||
}); | ||
}); | ||
|
||
describe('findOne', () => { | ||
it('should find a user', async () => { | ||
jest.spyOn(repository, 'findOneOrFail').mockResolvedValue({ id: 1 } as any); | ||
const found = await service.findOne(1); | ||
expect(found.id).toEqual(1); | ||
}); | ||
|
||
it('should throw NotFoundException if user not found', async () => { | ||
jest.spyOn(repository, 'findOneOrFail').mockRejectedValue(new NotFoundException()); | ||
await expect(service.findOne(999)).rejects.toThrow(NotFoundException); | ||
}); | ||
}); | ||
|
||
describe('update', () => { | ||
it('should update a user', async () => { | ||
jest.spyOn(repository, 'findOneOrFail').mockResolvedValue({ id: 1 } as any); | ||
jest.spyOn(repository, 'save').mockResolvedValue({ id: 1, nome: 'Henrique' } as any); | ||
const updated = await service.update(1, { nome: 'Henrique' }); | ||
expect(updated).toEqual({ id: 1, nome: 'Henrique' }); | ||
}); | ||
}); | ||
|
||
describe('remove', () => { | ||
it('should remove a user', async () => { | ||
jest.spyOn(repository, 'findOneOrFail').mockResolvedValue({ id: 1 } as any); | ||
jest.spyOn(repository, 'remove').mockResolvedValue({ id: 1 } as any); | ||
const removed = await service.remove(1); | ||
expect(removed.id).toEqual(1); | ||
}); | ||
}); | ||
|
||
describe('enviarCodigoRedefinicao', () => { | ||
it('should send reset code email', async () => { | ||
const user = { email: '[email protected]', codigoReset: '', codigoResetExpiracao: new Date() }; | ||
jest.spyOn(repository, 'findOne').mockResolvedValue(user as any); | ||
jest.spyOn(repository, 'save').mockResolvedValue(user as any); | ||
(sendResetEmail as jest.Mock).mockResolvedValue(true); | ||
|
||
const expectedUser = { | ||
...usuario, | ||
foto: 'data:image/png;base64,1', | ||
const result = await service.enviarCodigoRedefinicao('[email protected]'); | ||
expect(result).toEqual({ message: 'Código enviado para o e-mail' }); | ||
expect(sendResetEmail).toHaveBeenCalledWith('[email protected]', expect.any(String)); | ||
}); | ||
|
||
it('should throw error if email not provided', async () => { | ||
await expect(service.enviarCodigoRedefinicao('')).rejects.toThrow( | ||
new BadRequestException('Email não forncedio!'), | ||
); | ||
}); | ||
|
||
it('should throw error if user not found', async () => { | ||
jest.spyOn(repository, 'findOne').mockResolvedValue(null); | ||
await expect(service.enviarCodigoRedefinicao('[email protected]')).rejects.toThrow( | ||
new NotFoundException('Usuário não encontrado'), | ||
); | ||
}); | ||
}); | ||
|
||
describe('resetarSenha', () => { | ||
it('should reset password successfully', async () => { | ||
const user = { | ||
email: '[email protected]', | ||
codigoReset: '123456', | ||
codigoResetExpiracao: new Date(Date.now() + 3600000), | ||
}; | ||
jest.spyOn(repository, 'findOne').mockResolvedValue(user as any); | ||
jest.spyOn(repository, 'save').mockResolvedValue(user as any); | ||
|
||
const data = await service.findAllToPublicacao([1]); | ||
expect(data).toEqual([expectedUser]); | ||
const dto: ResetarSenhaDto = { | ||
email: '[email protected]', | ||
codigo: '123456', | ||
novaSenha: 'newpassword', | ||
}; | ||
|
||
const result = await service.resetarSenha(dto); | ||
expect(result).toEqual({ message: 'Senha redefinida com sucesso' }); | ||
}); | ||
|
||
it('should throw error if code is invalid or expired', async () => { | ||
const user = { | ||
email: '[email protected]', | ||
codigoReset: '123456', | ||
codigoResetExpiracao: new Date(Date.now() - 3600000), // Expirado | ||
}; | ||
jest.spyOn(repository, 'findOne').mockResolvedValue(user as any); | ||
|
||
const dto: ResetarSenhaDto = { | ||
email: '[email protected]', | ||
codigo: '654321', | ||
novaSenha: 'newpassword', | ||
}; | ||
|
||
await expect(service.resetarSenha(dto)).rejects.toThrow( | ||
new NotFoundException('Código inválido ou expirado'), | ||
); | ||
}); | ||
|
||
it('should throw error if user not found', async () => { | ||
jest.spyOn(repository, 'findOne').mockResolvedValue(null); | ||
|
||
const dto: ResetarSenhaDto = { | ||
email: '[email protected]', | ||
codigo: '123456', | ||
novaSenha: 'newpassword', | ||
}; | ||
|
||
await expect(service.resetarSenha(dto)).rejects.toThrow( | ||
new NotFoundException('Usuário não encontrado'), | ||
); | ||
}); | ||
|
||
}); | ||
|
||
describe('allUpdatedUsuariosSince', () => { | ||
it('should get all updated users since timestamp', async () => { | ||
const timestamp = new Date(); | ||
jest.spyOn(repository, 'createQueryBuilder').mockReturnValue({ | ||
where: jest.fn().mockReturnThis(), | ||
getMany: jest.fn().mockResolvedValue([{ id: 1 }]), | ||
} as any); | ||
const users = await service.allUpdatedUsuariosSince(timestamp); | ||
expect(users).toEqual([{ id: 1 }]); | ||
}); | ||
}); | ||
|
||
describe('allCreatedUsuariosSince', () => { | ||
it('should get all created users since timestamp', async () => { | ||
const timestamp = new Date(); | ||
jest.spyOn(repository, 'createQueryBuilder').mockReturnValue({ | ||
where: jest.fn().mockReturnThis(), | ||
getMany: jest.fn().mockResolvedValue([{ id: 1 }]), | ||
} as any); | ||
const users = await service.allCreatedUsuariosSince(timestamp); | ||
expect(users).toEqual([{ id: 1 }]); | ||
}); | ||
}); | ||
}); |