add validation tests
This commit is contained in:
parent
03d718036d
commit
9583937622
|
@ -10,7 +10,7 @@ import { CreateAuthRequest } from '../../domain/dto/create-auth.request';
|
||||||
import { UpdateAuthRequest } from '../../domain/dto/update-auth.request';
|
import { UpdateAuthRequest } from '../../domain/dto/update-auth.request';
|
||||||
import { ValidateAuthRequest } from '../../domain/dto/validate-auth.request';
|
import { ValidateAuthRequest } from '../../domain/dto/validate-auth.request';
|
||||||
import { Auth } from '../../domain/entities/auth';
|
import { Auth } from '../../domain/entities/auth';
|
||||||
import { ValidateQuery } from '../../queries/validate.query';
|
import { ValidateAuthQuery } from '../../queries/validate-auth.query';
|
||||||
import { AuthPresenter } from './auth.presenter';
|
import { AuthPresenter } from './auth.presenter';
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
|
@ -25,7 +25,7 @@ export class AuthController {
|
||||||
async validate(data: ValidateAuthRequest): Promise<AuthPresenter> {
|
async validate(data: ValidateAuthRequest): Promise<AuthPresenter> {
|
||||||
try {
|
try {
|
||||||
const auth = await this._queryBus.execute(
|
const auth = await this._queryBus.execute(
|
||||||
new ValidateQuery(data.username, data.password),
|
new ValidateAuthQuery(data.username, data.password),
|
||||||
);
|
);
|
||||||
return this._mapper.map(auth, Auth, AuthPresenter);
|
return this._mapper.map(auth, Auth, AuthPresenter);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { CqrsModule } from '@nestjs/cqrs';
|
||||||
import { DatabaseModule } from '../database/database.module';
|
import { DatabaseModule } from '../database/database.module';
|
||||||
import { AuthController } from './adapters/primaries/auth.controller';
|
import { AuthController } from './adapters/primaries/auth.controller';
|
||||||
import { CreateAuthUseCase } from './domain/usecases/create-auth.usecase';
|
import { CreateAuthUseCase } from './domain/usecases/create-auth.usecase';
|
||||||
import { ValidateUseCase } from './domain/usecases/validate-auth.usecase';
|
import { ValidateAuthUseCase } from './domain/usecases/validate-auth.usecase';
|
||||||
import { AuthProfile } from './mappers/auth.profile';
|
import { AuthProfile } from './mappers/auth.profile';
|
||||||
import { AuthRepository } from './adapters/secondaries/auth.repository';
|
import { AuthRepository } from './adapters/secondaries/auth.repository';
|
||||||
import { UpdateAuthUseCase } from './domain/usecases/update-auth.usecase';
|
import { UpdateAuthUseCase } from './domain/usecases/update-auth.usecase';
|
||||||
|
@ -14,7 +14,7 @@ import { UpdateAuthUseCase } from './domain/usecases/update-auth.usecase';
|
||||||
providers: [
|
providers: [
|
||||||
AuthProfile,
|
AuthProfile,
|
||||||
AuthRepository,
|
AuthRepository,
|
||||||
ValidateUseCase,
|
ValidateAuthUseCase,
|
||||||
CreateAuthUseCase,
|
CreateAuthUseCase,
|
||||||
UpdateAuthUseCase,
|
UpdateAuthUseCase,
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
import { QueryHandler } from '@nestjs/cqrs';
|
import { QueryHandler } from '@nestjs/cqrs';
|
||||||
import { AuthRepository } from '../../adapters/secondaries/auth.repository';
|
import { AuthRepository } from '../../adapters/secondaries/auth.repository';
|
||||||
import { ValidateQuery } from '../../queries/validate.query';
|
import { ValidateAuthQuery } from '../../queries/validate-auth.query';
|
||||||
import { Auth } from '../entities/auth';
|
import { Auth } from '../entities/auth';
|
||||||
import * as bcrypt from 'bcrypt';
|
import * as bcrypt from 'bcrypt';
|
||||||
import { NotFoundException, UnauthorizedException } from '@nestjs/common';
|
import { NotFoundException, UnauthorizedException } from '@nestjs/common';
|
||||||
|
|
||||||
@QueryHandler(ValidateQuery)
|
@QueryHandler(ValidateAuthQuery)
|
||||||
export class ValidateUseCase {
|
export class ValidateAuthUseCase {
|
||||||
constructor(private readonly _authRepository: AuthRepository) {}
|
constructor(private readonly _authRepository: AuthRepository) {}
|
||||||
|
|
||||||
async execute(validate: ValidateQuery): Promise<Auth> {
|
async execute(validate: ValidateAuthQuery): Promise<Auth> {
|
||||||
const auth = await this._authRepository.findOne({
|
const auth = await this._authRepository.findOne({
|
||||||
username: validate.username,
|
username: validate.username,
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
export class ValidateQuery {
|
export class ValidateAuthQuery {
|
||||||
readonly username: string;
|
readonly username: string;
|
||||||
readonly password: string;
|
readonly password: string;
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
import { classes } from '@automapper/classes';
|
||||||
|
import { AutomapperModule } from '@automapper/nestjs';
|
||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { AuthRepository } from '../../adapters/secondaries/auth.repository';
|
||||||
|
import { Auth } from '../../domain/entities/auth';
|
||||||
|
import * as bcrypt from 'bcrypt';
|
||||||
|
import { ValidateAuthUseCase } from '../../domain/usecases/validate-auth.usecase';
|
||||||
|
import { AuthProfile } from '../../mappers/auth.profile';
|
||||||
|
import { ValidateAuthQuery } from '../../queries/validate-auth.query';
|
||||||
|
|
||||||
|
const mockAuthRepository = {
|
||||||
|
findOne: jest.fn().mockResolvedValue({
|
||||||
|
uuid: 'bb281075-1b98-4456-89d6-c643d3044a91',
|
||||||
|
username: 'john.doe@email.com',
|
||||||
|
password: bcrypt.hashSync('John123', 10),
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('ValidateAuthUseCase', () => {
|
||||||
|
let validateAuthUseCase: ValidateAuthUseCase;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
imports: [AutomapperModule.forRoot({ strategyInitializer: classes() })],
|
||||||
|
providers: [
|
||||||
|
{
|
||||||
|
provide: AuthRepository,
|
||||||
|
useValue: mockAuthRepository,
|
||||||
|
},
|
||||||
|
ValidateAuthUseCase,
|
||||||
|
AuthProfile,
|
||||||
|
],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
validateAuthUseCase = module.get<ValidateAuthUseCase>(ValidateAuthUseCase);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(validateAuthUseCase).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('execute', () => {
|
||||||
|
it('should validate an auth and returns entity object', async () => {
|
||||||
|
const auth: Auth = await validateAuthUseCase.execute(
|
||||||
|
new ValidateAuthQuery('john.doe@email.com', 'John123'),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(auth.username).toBe('john.doe@email.com');
|
||||||
|
expect(bcrypt.compareSync('John123', auth.password)).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue