From 1d2e7da67347864254776a8145005673e5019ee1 Mon Sep 17 00:00:00 2001 From: Gsk54 Date: Wed, 18 Jan 2023 10:47:20 +0100 Subject: [PATCH] update tests --- opa/user/{user2.rego => list.rego} | 0 opa/user/{user1.rego => read.rego} | 0 .../unit/update-username.usecase.spec.ts | 77 ++++++++++--------- .../secondaries/opa.decision-maker.ts | 11 +-- .../authorization/domain/dtos/context-item.ts | 9 +++ .../domain/dtos/decision.request.ts | 3 +- .../authorization/queries/decision.query.ts | 5 +- .../tests/unit/decision.usecase.spec.ts | 9 +-- 8 files changed, 62 insertions(+), 52 deletions(-) rename opa/user/{user2.rego => list.rego} (100%) rename opa/user/{user1.rego => read.rego} (100%) create mode 100644 src/modules/authorization/domain/dtos/context-item.ts diff --git a/opa/user/user2.rego b/opa/user/list.rego similarity index 100% rename from opa/user/user2.rego rename to opa/user/list.rego diff --git a/opa/user/user1.rego b/opa/user/read.rego similarity index 100% rename from opa/user/user1.rego rename to opa/user/read.rego diff --git a/src/modules/authentication/tests/unit/update-username.usecase.spec.ts b/src/modules/authentication/tests/unit/update-username.usecase.spec.ts index 2fcc95b..084a6b6 100644 --- a/src/modules/authentication/tests/unit/update-username.usecase.spec.ts +++ b/src/modules/authentication/tests/unit/update-username.usecase.spec.ts @@ -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), diff --git a/src/modules/authorization/adapters/secondaries/opa.decision-maker.ts b/src/modules/authorization/adapters/secondaries/opa.decision-maker.ts index 73d93c0..f4c89e4 100644 --- a/src/modules/authorization/adapters/secondaries/opa.decision-maker.ts +++ b/src/modules/authorization/adapters/secondaries/opa.decision-maker.ts @@ -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, ): Promise { const { data } = await lastValueFrom( - this.httpService.post( - this.configService.get('OPA_URL') + domain + '/' + action, + this._httpService.post( + this._configService.get('OPA_URL') + domain + '/' + action, { input: { uuid, diff --git a/src/modules/authorization/domain/dtos/context-item.ts b/src/modules/authorization/domain/dtos/context-item.ts new file mode 100644 index 0000000..b9b95dc --- /dev/null +++ b/src/modules/authorization/domain/dtos/context-item.ts @@ -0,0 +1,9 @@ +export class ContextItem { + name: string; + value: any; + + constructor(name: string, value: any) { + this.name = name; + this.value = value; + } +} diff --git a/src/modules/authorization/domain/dtos/decision.request.ts b/src/modules/authorization/domain/dtos/decision.request.ts index 16633a6..2dfa6ea 100644 --- a/src/modules/authorization/domain/dtos/decision.request.ts +++ b/src/modules/authorization/domain/dtos/decision.request.ts @@ -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; } diff --git a/src/modules/authorization/queries/decision.query.ts b/src/modules/authorization/queries/decision.query.ts index 58cc1f3..7110acf 100644 --- a/src/modules/authorization/queries/decision.query.ts +++ b/src/modules/authorization/queries/decision.query.ts @@ -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; constructor( uuid: string, domain: Domain, action: Action, - context?: Array<{ name: string; value: string }>, + context?: Array, ) { this.uuid = uuid; this.domain = domain; diff --git a/src/modules/authorization/tests/unit/decision.usecase.spec.ts b/src/modules/authorization/tests/unit/decision.usecase.spec.ts index 3d3a022..082d283 100644 --- a/src/modules/authorization/tests/unit/decision.usecase.spec.ts +++ b/src/modules/authorization/tests/unit/decision.usecase.spec.ts @@ -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(