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