From 1cf54a915338360b34849f01544b2ecc71285db1 Mon Sep 17 00:00:00 2001 From: Jonno Date: Thu, 26 Mar 2026 11:59:35 +0000 Subject: [PATCH 1/5] changed log levels and added testing --- .../src/authenticationConcurrentAwareMiddleware.ts | 3 ++- .../common/authFunctions/src/authenticationMiddleware.ts | 8 ++------ .../test_authenticationConcurrentAwareMiddleware.test.ts | 9 ++++++--- .../tests/test_authenticationMiddleware.test.ts | 2 +- packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx | 2 +- packages/cpt-ui/src/helpers/userInfo.tsx | 2 +- packages/cpt-ui/src/pages/PrescriptionListPage.tsx | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/common/authFunctions/src/authenticationConcurrentAwareMiddleware.ts b/packages/common/authFunctions/src/authenticationConcurrentAwareMiddleware.ts index d6d629233f..711c1d2054 100644 --- a/packages/common/authFunctions/src/authenticationConcurrentAwareMiddleware.ts +++ b/packages/common/authFunctions/src/authenticationConcurrentAwareMiddleware.ts @@ -77,7 +77,7 @@ export const authenticationConcurrentAwareMiddleware = ( logger.info("A session is active but does not match the requestors sessionId", {username, sessionId}) invalidSessionCause = "ConcurrentSession" } else { - logger.error("Request token invalid. No matching session found.", { + logger.warn("Request token invalid. No matching session found.", { tokenMappingSessionId, sessionManagementSessionId }) @@ -88,6 +88,7 @@ export const authenticationConcurrentAwareMiddleware = ( } } catch (error) { logger.error("Authentication failed returning restart login prompt", {error}) + invalidSessionCause = "InvalidSession" } // Handle timeout responses diff --git a/packages/common/authFunctions/src/authenticationMiddleware.ts b/packages/common/authFunctions/src/authenticationMiddleware.ts index 789c5f8eac..836e7cc994 100644 --- a/packages/common/authFunctions/src/authenticationMiddleware.ts +++ b/packages/common/authFunctions/src/authenticationMiddleware.ts @@ -51,17 +51,13 @@ export const authenticationMiddleware = ({ invalidSessionCause = "Timeout" authenticatedResult = null } - } else if (tokenMappingItem !== undefined) { + } else { logger.info("A session is active but does not match the requestors sessionId", {username, sessionId}) invalidSessionCause = "ConcurrentSession" - } else { - logger.error("Request token invalid. No matching session found.", { - tokenMappingSessionId - }) - invalidSessionCause = "InvalidSession" } } catch (error) { logger.error("Authentication failed returning restart login prompt", {error}) + invalidSessionCause = "InvalidSession" } if (!authenticatedResult || "isTimeout" in authenticatedResult) { request.earlyResponse = { diff --git a/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts b/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts index 77064f99bb..c6390a1dd0 100644 --- a/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts +++ b/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts @@ -453,7 +453,8 @@ describe("authenticationConcurrentAwareMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -553,7 +554,8 @@ describe("authenticationConcurrentAwareMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + InvalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -585,7 +587,8 @@ describe("authenticationConcurrentAwareMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) diff --git a/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts b/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts index f66ec7d4cb..171588c371 100644 --- a/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts +++ b/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts @@ -189,7 +189,7 @@ describe("authenticationMiddleware", () => { mockGetUsernameFromEvent.mockReturnValue(username) mockGetSessionIdFromEvent.mockReturnValue(sessionId) - mockGetTokenMapping.mockResolvedValue(undefined) + mockGetTokenMapping.mockRejectedValue(new Error("No matching session found")) const middleware = authenticationMiddleware({axiosInstance, ddbClient, authOptions, logger}) diff --git a/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx b/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx index fd563a57f3..19e2db227f 100644 --- a/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx +++ b/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx @@ -726,7 +726,7 @@ describe("PrescriptionListPage", () => { expect(mockNavigate).toHaveBeenCalledWith(FRONTEND_PATHS.NO_PRESCRIPTIONS_FOUND) }) - expect(logger.error).toHaveBeenCalledWith( + expect(logger.info).toHaveBeenCalledWith( "A patient was returned, but they do not have any prescriptions.", emptyResponse ) diff --git a/packages/cpt-ui/src/helpers/userInfo.tsx b/packages/cpt-ui/src/helpers/userInfo.tsx index e987f9cb24..842cedce96 100644 --- a/packages/cpt-ui/src/helpers/userInfo.tsx +++ b/packages/cpt-ui/src/helpers/userInfo.tsx @@ -81,7 +81,7 @@ export const getTrackerUserInfo = async (): Promise => { } if (!invalidSessionCause) { - logger.error("Error fetching tracker user info:", err) + logger.warn("Error fetching tracker user info:", err) } else { logger.warn("trackerUserInfo triggered restart login due to:", invalidSessionCause) } diff --git a/packages/cpt-ui/src/pages/PrescriptionListPage.tsx b/packages/cpt-ui/src/pages/PrescriptionListPage.tsx index 3c018907ea..6d97e33847 100644 --- a/packages/cpt-ui/src/pages/PrescriptionListPage.tsx +++ b/packages/cpt-ui/src/pages/PrescriptionListPage.tsx @@ -115,7 +115,7 @@ export default function PrescriptionListPage() { searchResults.pastPrescriptions.length === 0 && searchResults.futurePrescriptions.length === 0 ) { - logger.error( + logger.info( "A patient was returned, but they do not have any prescriptions.", searchResults ) From 97592cae6842c3718465fac293bde402a087305a Mon Sep 17 00:00:00 2001 From: Jonno Date: Thu, 26 Mar 2026 12:10:05 +0000 Subject: [PATCH 2/5] tests --- ...t_authenticationConcurrentAwareMiddleware.test.ts | 8 +++++--- .../tests/test_authenticationMiddleware.test.ts | 12 ++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts b/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts index c6390a1dd0..0b36afb56a 100644 --- a/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts +++ b/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts @@ -484,7 +484,8 @@ describe("authenticationConcurrentAwareMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -517,7 +518,8 @@ describe("authenticationConcurrentAwareMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -555,7 +557,7 @@ describe("authenticationConcurrentAwareMiddleware", () => { body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", restartLogin: true, - InvalidSessionCause: "InvalidSession" + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) diff --git a/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts b/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts index 171588c371..7d73d5aed0 100644 --- a/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts +++ b/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts @@ -230,7 +230,8 @@ describe("authenticationMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -301,7 +302,8 @@ describe("authenticationMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -331,7 +333,8 @@ describe("authenticationMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -364,7 +367,8 @@ describe("authenticationMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) From f0285658338524a0e72bc98291c153a87bb02128 Mon Sep 17 00:00:00 2001 From: jonathanwelch1-nhs <148754575+jonathanwelch1-nhs@users.noreply.github.com> Date: Thu, 26 Mar 2026 14:40:52 +0000 Subject: [PATCH 3/5] Update packages/cpt-ui/src/pages/PrescriptionListPage.tsx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- packages/cpt-ui/src/pages/PrescriptionListPage.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/cpt-ui/src/pages/PrescriptionListPage.tsx b/packages/cpt-ui/src/pages/PrescriptionListPage.tsx index 6d97e33847..5dc3c4fb81 100644 --- a/packages/cpt-ui/src/pages/PrescriptionListPage.tsx +++ b/packages/cpt-ui/src/pages/PrescriptionListPage.tsx @@ -117,7 +117,11 @@ export default function PrescriptionListPage() { ) { logger.info( "A patient was returned, but they do not have any prescriptions.", - searchResults + { + currentPrescriptionsCount: searchResults.currentPrescriptions.length, + futurePrescriptionsCount: searchResults.futurePrescriptions.length, + pastPrescriptionsCount: searchResults.pastPrescriptions.length + } ) setPatientDetails(searchResults.patient) setPatientFallback(searchResults.patientFallback) From 11338525fadb5d092fd00757c9054876bff50b13 Mon Sep 17 00:00:00 2001 From: Jonno Date: Thu, 26 Mar 2026 14:41:19 +0000 Subject: [PATCH 4/5] copilot suggestioins --- .../tests/test_authenticationMiddleware.test.ts | 2 +- packages/cpt-ui/src/pages/PrescriptionListPage.tsx | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts b/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts index 7d73d5aed0..c02f51cdb8 100644 --- a/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts +++ b/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts @@ -182,7 +182,7 @@ describe("authenticationMiddleware", () => { expect(result).toEqual(mockRequest.earlyResponse) }) - it("should return 401 when token mapping is undefined", async () => { + it("should return 401 when getTokenMapping throws an error", async () => { // Arrange const username = "test-user" const sessionId = "test-session-id" diff --git a/packages/cpt-ui/src/pages/PrescriptionListPage.tsx b/packages/cpt-ui/src/pages/PrescriptionListPage.tsx index 6d97e33847..5dc3c4fb81 100644 --- a/packages/cpt-ui/src/pages/PrescriptionListPage.tsx +++ b/packages/cpt-ui/src/pages/PrescriptionListPage.tsx @@ -117,7 +117,11 @@ export default function PrescriptionListPage() { ) { logger.info( "A patient was returned, but they do not have any prescriptions.", - searchResults + { + currentPrescriptionsCount: searchResults.currentPrescriptions.length, + futurePrescriptionsCount: searchResults.futurePrescriptions.length, + pastPrescriptionsCount: searchResults.pastPrescriptions.length + } ) setPatientDetails(searchResults.patient) setPatientFallback(searchResults.patientFallback) From bdc2d86ff3bb875980d941eb607d2b25cef26f1c Mon Sep 17 00:00:00 2001 From: Jonno Date: Thu, 26 Mar 2026 16:43:49 +0000 Subject: [PATCH 5/5] improved log and fixed test --- packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx | 3 +-- packages/cpt-ui/src/pages/PrescriptionListPage.tsx | 7 +------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx b/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx index 19e2db227f..b5ae6adb66 100644 --- a/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx +++ b/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx @@ -727,8 +727,7 @@ describe("PrescriptionListPage", () => { }) expect(logger.info).toHaveBeenCalledWith( - "A patient was returned, but they do not have any prescriptions.", - emptyResponse + "A patient was returned, but they do not have any prescriptions." ) }) }) diff --git a/packages/cpt-ui/src/pages/PrescriptionListPage.tsx b/packages/cpt-ui/src/pages/PrescriptionListPage.tsx index 5dc3c4fb81..588ee04aeb 100644 --- a/packages/cpt-ui/src/pages/PrescriptionListPage.tsx +++ b/packages/cpt-ui/src/pages/PrescriptionListPage.tsx @@ -116,12 +116,7 @@ export default function PrescriptionListPage() { searchResults.futurePrescriptions.length === 0 ) { logger.info( - "A patient was returned, but they do not have any prescriptions.", - { - currentPrescriptionsCount: searchResults.currentPrescriptions.length, - futurePrescriptionsCount: searchResults.futurePrescriptions.length, - pastPrescriptionsCount: searchResults.pastPrescriptions.length - } + "A patient was returned, but they do not have any prescriptions." ) setPatientDetails(searchResults.patient) setPatientFallback(searchResults.patientFallback)