|
1 | 1 | import { test, expect } from '@playwright/test'; |
2 | | -import { SUPPLIER_API_GATEWAY_NAME, SUPPLIER_LETTERS, AWS_REGION } from '../../constants/api_constants'; |
| 2 | +import { SUPPLIER_LETTERS, supplierId } from '../../constants/api_constants'; |
3 | 3 | import { getRestApiGatewayBaseUrl } from '../../helpers/awsGatewayHelper'; |
4 | | -import { createValidRequestHeaders } from '../../constants/request_headers'; |
5 | | -import { apiPatchMessageRequestTestData } from './testCases/UpdateLetterStatus'; |
| 4 | +import { patchFailureRequestBody, patchRequestHeaders, patchValidRequestBody } from './testCases/UpdateLetterStatus'; |
| 5 | +import { createTestData, deleteLettersBySupplier, getLettersBySupplier } from '../../helpers/generate_fetch_testData'; |
| 6 | +import { randomUUID } from 'crypto'; |
| 7 | +import { createInvalidRequestHeaders } from '../../constants/request_headers'; |
6 | 8 |
|
7 | 9 | let baseUrl: string; |
8 | 10 |
|
9 | 11 | test.beforeAll(async () => { |
10 | | - const region = AWS_REGION; |
11 | | - baseUrl = await getRestApiGatewayBaseUrl(SUPPLIER_API_GATEWAY_NAME, region); |
| 12 | + baseUrl = await getRestApiGatewayBaseUrl(); |
12 | 13 | }); |
13 | 14 |
|
14 | | -test.describe('API Gateway Tests To Verify Patch Status Endpoint ', () => { |
15 | | - apiPatchMessageRequestTestData.forEach(({ testCase, id, body, expectedStatus, expectedResponse }) => { |
16 | | - test(`Patch /letters returns ${testCase}`, async ({ request }) => { |
17 | | - const response = await request.patch(`${baseUrl}/${SUPPLIER_LETTERS}/${id}` ,{ |
18 | | - headers: await createValidRequestHeaders(), |
| 15 | +test.describe('API Gateway Tests to Verify Patch Status Endpoint', () => { |
| 16 | + test(`Patch /letters returns 200 and status is updated to ACCEPTED`, async ({ request }) => { |
| 17 | + |
| 18 | + await createTestData(supplierId); |
| 19 | + const letters = await getLettersBySupplier(supplierId, 'PENDING', 1); |
| 20 | + |
| 21 | + if (!letters?.length) { |
| 22 | + test.fail(true, `No PENDING letters found for supplier ${supplierId}`); |
| 23 | + return; |
| 24 | + } |
| 25 | + const letter = letters[0]; |
| 26 | + const headers = await patchRequestHeaders(); |
| 27 | + const body = await patchValidRequestBody(letter.id, 'ACCEPTED'); |
| 28 | + |
| 29 | + const response = await request.patch(`${baseUrl}/${SUPPLIER_LETTERS}/${letter.id}`, { |
| 30 | + headers: headers, |
19 | 31 | data: body |
20 | | - }, |
21 | | - ); |
22 | | - const res = await response.json(); |
| 32 | + }); |
23 | 33 |
|
24 | | - expect(response.status()).toBe(expectedStatus); |
25 | | - expect(res).toEqual(expectedResponse); |
| 34 | + const res = await response.json(); |
| 35 | + expect(response.status()).toBe(200); |
| 36 | + expect(res).toMatchObject({ |
| 37 | + data:{ |
| 38 | + attributes: { |
| 39 | + status: 'ACCEPTED', |
| 40 | + specificationId: 'TestSpecificationID', |
| 41 | + groupId: 'TestGroupID', |
| 42 | + }, |
| 43 | + id: letter.id, |
| 44 | + type: 'Letter' |
| 45 | + } |
26 | 46 | }); |
| 47 | + |
| 48 | + await deleteLettersBySupplier(letter.id); |
27 | 49 | }); |
| 50 | + |
| 51 | + test(`Patch /letters returns 200 and status is updated to REJECTED`, async ({ request }) => { |
| 52 | + |
| 53 | + await createTestData(supplierId); |
| 54 | + const letters = await getLettersBySupplier(supplierId, 'PENDING', 1); |
| 55 | + |
| 56 | + if (!letters?.length) { |
| 57 | + test.fail(true, `No PENDING letters found for supplier ${supplierId}`); |
| 58 | + return; |
| 59 | + } |
| 60 | + const letter = letters[0]; |
| 61 | + const headers = await patchRequestHeaders(); |
| 62 | + const body = await patchFailureRequestBody(letter.id, 'REJECTED'); |
| 63 | + |
| 64 | + const response = await request.patch(`${baseUrl}/${SUPPLIER_LETTERS}/${letter.id}`, { |
| 65 | + headers: headers, |
| 66 | + data: body |
| 67 | + }); |
| 68 | + |
| 69 | + const res = await response.json(); |
| 70 | + expect(response.status()).toBe(200); |
| 71 | + await deleteLettersBySupplier(letter.id); |
| 72 | + }); |
| 73 | + |
| 74 | + test(`Patch /letters returns 400 if request Body is invalid`, async ({ request }) => { |
| 75 | + |
| 76 | + const id = randomUUID() |
| 77 | + const headers = await patchRequestHeaders(); |
| 78 | + const body = await patchValidRequestBody(id, ''); |
| 79 | + |
| 80 | + const response = await request.patch(`${baseUrl}/${SUPPLIER_LETTERS}/${id}`, { |
| 81 | + headers: headers, |
| 82 | + data: body |
| 83 | + }); |
| 84 | + |
| 85 | + const res = await response.json(); |
| 86 | + |
| 87 | + expect(response.status()).toBe(400); |
| 88 | + }); |
| 89 | + |
| 90 | + test(`Patch /letters returns 500 if Id doesn't exist for SupplierId`, async ({ request }) => { |
| 91 | + const headers = await patchRequestHeaders(); |
| 92 | + const id = randomUUID() |
| 93 | + const body = await patchValidRequestBody(id, 'PENDING'); |
| 94 | + |
| 95 | + const response = await request.patch(`${baseUrl}/${SUPPLIER_LETTERS}/${id}`, { |
| 96 | + headers: headers, |
| 97 | + data: body |
| 98 | + }); |
| 99 | + |
| 100 | + const res = await response.json(); |
| 101 | + expect(response.status()).toBe(500); |
| 102 | + }); |
| 103 | + |
| 104 | + test(`Patch /letters returns 403 for invalid headers`, async ({ request }) => { |
| 105 | + const headers = await createInvalidRequestHeaders(); |
| 106 | + const id = randomUUID() |
| 107 | + const body = await patchValidRequestBody(id, 'PENDING'); |
| 108 | + |
| 109 | + const response = await request.patch(`${baseUrl}/${SUPPLIER_LETTERS}/${id}`, { |
| 110 | + headers: headers, |
| 111 | + data: body |
| 112 | + }); |
| 113 | + |
| 114 | + const res = await response.json(); |
| 115 | + console.log(res); |
| 116 | + expect(response.status()).toBe(403); |
| 117 | + }); |
28 | 118 | }); |
0 commit comments