update tests
This commit is contained in:
parent
972d43ac30
commit
1d2e7da673
|
@ -18,37 +18,37 @@ const existingUsername = {
|
|||
type: Type.EMAIL,
|
||||
};
|
||||
|
||||
const newUsernameRequest: UpdateUsernameRequest = new UpdateUsernameRequest();
|
||||
newUsernameRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a90';
|
||||
newUsernameRequest.username = '+33611223344';
|
||||
newUsernameRequest.type = Type.PHONE;
|
||||
|
||||
const updateUsernameRequest: UpdateUsernameRequest =
|
||||
new UpdateUsernameRequest();
|
||||
updateUsernameRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91';
|
||||
updateUsernameRequest.username = 'johnny.doe@email.com';
|
||||
updateUsernameRequest.type = Type.EMAIL;
|
||||
|
||||
const newUsernameRequest: UpdateUsernameRequest = new UpdateUsernameRequest();
|
||||
newUsernameRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91';
|
||||
newUsernameRequest.username = '+33611223344';
|
||||
newUsernameRequest.type = Type.PHONE;
|
||||
|
||||
const invalidUpdateUsernameRequest: UpdateUsernameRequest =
|
||||
new UpdateUsernameRequest();
|
||||
invalidUpdateUsernameRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91';
|
||||
invalidUpdateUsernameRequest.username = '';
|
||||
invalidUpdateUsernameRequest.type = Type.EMAIL;
|
||||
|
||||
const unknownUsernameRequest: UpdateUsernameRequest =
|
||||
new UpdateUsernameRequest();
|
||||
unknownUsernameRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91';
|
||||
unknownUsernameRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a92';
|
||||
unknownUsernameRequest.username = 'unknown@email.com';
|
||||
unknownUsernameRequest.type = Type.EMAIL;
|
||||
|
||||
const updateUsernameCommand: UpdateUsernameCommand = new UpdateUsernameCommand(
|
||||
updateUsernameRequest,
|
||||
);
|
||||
const invalidUpdateUsernameRequest: UpdateUsernameRequest =
|
||||
new UpdateUsernameRequest();
|
||||
invalidUpdateUsernameRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a93';
|
||||
invalidUpdateUsernameRequest.username = '';
|
||||
invalidUpdateUsernameRequest.type = Type.EMAIL;
|
||||
|
||||
const newUsernameCommand: UpdateUsernameCommand = new UpdateUsernameCommand(
|
||||
newUsernameRequest,
|
||||
);
|
||||
|
||||
const updateUsernameCommand: UpdateUsernameCommand = new UpdateUsernameCommand(
|
||||
updateUsernameRequest,
|
||||
);
|
||||
|
||||
const invalidUpdateUsernameCommand: UpdateUsernameCommand =
|
||||
new UpdateUsernameCommand(invalidUpdateUsernameRequest);
|
||||
|
||||
|
@ -56,21 +56,24 @@ const unknownUpdateUsernameCommand: UpdateUsernameCommand =
|
|||
new UpdateUsernameCommand(unknownUsernameRequest);
|
||||
|
||||
const mockUsernameRepository = {
|
||||
findOne: jest.fn().mockResolvedValue(existingUsername),
|
||||
updateWhere: jest
|
||||
.fn()
|
||||
.mockImplementationOnce(() => {
|
||||
return Promise.resolve(updateUsernameRequest);
|
||||
})
|
||||
.mockImplementationOnce(() => {
|
||||
findOne: jest.fn().mockImplementation((request) => {
|
||||
if (request.uuid == 'bb281075-1b98-4456-89d6-c643d3044a90') {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
return Promise.resolve(existingUsername);
|
||||
}),
|
||||
updateWhere: jest.fn().mockImplementation((request) => {
|
||||
if (request.uuid_type.uuid == 'bb281075-1b98-4456-89d6-c643d3044a90') {
|
||||
return Promise.resolve(newUsernameRequest);
|
||||
})
|
||||
.mockImplementationOnce(() => {
|
||||
}
|
||||
if (request.uuid_type.uuid == 'bb281075-1b98-4456-89d6-c643d3044a91') {
|
||||
return Promise.resolve(updateUsernameRequest);
|
||||
}
|
||||
if (request.uuid_type.uuid == 'bb281075-1b98-4456-89d6-c643d3044a92') {
|
||||
throw new Error('Error');
|
||||
})
|
||||
.mockImplementationOnce(() => {
|
||||
return Promise.resolve(invalidUpdateUsernameRequest);
|
||||
}),
|
||||
}
|
||||
return Promise.resolve(invalidUpdateUsernameRequest);
|
||||
}),
|
||||
};
|
||||
|
||||
const mockAddUsernameCommand = {
|
||||
|
@ -115,15 +118,6 @@ describe('UpdateUsernameUseCase', () => {
|
|||
});
|
||||
|
||||
describe('execute', () => {
|
||||
it('should update a username for email type', async () => {
|
||||
const updatedUsername: Username = await updateUsernameUseCase.execute(
|
||||
updateUsernameCommand,
|
||||
);
|
||||
|
||||
expect(updatedUsername.username).toBe(updateUsernameRequest.username);
|
||||
expect(updatedUsername.type).toBe(updateUsernameRequest.type);
|
||||
});
|
||||
|
||||
it('should create a new username', async () => {
|
||||
const newUsername: Username = await updateUsernameUseCase.execute(
|
||||
newUsernameCommand,
|
||||
|
@ -133,6 +127,15 @@ describe('UpdateUsernameUseCase', () => {
|
|||
expect(newUsername.type).toBe(newUsernameRequest.type);
|
||||
});
|
||||
|
||||
it('should update a username for email type', async () => {
|
||||
const updatedUsername: Username = await updateUsernameUseCase.execute(
|
||||
updateUsernameCommand,
|
||||
);
|
||||
|
||||
expect(updatedUsername.username).toBe(updateUsernameRequest.username);
|
||||
expect(updatedUsername.type).toBe(updateUsernameRequest.type);
|
||||
});
|
||||
|
||||
it('should throw an error if username does not exist', async () => {
|
||||
await expect(
|
||||
updateUsernameUseCase.execute(unknownUpdateUsernameCommand),
|
||||
|
|
|
@ -5,13 +5,14 @@ import { lastValueFrom } from 'rxjs';
|
|||
import { Action } from '../../domain/dtos/action.enum';
|
||||
import { Domain } from '../../domain/dtos/domain.enum';
|
||||
import { IMakeDecision } from '../../domain/interfaces/decision-maker';
|
||||
import { ContextItem } from '../../domain/dtos/context-item';
|
||||
import { Decision } from './decision';
|
||||
|
||||
@Injectable()
|
||||
export class OpaDecisionMaker extends IMakeDecision {
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly httpService: HttpService,
|
||||
private readonly _configService: ConfigService,
|
||||
private readonly _httpService: HttpService,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
@ -20,11 +21,11 @@ export class OpaDecisionMaker extends IMakeDecision {
|
|||
uuid: string,
|
||||
domain: Domain,
|
||||
action: Action,
|
||||
context: Array<{ name: string; value: string }>,
|
||||
context: Array<ContextItem>,
|
||||
): Promise<boolean> {
|
||||
const { data } = await lastValueFrom(
|
||||
this.httpService.post<Decision>(
|
||||
this.configService.get<string>('OPA_URL') + domain + '/' + action,
|
||||
this._httpService.post<Decision>(
|
||||
this._configService.get<string>('OPA_URL') + domain + '/' + action,
|
||||
{
|
||||
input: {
|
||||
uuid,
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
export class ContextItem {
|
||||
name: string;
|
||||
value: any;
|
||||
|
||||
constructor(name: string, value: any) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
import { IsArray, IsNotEmpty, IsString } from 'class-validator';
|
||||
import { ContextItem } from './context-item';
|
||||
import { Action } from './action.enum';
|
||||
import { Domain } from './domain.enum';
|
||||
|
||||
|
@ -16,5 +17,5 @@ export class DecisionRequest {
|
|||
action: Action;
|
||||
|
||||
@IsArray()
|
||||
context?: Array<{ name: string; value: string }>;
|
||||
context?: Array<ContextItem>;
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { ContextItem } from '../domain/dtos/context-item';
|
||||
import { Action } from '../domain/dtos/action.enum';
|
||||
import { Domain } from '../domain/dtos/domain.enum';
|
||||
|
||||
|
@ -5,13 +6,13 @@ export class DecisionQuery {
|
|||
readonly uuid: string;
|
||||
readonly domain: Domain;
|
||||
readonly action: Action;
|
||||
readonly context: Array<{ name: string; value: string }>;
|
||||
readonly context: Array<ContextItem>;
|
||||
|
||||
constructor(
|
||||
uuid: string,
|
||||
domain: Domain,
|
||||
action: Action,
|
||||
context?: Array<{ name: string; value: string }>,
|
||||
context?: Array<ContextItem>,
|
||||
) {
|
||||
this.uuid = uuid;
|
||||
this.domain = domain;
|
||||
|
|
|
@ -3,6 +3,7 @@ import { AutomapperModule } from '@automapper/nestjs';
|
|||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { OpaDecisionMaker } from '../../adapters/secondaries/opa.decision-maker';
|
||||
import { Action } from '../../domain/dtos/action.enum';
|
||||
import { ContextItem } from '../../domain/dtos/context-item';
|
||||
import { DecisionRequest } from '../../domain/dtos/decision.request';
|
||||
import { Domain } from '../../domain/dtos/domain.enum';
|
||||
import { DecisionUseCase } from '../../domain/usecases/decision.usecase';
|
||||
|
@ -40,13 +41,7 @@ describe('DecisionUseCase', () => {
|
|||
decisionRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91';
|
||||
decisionRequest.domain = Domain.user;
|
||||
decisionRequest.action = Action.create;
|
||||
decisionRequest.context = [
|
||||
{
|
||||
name: 'context1',
|
||||
value: 'value1',
|
||||
},
|
||||
];
|
||||
|
||||
decisionRequest.context = [new ContextItem('context1', 'value1')];
|
||||
expect(
|
||||
decisionUseCase.execute(
|
||||
new DecisionQuery(
|
||||
|
|
Loading…
Reference in New Issue