Skip to content

Commit d05a624

Browse files
CCM- 11597: Tests for getLetters and Mi Endpoint (#224)
* tests * ignore errors * review --------- Co-authored-by: Tim Ireland <[email protected]>
1 parent 65c71d8 commit d05a624

File tree

10 files changed

+401
-32
lines changed

10 files changed

+401
-32
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 { miInvalidDateRequest, miInvalidRequest, miValidRequest } from './testCases/createMi';
6+
import { time } from 'console';
7+
import { error400InvalidDate, error400ResponseBody, error403ResponseBody, 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 responseBody = await response.json();
27+
expect(response.status()).toBe(201);
28+
expect(responseBody.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(responseBody.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 responseBody = await response.json();
49+
expect(response.status()).toBe(400);
50+
expect(responseBody).toMatchObject(error400ResponseBody());
51+
});
52+
53+
test(`Post /mi returns 403 when a authentication header is not 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 responseBody = await response.json();
63+
expect(response.status()).toBe(403);
64+
expect(responseBody).toMatchObject(error403ResponseBody());
65+
});
66+
67+
test(`Post /mi returns 500 when a correlationId is not passed`, async ({ request }) => {
68+
const headers = createHeaderWithNoCorrelationId();
69+
const body = miValidRequest();
70+
71+
const response = await request.post(`${baseUrl}/${MI_ENDPOINT}`, {
72+
headers: headers,
73+
data: body
74+
});
75+
76+
const res = await response.json();
77+
expect(response.status()).toBe(500);
78+
expect(res.errors[0].detail).toBe("The request headers don't contain the APIM correlation id");
79+
});
80+
81+
test(`Post /mi returns 500 when a x-request-id is not passed`, async ({ request }) => {
82+
const headers = createHeaderWithNoRequestId();
83+
const body = miValidRequest();
84+
85+
const response = await request.post(`${baseUrl}/${MI_ENDPOINT}`, {
86+
headers: headers,
87+
data: body
88+
});
89+
90+
const responseBody = await response.json();
91+
expect(response.status()).toBe(500);
92+
expect(responseBody).toMatchObject(requestId500Error());
93+
});
94+
95+
test(`Post /mi returns 400 when a invalid Date is passed`, async ({ request }) => {
96+
const headers = createValidRequestHeaders();
97+
const body = miInvalidDateRequest();
98+
99+
const response = await request.post(`${baseUrl}/${MI_ENDPOINT}`, {
100+
headers: headers,
101+
data: body
102+
});
103+
104+
const responseBody = await response.json();
105+
expect(response.status()).toBe(400);
106+
expect(responseBody).toMatchObject(error400InvalidDate());
107+
});
108+
109+
110+
});
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 responseBody = await response.json();
30+
31+
expect(response.status()).toBe(200);
32+
expect(responseBody).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 responseBody = await response.json();
54+
expect(response.status()).toBe(404);
55+
expect(responseBody).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 responseBody = await response.json();
67+
expect(response.status()).toBe(500);
68+
expect(responseBody).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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}
53+
54+
export function miInvalidDateRequest() : MiRequestBody{
55+
let requestBody: MiRequestBody;
56+
57+
requestBody = {
58+
data: {
59+
 attributes: {
60+
groupId: 'group123',
61+
lineItem: 'envelope-business-standard',
62+
quantity: 10,
63+
specificationId: 'Test-Spec-Id',
64+
stockRemaining: 100,
65+
timestamp: '2021-10-28T',
66+
},
67+
type: 'ManagementInformation',
68+
}};
69+
return requestBody;
70+
}

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
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';
4-
import { patch400ErrorResponseBody, patch500ErrorResponseBody, patchFailureRequestBody, patchRequestHeaders, patchValidRequestBody } from './testCases/UpdateLetterStatus';
4+
import { patch400ErrorResponseBody, patch500ErrorResponseBody, patchFailureRequestBody, patchRequestHeaders, patchValidRequestBody } from './testCases/updateLetterStatus';
55
import { createTestData, deleteLettersBySupplier, getLettersBySupplier } from '../../helpers/generate_fetch_testData';
66
import { randomUUID } from 'crypto';
77
import { createInvalidRequestHeaders } from '../../constants/request_headers';
8+
import { error403ResponseBody } from '../../helpers/commonTypes';
89

910
let baseUrl: string;
1011

@@ -15,11 +16,11 @@ test.beforeAll(async () => {
1516
test.describe('API Gateway Tests to Verify Patch Status Endpoint', () => {
1617
test(`Patch /letters returns 200 and status is updated to ACCEPTED`, async ({ request }) => {
1718

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

2122
if (!letters?.length) {
22-
test.fail(true, `No PENDING letters found for supplier ${supplierId}`);
23+
test.fail(true, `No PENDING letters found for supplier ${SUPPLIERID}`);
2324
return;
2425
}
2526
const letter = letters[0];
@@ -31,9 +32,9 @@ test.describe('API Gateway Tests to Verify Patch Status Endpoint', () => {
3132
data: body
3233
});
3334

34-
const res = await response.json();
35+
const responseBody = await response.json();
3536
expect(response.status()).toBe(200);
36-
expect(res).toMatchObject({
37+
expect(responseBody).toMatchObject({
3738
data:{
3839
attributes: {
3940
status: 'ACCEPTED',
@@ -50,11 +51,11 @@ test.describe('API Gateway Tests to Verify Patch Status Endpoint', () => {
5051

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

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

5657
if (!letters?.length) {
57-
test.fail(true, `No PENDING letters found for supplier ${supplierId}`);
58+
test.fail(true, `No PENDING letters found for supplier ${SUPPLIERID}`);
5859
return;
5960
}
6061
const letter = letters[0];
@@ -66,9 +67,9 @@ test.describe('API Gateway Tests to Verify Patch Status Endpoint', () => {
6667
data: body
6768
});
6869

69-
const res = await response.json();
70+
const responseBody = await response.json();
7071
expect(response.status()).toBe(200);
71-
expect(res).toMatchObject({
72+
expect(responseBody).toMatchObject({
7273
data:{
7374
attributes: {
7475
status: 'REJECTED',
@@ -94,10 +95,10 @@ test.describe('API Gateway Tests to Verify Patch Status Endpoint', () => {
9495
data: body
9596
});
9697

97-
const res = await response.json();
98+
const responseBody = await response.json();
9899

99100
expect(response.status()).toBe(400);
100-
expect(res).toMatchObject(patch400ErrorResponseBody());
101+
expect(responseBody).toMatchObject(patch400ErrorResponseBody());
101102
});
102103

103104
test(`Patch /letters returns 500 if Id doesn't exist for SupplierId`, async ({ request }) => {
@@ -110,13 +111,13 @@ test.describe('API Gateway Tests to Verify Patch Status Endpoint', () => {
110111
data: body
111112
});
112113

113-
const res = await response.json();
114+
const responseBody = await response.json();
114115
expect(response.status()).toBe(500);
115-
expect(res).toMatchObject(patch500ErrorResponseBody(id));
116+
expect(responseBody).toMatchObject(patch500ErrorResponseBody(id));
116117
});
117118

118119
test(`Patch /letters returns 403 for invalid headers`, async ({ request }) => {
119-
const headers = await createInvalidRequestHeaders();
120+
const headers = createInvalidRequestHeaders();
120121
const id = randomUUID()
121122
const body = patchValidRequestBody(id, 'PENDING');
122123

@@ -125,7 +126,8 @@ test.describe('API Gateway Tests to Verify Patch Status Endpoint', () => {
125126
data: body
126127
});
127128

128-
const res = await response.json();
129+
const responseBody = await response.json();
129130
expect(response.status()).toBe(403);
131+
expect(responseBody).toMatchObject(error403ResponseBody());
130132
});
131133
});

0 commit comments

Comments
 (0)