auth/src/modules/authentication/tests/unit/create-authentication.useca...

100 lines
3.3 KiB
TypeScript

import { classes } from '@automapper/classes';
import { AutomapperModule } from '@automapper/nestjs';
import { Test, TestingModule } from '@nestjs/testing';
import { AuthenticationRepository } from '../../adapters/secondaries/authentication.repository';
import { CreateAuthenticationCommand } from '../../commands/create-authentication.command';
import { CreateAuthenticationRequest } from '../../domain/dtos/create-authentication.request';
import { Authentication } from '../../domain/entities/authentication';
import { CreateAuthenticationUseCase } from '../../domain/usecases/create-authentication.usecase';
import * as bcrypt from 'bcrypt';
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
import { Type } from '../../domain/dtos/type.enum';
import { Messager } from '../../adapters/secondaries/messager';
const newAuthenticationRequest: CreateAuthenticationRequest =
new CreateAuthenticationRequest();
newAuthenticationRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91';
newAuthenticationRequest.username = 'john.doe@email.com';
newAuthenticationRequest.password = 'John123';
newAuthenticationRequest.type = Type.EMAIL;
const newAuthCommand: CreateAuthenticationCommand =
new CreateAuthenticationCommand(newAuthenticationRequest);
const mockAuthenticationRepository = {
create: jest
.fn()
.mockImplementationOnce(() => {
return Promise.resolve({
uuid: newAuthenticationRequest.uuid,
password: bcrypt.hashSync(newAuthenticationRequest.password, 10),
});
})
.mockImplementation(() => {
throw new Error('Already exists');
}),
};
const mockUsernameRepository = {
create: jest.fn().mockResolvedValue({
uuid: newAuthenticationRequest.uuid,
username: newAuthenticationRequest.username,
type: newAuthenticationRequest.type,
}),
};
const mockMessager = {
publish: jest.fn().mockImplementation(),
};
describe('CreateAuthenticationUseCase', () => {
let createAuthenticationUseCase: CreateAuthenticationUseCase;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AutomapperModule.forRoot({ strategyInitializer: classes() })],
providers: [
{
provide: AuthenticationRepository,
useValue: mockAuthenticationRepository,
},
{
provide: UsernameRepository,
useValue: mockUsernameRepository,
},
{
provide: Messager,
useValue: mockMessager,
},
CreateAuthenticationUseCase,
],
}).compile();
createAuthenticationUseCase = module.get<CreateAuthenticationUseCase>(
CreateAuthenticationUseCase,
);
});
it('should be defined', () => {
expect(createAuthenticationUseCase).toBeDefined();
});
describe('execute', () => {
it('should create an authentication with an encrypted password', async () => {
const newAuthentication: Authentication =
await createAuthenticationUseCase.execute(newAuthCommand);
expect(
bcrypt.compareSync(
newAuthenticationRequest.password,
newAuthentication.password,
),
).toBeTruthy();
});
it('should throw an error if user already exists', async () => {
await expect(
createAuthenticationUseCase.execute(newAuthCommand),
).rejects.toBeInstanceOf(Error);
});
});
});