Skip to content

Commit 9dfefd1

Browse files
CCM-12183 Component test for letter PDF (#367)
* Added tests for letter PDF * Fixed linting error * Modified tests * test ssh-sign * lint fix Signed-off-by: namitha.prabhu <namitha.prabhu2@nhs.net> * added comments for response.url Signed-off-by: namitha.prabhu <namitha.prabhu2@nhs.net> * lint Signed-off-by: namitha.prabhu <namitha.prabhu2@nhs.net> * fix Signed-off-by: namitha.prabhu <namitha.prabhu2@nhs.net> --------- Signed-off-by: namitha.prabhu <namitha.prabhu2@nhs.net> Co-authored-by: namitha.prabhu <namitha.prabhu2@nhs.net>
1 parent 8f42519 commit 9dfefd1

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { expect, test } from "@playwright/test";
2+
import getRestApiGatewayBaseUrl from "../../helpers/aws-gateway-helper";
3+
import { getLettersBySupplier } from "../../helpers/generate-fetch-test-data";
4+
import {
5+
DATA,
6+
SUPPLIERID,
7+
SUPPLIER_LETTERS,
8+
} from "../../constants/api-constants";
9+
import { createValidRequestHeaders } from "../../constants/request-headers";
10+
import {
11+
error404ResponseBody,
12+
error500ResponseBody,
13+
} from "../../helpers/common-types";
14+
15+
let baseUrl: string;
16+
17+
test.beforeAll(async () => {
18+
baseUrl = await getRestApiGatewayBaseUrl();
19+
});
20+
21+
test.describe("API Gateway Tests to Verify Get Letter PDF Endpoint", () => {
22+
test(`Get /letters/{id}/data returns 200 and valid response for a given id`, async ({
23+
request,
24+
}) => {
25+
const letters = await getLettersBySupplier(SUPPLIERID, "PENDING", 1);
26+
27+
if (!letters?.length) {
28+
test.fail(true, `No PENDING letters found for supplier ${SUPPLIERID}`);
29+
return;
30+
}
31+
const letter = letters[0];
32+
const headers = createValidRequestHeaders();
33+
const response = await request.get(
34+
`${baseUrl}/${SUPPLIER_LETTERS}/${letter.id}/${DATA}`,
35+
{
36+
headers,
37+
},
38+
);
39+
40+
expect(response.status()).toBe(200);
41+
const responseBody = await response.text();
42+
expect(responseBody).toContain("PDF");
43+
44+
async function fetchUrl(url: string) {
45+
const res = await request.get(url, { headers });
46+
return {
47+
status: res.status(),
48+
headers: res.headers(),
49+
buffer: await res.body().catch(() => null),
50+
text: await res.text().catch(() => null),
51+
};
52+
}
53+
54+
const pdfUrl = await response.url(); // function returns response url though get letter data will automatically redirect to the url in the Location header.
55+
const parsed = new URL(pdfUrl);
56+
const expiresParam = parsed.searchParams.get("X-Amz-Expires");
57+
const expiresSeconds = Number(expiresParam);
58+
expect(expiresSeconds).toBe(60);
59+
60+
const waitMs = Math.max(expiresSeconds * 1000 + 2000, 0);
61+
await new Promise((resolve) => {
62+
setTimeout(resolve, waitMs);
63+
});
64+
65+
const after = await fetchUrl(pdfUrl);
66+
expect(after.status).toBe(403);
67+
});
68+
69+
test(`Get /letters/{id}/data returns 404 if no resource is found for id`, async ({
70+
request,
71+
}) => {
72+
const id = "11";
73+
const headers = createValidRequestHeaders();
74+
const response = await request.get(
75+
`${baseUrl}/${SUPPLIER_LETTERS}/${id}/${DATA}`,
76+
{
77+
headers,
78+
},
79+
);
80+
81+
const responseBody = await response.json();
82+
expect(response.status()).toBe(404);
83+
expect(responseBody).toMatchObject(error404ResponseBody());
84+
});
85+
86+
// CCM-14318: Remove this test
87+
test(`Get /letters/{id}/data returns 500 if letter is not found for supplierId ${SUPPLIERID}`, async ({
88+
request,
89+
}) => {
90+
const id = "non-existing-id-12345";
91+
const headers = createValidRequestHeaders();
92+
const response = await request.get(
93+
`
94+
${baseUrl}/${SUPPLIER_LETTERS}/${id}/${DATA}`,
95+
{
96+
headers,
97+
},
98+
);
99+
100+
const responseBody = await response.json();
101+
expect(response.status()).toBe(500);
102+
expect(responseBody).toMatchObject(error500ResponseBody());
103+
});
104+
});

tests/config/playwright.base.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const envMaxInstances = Number.parseInt(process.env.WORKERS_MAX_INST!) || 10;
88
*/
99
export const config: PlaywrightTestConfig = {
1010
/* Maximum time one test can run for. */
11-
timeout: 60 * 1000,
11+
timeout: 80 * 1000,
1212
workers: envMaxInstances,
1313
expect: {
1414
/**

tests/constants/api-constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ export const SUPPLIERID = "TestSupplier1";
99
export const MI_ENDPOINT = "mi";
1010
export const SUPPLIERTABLENAME = `nhs-${envName}-supapi-suppliers`;
1111
export const UPSERT_LETTER_LAMBDA_ARN = `arn:aws:lambda:eu-west-2:820178564574:function:nhs-${envName}-supapi-upsertletter`;
12+
export const DATA = "data";

0 commit comments

Comments
 (0)