|
| 1 | +import { ninoCheckEndpoint, createSession, getJarAuthorization } from "../endpoints"; |
| 2 | +import { clearAttemptsTable, clearItemsFromTables, getItemByKey, queryItemsBySessionId } from "../../resources/dynamodb-helper"; |
| 3 | +import { AUDIENCE, NINO } from "../env-variables"; |
| 4 | + |
| 5 | +jest.setTimeout(30_000); |
| 6 | + |
| 7 | +describe("check-lambda retry logic", () => { |
| 8 | + const errorNino = "ER123456A"; |
| 9 | + let sessionId: string; |
| 10 | + let sessionTableName: string; |
| 11 | + let privateApi: string; |
| 12 | + let issuer: string | undefined; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + privateApi = `${process.env.PRIVATE_API}`; |
| 16 | + sessionTableName = `${process.env.SESSION_TABLE}`; |
| 17 | + const data = await getJarAuthorization(); |
| 18 | + const request = await data.json(); |
| 19 | + const session = await createSession(privateApi, request); |
| 20 | + const sessionData = await session.json(); |
| 21 | + sessionId = sessionData.session_id; |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(async () => { |
| 25 | + await clearItemsFromTables( |
| 26 | + { |
| 27 | + tableName: `${process.env.PERSON_IDENTITY_TABLE}`, |
| 28 | + items: { sessionId: sessionId }, |
| 29 | + }, |
| 30 | + { |
| 31 | + tableName: `${process.env.NINO_USERS_TABLE}`, |
| 32 | + items: { sessionId: sessionId }, |
| 33 | + }, |
| 34 | + { |
| 35 | + tableName: sessionTableName, |
| 36 | + items: { sessionId: sessionId }, |
| 37 | + } |
| 38 | + ); |
| 39 | + await clearAttemptsTable(sessionId, `${process.env.USERS_ATTEMPTS_TABLE}`); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should not attempt further matches after 2 attempts", async () => { |
| 43 | + const firstCheck = await ninoCheckEndpoint(privateApi, { "session-id": sessionId }, errorNino); |
| 44 | + const firstResBody = { ...(await firstCheck.json()) }; |
| 45 | + expect(firstCheck.status).toEqual(200); |
| 46 | + expect(firstResBody).toStrictEqual({ requestRetry: true }); |
| 47 | + const firstAttemptsItems = await queryItemsBySessionId(process.env.USERS_ATTEMPTS_TABLE!, sessionId); |
| 48 | + expect(firstAttemptsItems.Count).toEqual(1); |
| 49 | + expect(firstAttemptsItems.Items![0].attempt).toBe("FAIL"); |
| 50 | + expect(firstAttemptsItems.Items![0].text).toBe("CID returned no record"); |
| 51 | + |
| 52 | + const secondCheck = await ninoCheckEndpoint(privateApi, { "session-id": sessionId }, errorNino); |
| 53 | + const secondResBody = { ...(await secondCheck.json()) }; |
| 54 | + expect(secondCheck.status).toEqual(200); |
| 55 | + expect(secondResBody).toStrictEqual({ requestRetry: false }); |
| 56 | + const secondAttemptsItems = await queryItemsBySessionId(process.env.USERS_ATTEMPTS_TABLE!, sessionId); |
| 57 | + expect(secondAttemptsItems.Count).toEqual(2); |
| 58 | + expect(secondAttemptsItems.Items![0].attempt).toBe("FAIL"); |
| 59 | + expect(secondAttemptsItems.Items![1].attempt).toBe("FAIL"); |
| 60 | + |
| 61 | + const thirdCheck = await ninoCheckEndpoint(privateApi, { "session-id": sessionId }, errorNino); |
| 62 | + const thirdResBody = { ...(await thirdCheck.json()) }; |
| 63 | + expect(thirdCheck.status).toEqual(200); |
| 64 | + expect(thirdResBody).toStrictEqual({ requestRetry: false }); |
| 65 | + const thirdAttemptsItems = await queryItemsBySessionId(process.env.USERS_ATTEMPTS_TABLE!, sessionId); |
| 66 | + expect(thirdAttemptsItems.Count).toEqual(2); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should successfully match even after a failed attempt", async () => { |
| 70 | + const firstCheck = await ninoCheckEndpoint(privateApi, { "session-id": sessionId }, errorNino); |
| 71 | + const firstResBody = { ...(await firstCheck.json()) }; |
| 72 | + expect(firstCheck.status).toEqual(200); |
| 73 | + expect(firstResBody).toStrictEqual({ requestRetry: true }); |
| 74 | + const firstAttemptsItems = await queryItemsBySessionId(process.env.USERS_ATTEMPTS_TABLE!, sessionId); |
| 75 | + expect(firstAttemptsItems.Count).toEqual(1); |
| 76 | + expect(firstAttemptsItems.Items![0].attempt).toBe("FAIL"); |
| 77 | + |
| 78 | + const secondCheck = await ninoCheckEndpoint(privateApi, { "session-id": sessionId }, NINO); |
| 79 | + const secondResBody = { ...(await secondCheck.json()) }; |
| 80 | + expect(secondCheck.status).toEqual(200); |
| 81 | + expect(secondResBody).toStrictEqual({ requestRetry: false }); |
| 82 | + const secondAttemptsItems = await queryItemsBySessionId(process.env.USERS_ATTEMPTS_TABLE!, sessionId); |
| 83 | + expect(secondAttemptsItems.Count).toEqual(2); |
| 84 | + expect(secondAttemptsItems.Items!.some(item => item.attempt === 'PASS')).toBe(true); |
| 85 | + expect(secondAttemptsItems.Items!.some(item => item.attempt === 'FAIL')).toBe(true); |
| 86 | + }); |
| 87 | +}); |
0 commit comments