Skip to content

Commit 95a0396

Browse files
tests
1 parent 95c9408 commit 95a0396

File tree

10 files changed

+332
-21
lines changed

10 files changed

+332
-21
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import {test, expect} from '@playwright/test';
2+
import { getRestApiGatewayBaseUrl } from "../../helpers/awsGatewayHelper";
3+
import { MI_ENDPOINT } from '../../constants/api_constants';
4+
import { createHeaderWithNoCorrelationId, createHeaderWithNoRequestId, createInvalidRequestHeaders, createValidRequestHeaders } from '../../constants/request_headers';
5+
import { miInValidRequest, miValidRequest } from './testCases/createMi';
6+
import { time } from 'console';
7+
import { error400ResponseBody, error404ResponseBody, RequestId500Error } from '../../helpers/commonTypes';
8+
9+
let baseUrl: string;
10+
11+
test.beforeAll(async () => {
12+
baseUrl = await getRestApiGatewayBaseUrl();
13+
});
14+
15+
test.describe('API Gateway Tests to Verify Mi Endpoint', () => {
16+
test(`Post /mi returns 200 when a valid request is passed`, async ({ request }) => {
17+
18+
const headers = createValidRequestHeaders();
19+
const body = miValidRequest();
20+
21+
const response = await request.post(`${baseUrl}/${MI_ENDPOINT}`, {
22+
headers: headers,
23+
data: body
24+
});
25+
26+
const res = await response.json();
27+
expect(response.status()).toBe(201);
28+
expect(res.data.attributes).toMatchObject({
29+
groupId: 'group123',
30+
lineItem: 'envelope-business-standard',
31+
quantity: 10,
32+
specificationId: 'Test-Spec-Id',
33+
stockRemaining: 100,
34+
timestamp: body.data.attributes.timestamp,
35+
});
36+
expect(res.data.type).toBe('ManagementInformation');
37+
});
38+
39+
test(`Post /mi returns 400 when a invalid request is passed`, async ({ request }) => {
40+
const headers = createValidRequestHeaders();
41+
const body = miInValidRequest();
42+
43+
const response = await request.post(`${baseUrl}/${MI_ENDPOINT}`, {
44+
headers: headers,
45+
data: body
46+
});
47+
48+
const res = await response.json();
49+
expect(response.status()).toBe(400);
50+
expect(res).toMatchObject(error400ResponseBody());
51+
});
52+
53+
test(`Post /mi returns 403 when a invalid request is passed`, async ({ request }) => {
54+
const headers = createInvalidRequestHeaders();
55+
const body = miValidRequest();
56+
57+
const response = await request.post(`${baseUrl}/${MI_ENDPOINT}`, {
58+
headers: headers,
59+
data: body
60+
});
61+
62+
const res = await response.json();
63+
expect(response.status()).toBe(403);
64+
expect(res).toMatchObject({
65+
Message : 'User is not authorized to access this resource with an explicit deny in an identity-based policy' }
66+
);
67+
});
68+
69+
test(`Post /mi returns 500 when a correlationId is not passed`, async ({ request }) => {
70+
const headers = createHeaderWithNoCorrelationId();
71+
const body = miValidRequest();
72+
73+
const response = await request.post(`${baseUrl}/${MI_ENDPOINT}`, {
74+
headers: headers,
75+
data: body
76+
});
77+
78+
const res = await response.json();
79+
expect(response.status()).toBe(500);
80+
expect(res.errors[0].detail).toBe("The request headers don't contain the APIM correlation id");
81+
});
82+
83+
test(`Post /mi returns 500 when a x-request-id is not passed`, async ({ request }) => {
84+
const headers = createHeaderWithNoRequestId();
85+
const body = miValidRequest();
86+
87+
const response = await request.post(`${baseUrl}/${MI_ENDPOINT}`, {
88+
headers: headers,
89+
data: body
90+
});
91+
92+
const res = await response.json();
93+
expect(response.status()).toBe(500);
94+
expect(res).toMatchObject(RequestId500Error());
95+
});
96+
97+
98+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { test, expect } from '@playwright/test';
2+
import { getRestApiGatewayBaseUrl } from '../../helpers/awsGatewayHelper';
3+
import { getLettersBySupplier } from '../../helpers/generate_fetch_testData';
4+
import { SUPPLIER_LETTERS, SUPPLIERID } from '../../constants/api_constants';
5+
import { createValidRequestHeaders } from '../../constants/request_headers';
6+
import { error404ResponseBody, error500ResponseBody } from '../../helpers/commonTypes';
7+
8+
let baseUrl: string;
9+
10+
test.beforeAll(async () => {
11+
baseUrl = await getRestApiGatewayBaseUrl();
12+
});
13+
14+
test.describe('API Gateway Tests to Verify Get Letter Status Endpoint', () => {
15+
test(`Get /letters/{id} returns 200 and valid response for a given id`, async ({ request }) => {
16+
17+
const letters = await getLettersBySupplier(SUPPLIERID, 'PENDING', 1);
18+
19+
if (!letters?.length) {
20+
test.fail(true, `No PENDING letters found for supplier ${SUPPLIERID}`);
21+
return;
22+
}
23+
const letter = letters[0];
24+
const headers = createValidRequestHeaders();
25+
const response = await request.get(`${baseUrl}/${SUPPLIER_LETTERS}/${letter.id}`, {
26+
headers: headers,
27+
});
28+
29+
const res = await response.json();
30+
31+
expect(response.status()).toBe(200);
32+
expect(res).toMatchObject({
33+
data:{
34+
attributes: {
35+
status: 'PENDING',
36+
specificationId: letter.specificationId,
37+
groupId: letter.groupId,
38+
},
39+
id: letter.id,
40+
type: 'Letter'
41+
}
42+
});
43+
});
44+
45+
test(`Get /letters/{id} returns 404 if no resource is found for id`, async ({ request }) =>
46+
{
47+
const id = '11';
48+
const headers = createValidRequestHeaders();
49+
const response = await request.get(`${baseUrl}/${SUPPLIER_LETTERS}/${id}`, {
50+
headers: headers,
51+
});
52+
53+
const res = await response.json();
54+
expect(response.status()).toBe(404);
55+
expect(res).toMatchObject(error404ResponseBody());
56+
});
57+
58+
test(`Get /letters/{id} returns 500 if letter is not found for supplierId ${SUPPLIERID}`, async ({ request }) =>
59+
{
60+
const id = 'non-existing-id-12345';
61+
const headers = createValidRequestHeaders();
62+
const response = await request.get(`${baseUrl}/${SUPPLIER_LETTERS}/${id}`, {
63+
headers: headers,
64+
});
65+
66+
const res = await response.json();
67+
expect(response.status()).toBe(500);
68+
expect(res).toMatchObject(error500ResponseBody(id));
69+
});
70+
71+
});

tests/component-tests/apiGateway-tests/getLetters.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { SUPPLIER_LETTERS } from '../../constants/api_constants';
33
import { createHeaderWithNoCorrelationId, createInvalidRequestHeaders, createValidRequestHeaders } from '../../constants/request_headers';
44
import { getRestApiGatewayBaseUrl } from '../../helpers/awsGatewayHelper';
55
import { validateApiResponse } from '../../helpers/validateJsonSchema';
6-
import { link } from 'fs';
76

87
let baseUrl: string;
98

tests/component-tests/apiGateway-tests/testCases/UpdateLetterStatus.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { RequestHeaders } from '../../../constants/request_headers';
3-
import { supplierId } from '../../../constants/api_constants';
3+
import { SUPPLIERID } from '../../../constants/api_constants';
44
import { ErrorMessageBody } from '../../../helpers/commonTypes';
55

66
export type PatchMessageRequestBody = {
@@ -33,7 +33,7 @@ export function patchRequestHeaders(): RequestHeaders {
3333
let requestHeaders: RequestHeaders;
3434
requestHeaders = {
3535
headerauth1: process.env.HEADERAUTH || '',
36-
'NHSD-Supplier-ID': supplierId,
36+
'NHSD-Supplier-ID': SUPPLIERID,
3737
'NHSD-Correlation-ID': '12344',
3838
'X-Request-ID': 'requestId1'
3939
};
@@ -106,7 +106,7 @@ export function patch500ErrorResponseBody (id: string) : ErrorMessageBody{
106106
},
107107
status: "500",
108108
title: "Internal server error",
109-
detail: `Letter with id ${id} not found for supplier ${supplierId}`
109+
detail: `Letter with id ${id} not found for supplier ${SUPPLIERID}`
110110
}
111111
]
112112
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
3+
4+
export type MiRequestBody = {
5+
data: {
6+
type: string;
7+
attributes: {
8+
groupId: string;
9+
lineItem: string;
10+
quantity: number;
11+
specificationId: string;
12+
stockRemaining: number;
13+
timestamp: string;
14+
};
15+
};
16+
};
17+
18+
export function miValidRequest() : MiRequestBody{
19+
let requestBody: MiRequestBody;
20+
21+
requestBody = {
22+
data: {
23+
 attributes: {
24+
groupId: 'group123',
25+
lineItem: 'envelope-business-standard',
26+
quantity: 10,
27+
specificationId: 'Test-Spec-Id',
28+
stockRemaining: 100,
29+
timestamp: new Date().toISOString(),
30+
},
31+
type: 'ManagementInformation',
32+
}};
33+
return requestBody;
34+
}
35+
36+
export function miInValidRequest() : MiRequestBody{
37+
let requestBody: MiRequestBody;
38+
39+
requestBody = {
40+
data: {
41+
 attributes: {
42+
groupId: 'group123',
43+
lineItem: 'envelope-business-standard',
44+
quantity: 10,
45+
specificationId: 'Test-Spec-Id',
46+
stockRemaining: 100,
47+
timestamp: new Date().toISOString(),
48+
},
49+
type: '?',
50+
}};
51+
return requestBody;
52+
}

tests/component-tests/apiGateway-tests/updateLetterStatus.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect } from '@playwright/test';
2-
import { SUPPLIER_LETTERS, supplierId } from '../../constants/api_constants';
2+
import { SUPPLIER_LETTERS, SUPPLIERID } from '../../constants/api_constants';
33
import { getRestApiGatewayBaseUrl } from '../../helpers/awsGatewayHelper';
44
import { patch400ErrorResponseBody, patch500ErrorResponseBody, patchFailureRequestBody, patchRequestHeaders, patchValidRequestBody } from './testCases/UpdateLetterStatus';
55
import { createTestData, deleteLettersBySupplier, getLettersBySupplier } from '../../helpers/generate_fetch_testData';
@@ -15,11 +15,11 @@ test.beforeAll(async () => {
1515
test.describe('API Gateway Tests to Verify Patch Status Endpoint', () => {
1616
test(`Patch /letters returns 200 and status is updated to ACCEPTED`, async ({ request }) => {
1717

18-
await createTestData(supplierId);
19-
const letters = await getLettersBySupplier(supplierId, 'PENDING', 1);
18+
await createTestData(SUPPLIERID);
19+
const letters = await getLettersBySupplier(SUPPLIERID, 'PENDING', 1);
2020

2121
if (!letters?.length) {
22-
test.fail(true, `No PENDING letters found for supplier ${supplierId}`);
22+
test.fail(true, `No PENDING letters found for supplier ${SUPPLIERID}`);
2323
return;
2424
}
2525
const letter = letters[0];
@@ -50,11 +50,11 @@ test.describe('API Gateway Tests to Verify Patch Status Endpoint', () => {
5050

5151
test(`Patch /letters returns 200 and status is updated to REJECTED`, async ({ request }) => {
5252

53-
await createTestData(supplierId);
54-
const letters = await getLettersBySupplier(supplierId, 'PENDING', 1);
53+
await createTestData(SUPPLIERID);
54+
const letters = await getLettersBySupplier(SUPPLIERID, 'PENDING', 1);
5555

5656
if (!letters?.length) {
57-
test.fail(true, `No PENDING letters found for supplier ${supplierId}`);
57+
test.fail(true, `No PENDING letters found for supplier ${SUPPLIERID}`);
5858
return;
5959
}
6060
const letter = letters[0];

tests/constants/api_constants.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
export const SUPPLIER_LETTERS = 'letters';
22
export const SUPPLIER_API_URL_SANDBOX = 'https://internal-dev-sandbox.api.service.nhs.uk/nhs-notify-supplier';
33
export const AWS_REGION = 'eu-west-2';
4-
54
export const envName = process.env.PR_NUMBER ?? 'main';
65
export const API_NAME = `nhs-${envName}-supapi`;
76
export const LETTERSTABLENAME = `nhs-${envName}-supapi-letters`;
8-
9-
// Test
10-
export const supplierId = 'TestSupplier1';
7+
export const SUPPLIERID = 'TestSupplier1';
8+
export const MI_ENDPOINT = 'mi';

tests/constants/request_headers.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { randomUUID } from 'node:crypto';
2-
import { supplierId } from './api_constants';
2+
import { SUPPLIERID } from './api_constants';
33

44
export const sandBoxHeader: RequestSandBoxHeaders = {
55
'X-Request-ID': randomUUID(),
@@ -25,7 +25,7 @@ export function createInvalidRequestHeaders(): RequestHeaders {
2525
let requestHeaders: RequestHeaders;
2626
requestHeaders = {
2727
headerauth1: '',
28-
'NHSD-Supplier-ID': supplierId,
28+
'NHSD-Supplier-ID': SUPPLIERID,
2929
'NHSD-Correlation-ID': '1234',
3030
'X-Request-ID': 'requestId1'
3131
};
@@ -36,7 +36,7 @@ export function createHeaderWithNoCorrelationId(): RequestHeaders {
3636
let requestHeaders: RequestHeaders;
3737
requestHeaders = {
3838
headerauth1: process.env.HEADERAUTH || '',
39-
'NHSD-Supplier-ID': supplierId,
39+
'NHSD-Supplier-ID': SUPPLIERID,
4040
'NHSD-Correlation-ID': '',
4141
'X-Request-ID': 'requestId1'
4242
};
@@ -47,9 +47,20 @@ export function createValidRequestHeaders(): RequestHeaders{
4747
let requestHeaders: RequestHeaders;
4848
requestHeaders = {
4949
headerauth1: process.env.HEADERAUTH || '',
50-
'NHSD-Supplier-ID': supplierId,
50+
'NHSD-Supplier-ID': SUPPLIERID,
5151
'NHSD-Correlation-ID': '12345',
5252
'X-Request-ID': 'requestId1'
5353
};
5454
return requestHeaders;
5555
}
56+
57+
export function createHeaderWithNoRequestId(): RequestHeaders {
58+
let requestHeaders: RequestHeaders;
59+
requestHeaders = {
60+
headerauth1: process.env.HEADERAUTH || '',
61+
'NHSD-Supplier-ID': SUPPLIERID,
62+
'NHSD-Correlation-ID': '1234',
63+
'X-Request-ID': ''
64+
};
65+
return requestHeaders;
66+
}

0 commit comments

Comments
 (0)