|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { SUPPLIER_LETTERS, supplierId } from '../../constants/api_constants'; |
| 3 | +import { getRestApiGatewayBaseUrl } from '../../helpers/awsGatewayHelper'; |
| 4 | +import { patch400ErrorResponseBody, patch500ErrorResponseBody, 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'; |
| 8 | + |
| 9 | +let baseUrl: string; |
| 10 | + |
| 11 | +test.beforeAll(async () => { |
| 12 | + baseUrl = await getRestApiGatewayBaseUrl(); |
| 13 | +}); |
| 14 | + |
| 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 = patchRequestHeaders(); |
| 27 | + const body = patchValidRequestBody(letter.id, 'ACCEPTED'); |
| 28 | + |
| 29 | + const response = await request.patch(`${baseUrl}/${SUPPLIER_LETTERS}/${letter.id}`, { |
| 30 | + headers: headers, |
| 31 | + data: body |
| 32 | + }); |
| 33 | + |
| 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: letter.specificationId, |
| 41 | + groupId: letter.groupId, |
| 42 | + }, |
| 43 | + id: letter.id, |
| 44 | + type: 'Letter' |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + await deleteLettersBySupplier(letter.id); |
| 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 = patchRequestHeaders(); |
| 62 | + const body = 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 | + expect(res).toMatchObject({ |
| 72 | + data:{ |
| 73 | + attributes: { |
| 74 | + status: 'REJECTED', |
| 75 | + specificationId: letter.specificationId, |
| 76 | + groupId: letter.groupId, |
| 77 | + }, |
| 78 | + id: letter.id, |
| 79 | + type: 'Letter' |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + await deleteLettersBySupplier(letter.id); |
| 84 | + }); |
| 85 | + |
| 86 | + test(`Patch /letters returns 400 if request Body is invalid`, async ({ request }) => { |
| 87 | + |
| 88 | + const id = randomUUID() |
| 89 | + const headers = patchRequestHeaders(); |
| 90 | + const body = patchValidRequestBody(id, ''); |
| 91 | + |
| 92 | + const response = await request.patch(`${baseUrl}/${SUPPLIER_LETTERS}/${id}`, { |
| 93 | + headers: headers, |
| 94 | + data: body |
| 95 | + }); |
| 96 | + |
| 97 | + const res = await response.json(); |
| 98 | + |
| 99 | + expect(response.status()).toBe(400); |
| 100 | + expect(res).toMatchObject(patch400ErrorResponseBody()); |
| 101 | + }); |
| 102 | + |
| 103 | + test(`Patch /letters returns 500 if Id doesn't exist for SupplierId`, async ({ request }) => { |
| 104 | + const headers = patchRequestHeaders(); |
| 105 | + const id = randomUUID() |
| 106 | + const body = patchValidRequestBody(id, 'PENDING'); |
| 107 | + |
| 108 | + const response = await request.patch(`${baseUrl}/${SUPPLIER_LETTERS}/${id}`, { |
| 109 | + headers: headers, |
| 110 | + data: body |
| 111 | + }); |
| 112 | + |
| 113 | + const res = await response.json(); |
| 114 | + expect(response.status()).toBe(500); |
| 115 | + expect(res).toMatchObject(patch500ErrorResponseBody(id)); |
| 116 | + }); |
| 117 | + |
| 118 | + test(`Patch /letters returns 403 for invalid headers`, async ({ request }) => { |
| 119 | + const headers = await createInvalidRequestHeaders(); |
| 120 | + const id = randomUUID() |
| 121 | + const body = patchValidRequestBody(id, 'PENDING'); |
| 122 | + |
| 123 | + const response = await request.patch(`${baseUrl}/${SUPPLIER_LETTERS}/${id}`, { |
| 124 | + headers: headers, |
| 125 | + data: body |
| 126 | + }); |
| 127 | + |
| 128 | + const res = await response.json(); |
| 129 | + expect(response.status()).toBe(403); |
| 130 | + }); |
| 131 | +}); |
0 commit comments