Skip to content

Commit 58ebd8b

Browse files
committed
chore: DRY tests & use service search v3
1 parent f0a2bd2 commit 58ebd8b

File tree

6 files changed

+45
-66
lines changed

6 files changed

+45
-66
lines changed

packages/getMyPrescriptions/tests/statusUpdate.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
newHandler,
3030
stateMachineEventHandler
3131
} from "../src/getMyPrescriptions"
32-
import {EXPECTED_TRACE_IDS, SERVICE_SEARCH_PARAMS} from "./utils"
32+
import {EXPECTED_TRACE_IDS, SERVICE_SEARCH_PARAMS, getServiceSearchEndpoint} from "./utils"
3333
import {createMockedPfPConfig, MockedPfPConfig, setupTestEnvironment} from "@pfp-common/testing"
3434
import {LogLevel} from "@aws-lambda-powertools/logger/types"
3535
import {Logger} from "@aws-lambda-powertools/logger"
@@ -143,10 +143,10 @@ describe("Unit tests for statusUpdate, via handler", function () {
143143
const event: GetMyPrescriptionsEvent = JSON.parse(exampleEvent)
144144

145145
mock
146-
.onGet("https://service-search/service-search", {params: {...SERVICE_SEARCH_PARAMS, search: "flm49"}})
146+
.onGet(getServiceSearchEndpoint(), {params: {...SERVICE_SEARCH_PARAMS, search: "flm49"}})
147147
.reply(200, JSON.parse(pharmacy2uResponse))
148148
mock
149-
.onGet("https://service-search/service-search", {params: {...SERVICE_SEARCH_PARAMS, search: "few08"}})
149+
.onGet(getServiceSearchEndpoint(), {params: {...SERVICE_SEARCH_PARAMS, search: "few08"}})
150150
.reply(200, JSON.parse(pharmicaResponse))
151151

152152
mock.onGet("https://spine/mm/patientfacingprescriptions").reply(200, JSON.parse(exampleInteractionResponse))

packages/getMyPrescriptions/tests/test-handler.test.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727

2828
import {HEADERS, StateMachineFunctionResponseBody, TIMEOUT_RESPONSE} from "../src/responses"
2929
import "./toMatchJsonLogMessage"
30-
import {EXPECTED_TRACE_IDS} from "./utils"
30+
import {EXPECTED_TRACE_IDS, getServiceSearchEndpoint, SERVICE_SEARCH_PARAMS} from "./utils"
3131
import {LogLevel} from "@aws-lambda-powertools/logger/types"
3232
import {createSpineClient} from "@NHSDigital/eps-spine-client"
3333
import {MiddyfiedHandler} from "@middy/core"
@@ -355,14 +355,6 @@ describe("Unit tests for app handler including service search", function () {
355355
let testEnv: ReturnType<typeof setupTestEnvironment>
356356
let mockedConfig: MockedPfPConfig
357357

358-
const queryParams = {
359-
"api-version": 2,
360-
searchFields: "ODSCode",
361-
$filter: "OrganisationTypeId eq 'PHA' and OrganisationSubType eq 'DistanceSelling'",
362-
$select: "URL,OrganisationSubType",
363-
$top: 1
364-
}
365-
366358
beforeEach(() => {
367359
testEnv = setupTestEnvironment()
368360
mockedConfig = createMockedPfPConfig([TC008_NHS_NUMBER])
@@ -394,11 +386,11 @@ describe("Unit tests for app handler including service search", function () {
394386
const event: GetMyPrescriptionsEvent = JSON.parse(exampleStateMachineEvent)
395387

396388
mock
397-
.onGet("https://service-search/service-search", {params: {...queryParams, search: "flm49"}})
389+
.onGet(getServiceSearchEndpoint(), {params: {...SERVICE_SEARCH_PARAMS, search: "flm49"}})
398390
.reply(200, JSON.parse(pharmacy2uResponse))
399391

400392
mock
401-
.onGet("https://service-search/service-search", {params: {...queryParams, search: "few08"}})
393+
.onGet(getServiceSearchEndpoint(), {params: {...SERVICE_SEARCH_PARAMS, search: "few08"}})
402394
.reply(200, JSON.parse(pharmicaResponse))
403395

404396
mock.onGet("https://spine/mm/patientfacingprescriptions").reply(200, JSON.parse(exampleInteractionResponse))
@@ -434,11 +426,11 @@ describe("Unit tests for app handler including service search", function () {
434426
mock.onGet("https://spine/mm/patientfacingprescriptions").reply(200, interactionResponse)
435427

436428
mock
437-
.onGet("https://service-search/service-search", {params: {...queryParams, search: "flm49"}})
429+
.onGet(getServiceSearchEndpoint(), {params: {...SERVICE_SEARCH_PARAMS, search: "flm49"}})
438430
.reply(200, JSON.parse(pharmacy2uResponse))
439431

440432
mock
441-
.onGet("https://service-search/service-search", {params: {...queryParams, search: "few08"}})
433+
.onGet(getServiceSearchEndpoint(), {params: {...SERVICE_SEARCH_PARAMS, search: "few08"}})
442434
.reply(200, JSON.parse(pharmicaResponse))
443435

444436
const event: GetMyPrescriptionsEvent = JSON.parse(exampleStateMachineEvent)
@@ -465,7 +457,7 @@ describe("Unit tests for app handler including service search", function () {
465457
mock.onGet("https://spine/mm/patientfacingprescriptions").reply(200, exampleResponse)
466458

467459
// eslint-disable-next-line @typescript-eslint/no-unused-vars
468-
mock.onGet("https://service-search/service-search").reply(function (config) {
460+
mock.onGet(getServiceSearchEndpoint()).reply(function (config) {
469461
return new Promise((resolve) => setTimeout(() => resolve([200, {}]), 15_000))
470462
})
471463

@@ -502,14 +494,6 @@ describe("Unit tests for logging functionality", function () {
502494
let testEnv: ReturnType<typeof setupTestEnvironment>
503495
let mockedConfig: MockedPfPConfig
504496

505-
const queryParams = {
506-
"api-version": 2,
507-
searchFields: "ODSCode",
508-
$filter: "OrganisationTypeId eq 'PHA' and OrganisationSubType eq 'DistanceSelling'",
509-
$select: "URL,OrganisationSubType",
510-
$top: 1
511-
}
512-
513497
beforeEach(() => {
514498
testEnv = setupTestEnvironment()
515499
mockedConfig = createMockedPfPConfig([TC008_NHS_NUMBER])
@@ -590,11 +574,11 @@ describe("Unit tests for logging functionality", function () {
590574
mock.onGet("https://spine/mm/patientfacingprescriptions").reply(200, interactionResponse)
591575

592576
mock
593-
.onGet("https://service-search/service-search", {params: {...queryParams, search: "flm49"}})
577+
.onGet(getServiceSearchEndpoint(), {params: {...SERVICE_SEARCH_PARAMS, search: "flm49"}})
594578
.reply(200, JSON.parse(pharmacy2uResponse))
595579

596580
mock
597-
.onGet("https://service-search/service-search", {params: {...queryParams, search: "few08"}})
581+
.onGet(getServiceSearchEndpoint(), {params: {...SERVICE_SEARCH_PARAMS, search: "few08"}})
598582
.reply(200, JSON.parse(pharmicaResponse))
599583

600584
const event: GetMyPrescriptionsEvent = JSON.parse(exampleStateMachineEvent)

packages/getMyPrescriptions/tests/utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import {jest} from "@jest/globals"
22
import {Organization} from "fhir/r4"
33
import {TraceIDs} from "../src/responses"
4+
import {
5+
SERVICE_SEARCH_BASE_QUERY_PARAMS,
6+
getServiceSearchEndpoint
7+
} from "@prescriptionsforpatients/serviceSearchClient"
48

59
// Uses unstable jest method to enable mocking while using ESM. To be replaced in future.
610
export function mockInternalDependency(modulePath: string, module: object, dependency: string) {
@@ -12,13 +16,9 @@ export function mockInternalDependency(modulePath: string, module: object, depen
1216
return mockDependency
1317
}
1418

15-
export const SERVICE_SEARCH_PARAMS = {
16-
"api-version": 2,
17-
searchFields: "ODSCode",
18-
$filter: "OrganisationTypeId eq 'PHA' and OrganisationSubType eq 'DistanceSelling'",
19-
$select: "URL,OrganisationSubType",
20-
$top: 1
21-
}
19+
// Re-export for convenience in tests
20+
export const SERVICE_SEARCH_PARAMS = SERVICE_SEARCH_BASE_QUERY_PARAMS
21+
export {getServiceSearchEndpoint}
2222

2323
export const EXPECTED_TRACE_IDS: TraceIDs = {
2424
"apigw-request-id": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import {createServiceSearchClient, ServiceSearchClient} from "./serviceSearch-client"
2+
import {SERVICE_SEARCH_BASE_QUERY_PARAMS, getServiceSearchEndpoint} from "./live-serviceSearch-client"
23

3-
export {createServiceSearchClient, ServiceSearchClient}
4+
export {createServiceSearchClient, ServiceSearchClient, SERVICE_SEARCH_BASE_QUERY_PARAMS, getServiceSearchEndpoint}

packages/serviceSearchClient/src/live-serviceSearch-client.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,29 @@ export type ServiceSearchData = {
1818
"value": Array<Service>
1919
}
2020

21+
export const SERVICE_SEARCH_BASE_QUERY_PARAMS = {
22+
"api-version": 3,
23+
"searchFields": "ODSCode",
24+
"$filter": "OrganisationTypeId eq 'PHA' and OrganisationSubType eq 'DistanceSelling'",
25+
"$select": "URL,OrganisationSubType",
26+
"$top": 1
27+
} as const
28+
29+
export function getServiceSearchEndpoint(targetServer?: string): string {
30+
const endpoint = targetServer || process.env.TargetServiceSearchServer || "service-search"
31+
const baseUrl = `https://${endpoint}`
32+
if (endpoint.toLowerCase().includes("api.service.nhs.uk")) {
33+
// service search v3
34+
return `${baseUrl}/service-search-api/`
35+
}
36+
// service search v2
37+
return `${baseUrl}/service-search`
38+
}
39+
2140
export class LiveServiceSearchClient implements ServiceSearchClient {
22-
private readonly SERVICE_SEARCH_URL_SCHEME = "https"
23-
private readonly SERVICE_SEARCH_ENDPOINT = process.env.TargetServiceSearchServer
2441
private readonly axiosInstance: AxiosInstance
2542
private readonly logger: Logger
2643
private readonly outboundHeaders: {"Subscription-Key": string | undefined}
27-
private readonly baseQueryParams: {
28-
"api-version": number,
29-
"searchFields": string,
30-
"$filter": string,
31-
"$select": string,
32-
"$top": number
33-
}
3444

3545
constructor(logger: Logger) {
3646
this.logger = logger
@@ -87,19 +97,12 @@ export class LiveServiceSearchClient implements ServiceSearchClient {
8797
this.outboundHeaders = {
8898
"Subscription-Key": process.env.ServiceSearchApiKey
8999
}
90-
this.baseQueryParams = {
91-
"api-version": 3,
92-
"searchFields": "ODSCode",
93-
"$filter": "OrganisationTypeId eq 'PHA' and OrganisationSubType eq 'DistanceSelling'",
94-
"$select": "URL,OrganisationSubType",
95-
"$top": 1
96-
}
97100
}
98101

99102
async searchService(odsCode: string): Promise<URL | undefined> {
100103
try {
101-
const address = this.getServiceSearchEndpoint()
102-
const queryParams = {...this.baseQueryParams, search: odsCode}
104+
const address = getServiceSearchEndpoint()
105+
const queryParams = {...SERVICE_SEARCH_BASE_QUERY_PARAMS, search: odsCode}
103106

104107
this.logger.info(`making request to ${address} with ods code ${odsCode}`, {odsCode: odsCode})
105108
const response = await this.axiosInstance.get(address, {
@@ -162,14 +165,4 @@ export class LiveServiceSearchClient implements ServiceSearchClient {
162165
delete error.request.headers[headerKey]
163166
}
164167
}
165-
166-
private getServiceSearchEndpoint() {
167-
const baseUrl = `${this.SERVICE_SEARCH_URL_SCHEME}://${this.SERVICE_SEARCH_ENDPOINT}`
168-
if (this.SERVICE_SEARCH_ENDPOINT?.toLowerCase().includes("api.service.nhs.uk")) {
169-
// service search v3
170-
return `${baseUrl}/service-search-api/`
171-
}
172-
// service search v2
173-
return `${baseUrl}/service-search`
174-
}
175168
}

packages/serviceSearchClient/tests/live-serviceSearch-client.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ describe("live serviceSearch client", () => {
2828
jest.restoreAllMocks()
2929
})
3030

31-
// Private helper tests
32-
test("getServiceSearchEndpoint returns correct URL", () => {
33-
const endpoint = client["getServiceSearchEndpoint"]()
31+
// Helper function tests
32+
test("getServiceSearchEndpoint returns correct URL", async () => {
33+
const {getServiceSearchEndpoint} = await import("../src/live-serviceSearch-client.js")
34+
const endpoint = getServiceSearchEndpoint()
3435
expect(endpoint).toBe(serviceSearchUrl)
3536
})
3637

0 commit comments

Comments
 (0)