|
| 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 | +}); |
0 commit comments