From 6ca363a853f728fc71bbd949b86178aaaae9b039 Mon Sep 17 00:00:00 2001 From: hannah-macdonald1 Date: Wed, 6 Nov 2024 15:46:24 -0700 Subject: [PATCH 1/3] Use reflection to determine path. Change id to rowId. --- src/common/constants/parameter-constants.ts | 3 ++- src/common/constants/upstream-constants.ts | 2 ++ src/common/guards/auth/auth.guard.spec.ts | 6 +++++ src/common/guards/auth/auth.guard.ts | 4 ++- src/common/guards/auth/auth.service.spec.ts | 20 +++++++++++--- src/common/guards/auth/auth.service.ts | 26 ++++++++++++++++--- .../cases/cases.controller.spec.ts | 5 ++-- src/controllers/cases/cases.controller.ts | 9 ++++--- src/controllers/cases/cases.service.spec.ts | 5 ++-- .../incidents/incidents.controller.spec.ts | 3 ++- .../incidents/incidents.controller.ts | 7 +++-- .../incidents/incidents.service.spec.ts | 3 ++- .../service-requests.controller.spec.ts | 3 ++- .../service-requests.controller.ts | 7 +++-- .../service-requests.service.spec.ts | 3 ++- src/dto/id-path-params.dto.ts | 4 +-- .../in-person-visits.service.spec.ts | 7 ++--- .../in-person-visits.service.ts | 3 ++- .../support-network.service.spec.ts | 7 ++--- .../support-network.service.ts | 3 ++- 20 files changed, 95 insertions(+), 35 deletions(-) diff --git a/src/common/constants/parameter-constants.ts b/src/common/constants/parameter-constants.ts index f60067b..733bf52 100644 --- a/src/common/constants/parameter-constants.ts +++ b/src/common/constants/parameter-constants.ts @@ -4,5 +4,6 @@ const CONTENT_TYPE = 'application/json'; const PAGINATION = 'N'; const idRegex = /[0-9\-A-Za-z]+/; +const idName = 'rowId'; -export { VIEW_MODE, CHILD_LINKS, CONTENT_TYPE, PAGINATION, idRegex }; +export { VIEW_MODE, CHILD_LINKS, CONTENT_TYPE, PAGINATION, idRegex, idName }; diff --git a/src/common/constants/upstream-constants.ts b/src/common/constants/upstream-constants.ts index fa6b755..ae3deee 100644 --- a/src/common/constants/upstream-constants.ts +++ b/src/common/constants/upstream-constants.ts @@ -1,9 +1,11 @@ const baseUrlEnvVarName = 'UPSTREAM_BASE_URL'; const supportNetworkEndpointEnvVarName = 'SUPPORT_NETWORK_ENDPOINT'; const inPersonVisitsEndpointEnvVarName = 'IN_PERSON_VISITS_ENDPOINT'; +const idirUsernameHeaderField = 'x-idir-username'; export { baseUrlEnvVarName, supportNetworkEndpointEnvVarName, inPersonVisitsEndpointEnvVarName, + idirUsernameHeaderField, }; diff --git a/src/common/guards/auth/auth.guard.spec.ts b/src/common/guards/auth/auth.guard.spec.ts index 6b04a80..aadd839 100644 --- a/src/common/guards/auth/auth.guard.spec.ts +++ b/src/common/guards/auth/auth.guard.spec.ts @@ -78,6 +78,9 @@ describe('AuthGuard', () => { switchToHttp: () => ({ getRequest: () => getMockReq(), }), + getClass: () => { + return TestController; + }, }; const isAuthed = await guard.canActivate(execContext as ExecutionContext); expect(authSpy).toHaveBeenCalledTimes(0); @@ -123,6 +126,9 @@ describe('AuthGuard', () => { switchToHttp: () => ({ getRequest: () => getMockReq(), }), + getClass: () => { + return TestController; + }, }; const isAuthed = await guard.canActivate(execContext); expect(authSpy).toHaveBeenCalledTimes(1); diff --git a/src/common/guards/auth/auth.guard.ts b/src/common/guards/auth/auth.guard.ts index 2dbe42b..52791f7 100644 --- a/src/common/guards/auth/auth.guard.ts +++ b/src/common/guards/auth/auth.guard.ts @@ -18,6 +18,8 @@ export class AuthGuard implements CanActivate { return true; } const request = context.switchToHttp().getRequest(); - return await this.authService.getRecordAndValidate(request); + const controllerPath = + Reflect.getMetadata('path', context.getClass()) || ''; + return await this.authService.getRecordAndValidate(request, controllerPath); } } diff --git a/src/common/guards/auth/auth.service.spec.ts b/src/common/guards/auth/auth.service.spec.ts index 8190e7e..4b99b1f 100644 --- a/src/common/guards/auth/auth.service.spec.ts +++ b/src/common/guards/auth/auth.service.spec.ts @@ -16,6 +16,8 @@ import { RecordType } from '../../../common/constants/enumerations'; import { EnumTypeError } from '../../../common/errors/errors'; import { UtilitiesService } from '../../../helpers/utilities/utilities.service'; import { TokenRefresherService } from '../../../external-api/token-refresher/token-refresher.service'; +import { idirUsernameHeaderField } from '../../../common/constants/upstream-constants'; +import { idName } from '../../../common/constants/parameter-constants'; describe('AuthService', () => { let service: AuthService; @@ -96,12 +98,17 @@ describe('AuthService', () => { path: validPath, header: jest.fn((key: string): string => { const headerVal: { [key: string]: string } = { - 'x-idir-username': testIdir, + [idirUsernameHeaderField]: testIdir, }; return headerVal[key]; }), + params: { [idName]: 'id' }, }); - const isAuthed = await service.getRecordAndValidate(mockRequest); + const controllerPath = 'case'; + const isAuthed = await service.getRecordAndValidate( + mockRequest, + controllerPath, + ); expect(spy).toHaveBeenCalledTimes(1); expect(cacheSpy).toHaveBeenCalledTimes(2); expect(isAuthed).toBe(true); @@ -109,7 +116,7 @@ describe('AuthService', () => { it.each([ [{}, undefined, 0], - [{ 'x-idir-username': testIdir }, null, 1], + [{ [idirUsernameHeaderField]: testIdir }, null, 1], ])( 'should return false with invalid record', async (headers, cacheReturn, cacheSpyCallTimes) => { @@ -122,8 +129,13 @@ describe('AuthService', () => { const headerVal: { [key: string]: string } = headers; return headerVal[key]; }), + params: { [idName]: 'id' }, }); - const isAuthed = await service.getRecordAndValidate(mockRequest); + const controllerPath = 'case'; + const isAuthed = await service.getRecordAndValidate( + mockRequest, + controllerPath, + ); expect(cacheSpy).toHaveBeenCalledTimes(cacheSpyCallTimes); expect(isAuthed).toBe(false); }, diff --git a/src/common/guards/auth/auth.service.ts b/src/common/guards/auth/auth.service.ts index bd3109f..2613565 100644 --- a/src/common/guards/auth/auth.service.ts +++ b/src/common/guards/auth/auth.service.ts @@ -10,12 +10,16 @@ import { UtilitiesService } from '../../../helpers/utilities/utilities.service'; import { CHILD_LINKS, CONTENT_TYPE, + idName, VIEW_MODE, } from '../../../common/constants/parameter-constants'; import { firstValueFrom } from 'rxjs'; import { AxiosError } from 'axios'; import { TokenRefresherService } from '../../../external-api/token-refresher/token-refresher.service'; -import { baseUrlEnvVarName } from '../../../common/constants/upstream-constants'; +import { + baseUrlEnvVarName, + idirUsernameHeaderField, +} from '../../../common/constants/upstream-constants'; @Injectable() export class AuthService { @@ -36,11 +40,14 @@ export class AuthService { this.buildNumber = this.configService.get('buildInfo.buildNumber'); } - async getRecordAndValidate(req: Request): Promise { + async getRecordAndValidate( + req: Request, + controllerPath: string, + ): Promise { let idir: string, id: string, recordType: RecordType; try { - idir = req.header('x-idir-username').trim(); - [id, recordType] = this.grabRecordInfoFromPath(req.path); + idir = req.header(idirUsernameHeaderField).trim(); + [id, recordType] = this.grabRecordInfo(req, controllerPath); } catch (error: any) { this.logger.error({ error }); return false; @@ -70,6 +77,17 @@ export class AuthService { return [pathParts[1].trim(), pathParts[0].trim() as RecordType]; } + grabRecordInfo(req: Request, controllerPath: string): [string, RecordType] { + if (!this.utilitiesService.enumTypeGuard(RecordType, controllerPath)) { + throw new EnumTypeError(controllerPath); + } + const rowId = req.params[idName]; + if (rowId === undefined) { + throw new Error(`Id not found in path`); + } + return [rowId, controllerPath as RecordType]; + } + async getAssignedIdirUpstream( id: string, recordType: RecordType, diff --git a/src/controllers/cases/cases.controller.spec.ts b/src/controllers/cases/cases.controller.spec.ts index c1e8661..f02d5e7 100644 --- a/src/controllers/cases/cases.controller.spec.ts +++ b/src/controllers/cases/cases.controller.spec.ts @@ -20,6 +20,7 @@ import { InPersonVisitsEntity, InPersonVisitsSingleResponseCaseExample, } from '../../entities/in-person-visits.entity'; +import { idName } from '../../common/constants/parameter-constants'; describe('CasesController', () => { let controller: CasesController; @@ -55,7 +56,7 @@ describe('CasesController', () => { it.each([ [ SupportNetworkSingleResponseCaseExample, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, { since: '2020-02-02' } as SinceQueryParams, ], ])( @@ -83,7 +84,7 @@ describe('CasesController', () => { it.each([ [ InPersonVisitsSingleResponseCaseExample, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, { since: '2020-02-02' } as SinceQueryParams, ], ])( diff --git a/src/controllers/cases/cases.controller.ts b/src/controllers/cases/cases.controller.ts index 1a325ef..e1d7c32 100644 --- a/src/controllers/cases/cases.controller.ts +++ b/src/controllers/cases/cases.controller.ts @@ -27,7 +27,10 @@ import { import { IdPathParams } from '../../dto/id-path-params.dto'; import { SinceQueryParams } from '../../dto/since-query-params.dto'; import { ApiNotFoundEntity } from '../../entities/api-not-found.entity'; -import { CONTENT_TYPE } from '../../common/constants/parameter-constants'; +import { + CONTENT_TYPE, + idName, +} from '../../common/constants/parameter-constants'; import { ApiInternalServerErrorEntity } from '../../entities/api-internal-server-error.entity'; import { AuthGuard } from '../../common/guards/auth/auth.guard'; import { @@ -45,7 +48,7 @@ export class CasesController { constructor(private readonly casesService: CasesService) {} @UseInterceptors(ClassSerializerInterceptor) - @Get(':id/support-network') + @Get(`:${idName}/support-network`) @ApiOperation({ description: 'Find all Support Network entries related to a given Case entity by Case id.', @@ -98,7 +101,7 @@ export class CasesController { } @UseInterceptors(ClassSerializerInterceptor) - @Get(':id/visits') + @Get(`:${idName}/visits`) @ApiOperation({ description: 'Find all In Person Child / Youth Visits related to a given Case entity by Case id.', diff --git a/src/controllers/cases/cases.service.spec.ts b/src/controllers/cases/cases.service.spec.ts index bda6a6f..748fd52 100644 --- a/src/controllers/cases/cases.service.spec.ts +++ b/src/controllers/cases/cases.service.spec.ts @@ -19,6 +19,7 @@ import { InPersonVisitsEntity, InPersonVisitsSingleResponseCaseExample, } from '../../entities/in-person-visits.entity'; +import { idName } from '../../common/constants/parameter-constants'; describe('CasesService', () => { let service: CasesService; @@ -62,7 +63,7 @@ describe('CasesService', () => { it.each([ [ SupportNetworkSingleResponseCaseExample, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, { since: '2024-12-01' } as SinceQueryParams, ], ])( @@ -94,7 +95,7 @@ describe('CasesService', () => { it.each([ [ InPersonVisitsSingleResponseCaseExample, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, { since: '2024-12-01' } as SinceQueryParams, ], ])( diff --git a/src/controllers/incidents/incidents.controller.spec.ts b/src/controllers/incidents/incidents.controller.spec.ts index 2047e1e..aa9381a 100644 --- a/src/controllers/incidents/incidents.controller.spec.ts +++ b/src/controllers/incidents/incidents.controller.spec.ts @@ -15,6 +15,7 @@ import { SupportNetworkService } from '../../helpers/support-network/support-net import { TokenRefresherService } from '../../external-api/token-refresher/token-refresher.service'; import { UtilitiesService } from '../../helpers/utilities/utilities.service'; import { RequestPreparerService } from '../../external-api/request-preparer/request-preparer.service'; +import { idName } from '../../common/constants/parameter-constants'; describe('IncidentsController', () => { let controller: IncidentsController; @@ -49,7 +50,7 @@ describe('IncidentsController', () => { it.each([ [ SupportNetworkSingleResponseIncidentExample, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, { since: '2020-02-02' } as SinceQueryParams, ], ])( diff --git a/src/controllers/incidents/incidents.controller.ts b/src/controllers/incidents/incidents.controller.ts index 2e388fa..51d3095 100644 --- a/src/controllers/incidents/incidents.controller.ts +++ b/src/controllers/incidents/incidents.controller.ts @@ -28,7 +28,10 @@ import { import { IdPathParams } from '../../dto/id-path-params.dto'; import { SinceQueryParams } from '../../dto/since-query-params.dto'; import { ApiNotFoundEntity } from '../../entities/api-not-found.entity'; -import { CONTENT_TYPE } from '../../common/constants/parameter-constants'; +import { + CONTENT_TYPE, + idName, +} from '../../common/constants/parameter-constants'; import { ApiInternalServerErrorEntity } from '../../entities/api-internal-server-error.entity'; import { AuthGuard } from '../../common/guards/auth/auth.guard'; @@ -40,7 +43,7 @@ export class IncidentsController { constructor(private readonly incidentsService: IncidentsService) {} @UseInterceptors(ClassSerializerInterceptor) - @Get(':id/support-network') + @Get(`:${idName}/support-network`) @ApiOperation({ description: 'Find all Support Network entries related to a given Incident entity by Incident id.', diff --git a/src/controllers/incidents/incidents.service.spec.ts b/src/controllers/incidents/incidents.service.spec.ts index fe5175d..5a38ac3 100644 --- a/src/controllers/incidents/incidents.service.spec.ts +++ b/src/controllers/incidents/incidents.service.spec.ts @@ -14,6 +14,7 @@ import { IdPathParams } from '../../dto/id-path-params.dto'; import { RecordType } from '../../common/constants/enumerations'; import { TokenRefresherService } from '../../external-api/token-refresher/token-refresher.service'; import { RequestPreparerService } from '../../external-api/request-preparer/request-preparer.service'; +import { idName } from '../../common/constants/parameter-constants'; describe('IncidentsService', () => { let service: IncidentsService; @@ -52,7 +53,7 @@ describe('IncidentsService', () => { it.each([ [ SupportNetworkSingleResponseIncidentExample, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, { since: '2024-12-01' } as SinceQueryParams, ], ])( diff --git a/src/controllers/service-requests/service-requests.controller.spec.ts b/src/controllers/service-requests/service-requests.controller.spec.ts index c17710f..34fbc84 100644 --- a/src/controllers/service-requests/service-requests.controller.spec.ts +++ b/src/controllers/service-requests/service-requests.controller.spec.ts @@ -14,6 +14,7 @@ import { SupportNetworkService } from '../../helpers/support-network/support-net import { TokenRefresherService } from '../../external-api/token-refresher/token-refresher.service'; import { UtilitiesService } from '../../helpers/utilities/utilities.service'; import { RequestPreparerService } from '../../external-api/request-preparer/request-preparer.service'; +import { idName } from '../../common/constants/parameter-constants'; describe('ServiceRequestsController', () => { let controller: ServiceRequestsController; @@ -51,7 +52,7 @@ describe('ServiceRequestsController', () => { it.each([ [ SupportNetworkSingleResponseSRExample, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, { since: '2020-02-02' } as SinceQueryParams, ], ])( diff --git a/src/controllers/service-requests/service-requests.controller.ts b/src/controllers/service-requests/service-requests.controller.ts index d3cbf42..245c5cc 100644 --- a/src/controllers/service-requests/service-requests.controller.ts +++ b/src/controllers/service-requests/service-requests.controller.ts @@ -26,7 +26,10 @@ import { import { IdPathParams } from '../../dto/id-path-params.dto'; import { SinceQueryParams } from '../../dto/since-query-params.dto'; import { ApiNotFoundEntity } from '../../entities/api-not-found.entity'; -import { CONTENT_TYPE } from '../../common/constants/parameter-constants'; +import { + CONTENT_TYPE, + idName, +} from '../../common/constants/parameter-constants'; import { ApiInternalServerErrorEntity } from '../../entities/api-internal-server-error.entity'; @Controller('sr') @@ -36,7 +39,7 @@ export class ServiceRequestsController { constructor(private readonly serviceRequestService: ServiceRequestsService) {} @UseInterceptors(ClassSerializerInterceptor) - @Get(':id/support-network') + @Get(`:${idName}/support-network`) @ApiOperation({ description: 'Find all Support Network entries related to a given Service Request entity by Service Request id.', diff --git a/src/controllers/service-requests/service-requests.service.spec.ts b/src/controllers/service-requests/service-requests.service.spec.ts index 3061e5b..7d7216a 100644 --- a/src/controllers/service-requests/service-requests.service.spec.ts +++ b/src/controllers/service-requests/service-requests.service.spec.ts @@ -14,6 +14,7 @@ import { IdPathParams } from '../../dto/id-path-params.dto'; import { SinceQueryParams } from '../../dto/since-query-params.dto'; import { TokenRefresherService } from '../../external-api/token-refresher/token-refresher.service'; import { RequestPreparerService } from '../../external-api/request-preparer/request-preparer.service'; +import { idName } from '../../common/constants/parameter-constants'; describe('ServiceRequestsService', () => { let service: ServiceRequestsService; @@ -53,7 +54,7 @@ describe('ServiceRequestsService', () => { it.each([ [ SupportNetworkSingleResponseSRExample, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, { since: '2024-12-01' } as SinceQueryParams, ], ])( diff --git a/src/dto/id-path-params.dto.ts b/src/dto/id-path-params.dto.ts index 007c086..60c2e50 100644 --- a/src/dto/id-path-params.dto.ts +++ b/src/dto/id-path-params.dto.ts @@ -1,6 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; import { Matches } from 'class-validator'; -import { idRegex } from '../common/constants/parameter-constants'; +import { idName, idRegex } from '../common/constants/parameter-constants'; export class IdPathParams { @Matches(idRegex) @@ -8,5 +8,5 @@ export class IdPathParams { example: 'Entity-Id-Here', description: 'The Entity Id for your selected record type', }) - id: string; + [idName]: string; } diff --git a/src/helpers/in-person-visits/in-person-visits.service.spec.ts b/src/helpers/in-person-visits/in-person-visits.service.spec.ts index 6b70b9e..78f17a9 100644 --- a/src/helpers/in-person-visits/in-person-visits.service.spec.ts +++ b/src/helpers/in-person-visits/in-person-visits.service.spec.ts @@ -16,6 +16,7 @@ import { InPersonVisitsSingleResponseCaseExample, NestedInPersonVisitsEntity, } from '../../entities/in-person-visits.entity'; +import { idName } from '../../common/constants/parameter-constants'; describe('InPersonVisitsService', () => { let service: InPersonVisitsService; @@ -56,13 +57,13 @@ describe('InPersonVisitsService', () => { [ InPersonVisitsSingleResponseCaseExample, RecordType.Case, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, undefined, ], [ InPersonVisitsListResponseCaseExample.items[0], RecordType.Case, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, { since: '2024-12-24' } as SinceQueryParams, ], ])( @@ -91,7 +92,7 @@ describe('InPersonVisitsService', () => { [ InPersonVisitsListResponseCaseExample, RecordType.Case, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, undefined, ], ])( diff --git a/src/helpers/in-person-visits/in-person-visits.service.ts b/src/helpers/in-person-visits/in-person-visits.service.ts index 1266124..0f6f9a7 100644 --- a/src/helpers/in-person-visits/in-person-visits.service.ts +++ b/src/helpers/in-person-visits/in-person-visits.service.ts @@ -12,6 +12,7 @@ import { baseUrlEnvVarName, inPersonVisitsEndpointEnvVarName, } from '../../common/constants/upstream-constants'; +import { idName } from '../../common/constants/parameter-constants'; @Injectable() export class InPersonVisitsService { @@ -37,7 +38,7 @@ export class InPersonVisitsService { id: IdPathParams, since?: SinceQueryParams, ): Promise { - const baseSearchSpec = `([Parent Id]="${id.id}"`; + const baseSearchSpec = `([Parent Id]="${id[idName]}"`; const [headers, params] = this.requestPreparerService.prepareHeadersAndParams( baseSearchSpec, diff --git a/src/helpers/support-network/support-network.service.spec.ts b/src/helpers/support-network/support-network.service.spec.ts index 7520387..9039220 100644 --- a/src/helpers/support-network/support-network.service.spec.ts +++ b/src/helpers/support-network/support-network.service.spec.ts @@ -17,6 +17,7 @@ import { IdPathParams } from '../../dto/id-path-params.dto'; import { SinceQueryParams } from '../../dto/since-query-params.dto'; import { TokenRefresherService } from '../../external-api/token-refresher/token-refresher.service'; import { RequestPreparerService } from '../../external-api/request-preparer/request-preparer.service'; +import { idName } from '../../common/constants/parameter-constants'; describe('SupportNetworkService', () => { let service: SupportNetworkService; @@ -57,13 +58,13 @@ describe('SupportNetworkService', () => { [ SupportNetworkSingleResponseCaseExample, RecordType.Case, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, undefined, ], [ SupportNetworkListResponseSRExample.items[0], RecordType.SR, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, { since: '2024-12-24' } as SinceQueryParams, ], ])( @@ -92,7 +93,7 @@ describe('SupportNetworkService', () => { [ SupportNetworkListResponseIncidentExample, RecordType.Incident, - { id: 'test' } as IdPathParams, + { [idName]: 'test' } as IdPathParams, undefined, ], ])( diff --git a/src/helpers/support-network/support-network.service.ts b/src/helpers/support-network/support-network.service.ts index 71eb45c..792335d 100644 --- a/src/helpers/support-network/support-network.service.ts +++ b/src/helpers/support-network/support-network.service.ts @@ -15,6 +15,7 @@ import { baseUrlEnvVarName, supportNetworkEndpointEnvVarName, } from '../../common/constants/upstream-constants'; +import { idName } from '../../common/constants/parameter-constants'; @Injectable() export class SupportNetworkService { @@ -40,7 +41,7 @@ export class SupportNetworkService { id: IdPathParams, since?: SinceQueryParams, ) { - const baseSearchSpec = `([Entity Id]="${id.id}" AND [Entity Name]="${RecordEntityMap[type]}"`; + const baseSearchSpec = `([Entity Id]="${id[idName]}" AND [Entity Name]="${RecordEntityMap[type]}"`; const [headers, params] = this.requestPreparerService.prepareHeadersAndParams( baseSearchSpec, From ad96fd31f66b16dcf590adf745b29388f23ca972 Mon Sep 17 00:00:00 2001 From: hannah-macdonald1 Date: Thu, 7 Nov 2024 09:19:45 -0700 Subject: [PATCH 2/3] removing old function and unit test updates --- src/common/guards/auth/auth.service.spec.ts | 28 +++++++++++---------- src/common/guards/auth/auth.service.ts | 11 -------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/common/guards/auth/auth.service.spec.ts b/src/common/guards/auth/auth.service.spec.ts index 4b99b1f..66aa829 100644 --- a/src/common/guards/auth/auth.service.spec.ts +++ b/src/common/guards/auth/auth.service.spec.ts @@ -29,8 +29,6 @@ describe('AuthService', () => { const validRecordType = RecordType.Case; const validPath = `/${validRecordType}/${validId}/testendpoint`; const notinEnumPath = `/fjofijp/${validId}/testendpoint`; - const noIdPath = `/${validRecordType}`; - const incorrectFormatPath = 'abcdefg'; const testIdir = 'IDIRTEST'; beforeEach(async () => { @@ -142,28 +140,32 @@ describe('AuthService', () => { ); }); - describe('grabRecordInfoFromPath tests', () => { + describe('grabRecordInfo tests', () => { it('returns an array of [id, type] when the correct url format is passed', () => { - const [id, recordType] = service.grabRecordInfoFromPath(validPath); + const mockRequest = getMockReq({ + params: { [idName]: validId }, + }); + const [id, recordType] = service.grabRecordInfo( + mockRequest, + validRecordType, + ); expect(id).toEqual(validId); expect(recordType).toEqual(validRecordType); }); it(`throws an error when the enum doesn't match the record type`, () => { + const mockRequest = getMockReq({ + params: { [idName]: validId }, + }); expect(() => { - service.grabRecordInfoFromPath(notinEnumPath); + service.grabRecordInfo(mockRequest, notinEnumPath); }).toThrow(EnumTypeError); }); - it(`throws an error when the url doesn't match the correct format`, () => { - expect(() => { - service.grabRecordInfoFromPath(incorrectFormatPath); - }).toThrow(Error); - }); - - it(`throws an error when the url doen't have an id`, () => { + it(`throws an error when the parameter doesn't exist for id`, () => { + const mockRequest = getMockReq(); expect(() => { - service.grabRecordInfoFromPath(noIdPath); + service.grabRecordInfo(mockRequest, validRecordType); }).toThrow(Error); }); }); diff --git a/src/common/guards/auth/auth.service.ts b/src/common/guards/auth/auth.service.ts index 2613565..282123e 100644 --- a/src/common/guards/auth/auth.service.ts +++ b/src/common/guards/auth/auth.service.ts @@ -66,17 +66,6 @@ export class AuthService { return true; } - grabRecordInfoFromPath(path: string): [string, RecordType] { - const pathParts = path.trim().slice(1).split('/', 2); // slice removes the leading / in the url - if (!this.utilitiesService.enumTypeGuard(RecordType, pathParts[0].trim())) { - throw new EnumTypeError(pathParts[0]); - } - if (pathParts.length < 2) { - throw new Error(`Id not found in path: '${path}'`); - } - return [pathParts[1].trim(), pathParts[0].trim() as RecordType]; - } - grabRecordInfo(req: Request, controllerPath: string): [string, RecordType] { if (!this.utilitiesService.enumTypeGuard(RecordType, controllerPath)) { throw new EnumTypeError(controllerPath); From a45875d864a3eefdf10f217ccf2efaeaea3adadf Mon Sep 17 00:00:00 2001 From: hannah-macdonald1 Date: Thu, 7 Nov 2024 09:30:09 -0700 Subject: [PATCH 3/3] changes to unit test --- src/common/guards/auth/auth.service.spec.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/common/guards/auth/auth.service.spec.ts b/src/common/guards/auth/auth.service.spec.ts index 66aa829..762a00c 100644 --- a/src/common/guards/auth/auth.service.spec.ts +++ b/src/common/guards/auth/auth.service.spec.ts @@ -27,8 +27,7 @@ describe('AuthService', () => { const validId = 'id1234'; const validRecordType = RecordType.Case; - const validPath = `/${validRecordType}/${validId}/testendpoint`; - const notinEnumPath = `/fjofijp/${validId}/testendpoint`; + const notinEnumPath = `fjofijp`; const testIdir = 'IDIRTEST'; beforeEach(async () => { @@ -93,7 +92,6 @@ describe('AuthService', () => { } as AxiosResponse), ); const mockRequest = getMockReq({ - path: validPath, header: jest.fn((key: string): string => { const headerVal: { [key: string]: string } = { [idirUsernameHeaderField]: testIdir, @@ -102,10 +100,9 @@ describe('AuthService', () => { }), params: { [idName]: 'id' }, }); - const controllerPath = 'case'; const isAuthed = await service.getRecordAndValidate( mockRequest, - controllerPath, + validRecordType, ); expect(spy).toHaveBeenCalledTimes(1); expect(cacheSpy).toHaveBeenCalledTimes(2); @@ -122,17 +119,15 @@ describe('AuthService', () => { .spyOn(cache, 'get') .mockResolvedValueOnce(cacheReturn); const mockRequest = getMockReq({ - path: validPath, header: jest.fn((key: string): string => { const headerVal: { [key: string]: string } = headers; return headerVal[key]; }), params: { [idName]: 'id' }, }); - const controllerPath = 'case'; const isAuthed = await service.getRecordAndValidate( mockRequest, - controllerPath, + validRecordType, ); expect(cacheSpy).toHaveBeenCalledTimes(cacheSpyCallTimes); expect(isAuthed).toBe(false); @@ -163,7 +158,7 @@ describe('AuthService', () => { }); it(`throws an error when the parameter doesn't exist for id`, () => { - const mockRequest = getMockReq(); + const mockRequest = getMockReq({ params: {} }); expect(() => { service.grabRecordInfo(mockRequest, validRecordType); }).toThrow(Error);