Skip to content

Commit aa08f80

Browse files
tests
1 parent e4ac798 commit aa08f80

File tree

11 files changed

+335
-188
lines changed

11 files changed

+335
-188
lines changed

.github/workflows/stage-4-acceptance.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ jobs:
116116
steps:
117117
- name: "Checkout code"
118118
uses: actions/checkout@v4
119+
- name: "Set API Suffix"
120+
run: |
121+
echo "API_SUFFIX=${{ inputs.pr_number }}" >> $GITHUB_ENV
119122
- name: "Run integration test"
120123
run: |
121124
make test-component

scripts/test-data/src/helpers/create_letter_helpers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export async function createLetter(params: {
4646

4747
const letterRecord = await letterRepository.putLetter(letter);
4848
console.log(letterRecord);
49+
return letterRecord;
4950
}
5051

5152
export function createLetterDto(params: {
@@ -75,6 +76,4 @@ export function createLetterDto(params: {
7576
createdAt: new Date().toISOString(),
7677
updatedAt: new Date().toISOString(),
7778
};
78-
79-
return letter;
8079
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect } from '@playwright/test';
2-
import { SUPPLIER_API_GATEWAY_NAME, SUPPLIER_LETTERS, AWS_REGION } from '../../constants/api_constants';
2+
import { SUPPLIER_LETTERS, AWS_REGION } from '../../constants/api_constants';
33
import { createHeaderWithNoCorrelationId, createInvalidRequestHeaders, createValidRequestHeaders } from '../../constants/request_headers';
44
import { validateApiResponse } from '../../helpers/validateJsonSchema';
55
import { getRestApiGatewayBaseUrl } from '../../helpers/awsGatewayHelper';
@@ -8,7 +8,7 @@ let baseUrl: string;
88

99
test.beforeAll(async () => {
1010
const region = AWS_REGION;
11-
baseUrl = await getRestApiGatewayBaseUrl(SUPPLIER_API_GATEWAY_NAME, region);
11+
baseUrl = await getRestApiGatewayBaseUrl();
1212
});
1313

1414
test.describe('API Gateway Tests To Get List Of Pending ', () =>
Lines changed: 38 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11

2-
import { createValidRequestHeaders, RequestHeaders } from '../../../constants/request_headers';
3-
import { test } from '@playwright/test';
4-
5-
type APIPatchMessageRequestTestCase = {
6-
testCase: string;
7-
id: string,
8-
body?: PatchMessageRequestBody;
9-
expectedStatus: number;
10-
expectedResponse?: PatchMessageResponseBody | PatchErrorMessageBody;
11-
};
2+
import { RequestHeaders } from '../../../constants/request_headers';
3+
import { supplierId } from '../../../constants/api_constants';
124

135
export type PatchMessageRequestBody = {
146
data: {
@@ -36,137 +28,48 @@ export type PatchMessageResponseBody = {
3628
};
3729
};
3830

39-
export type ErrorLink = {
40-
about: string;
31+
export async function patchRequestHeaders(): Promise<RequestHeaders> {
32+
let requestHeaders: RequestHeaders;
33+
requestHeaders = {
34+
headerauth1: process.env.HEADERAUTH || '',
35+
'NHSD-Supplier-ID': supplierId,
36+
'NHSD-Correlation-ID': '12344',
37+
};
38+
return requestHeaders;
4139
};
4240

43-
type PatchErrorResponse = {
44-
id: string;
45-
code: string;
46-
links: ErrorLink;
47-
status: string;
48-
title: string;
49-
detail: string;
50-
};
5141

52-
export type PatchErrorMessageBody = {
53-
errors: PatchErrorResponse[];
54-
};
42+
export async function patchValidRequestBody (id: string, status: string) : Promise<PatchMessageRequestBody>{
43+
let requestBody: PatchMessageRequestBody;
5544

45+
requestBody = {
46+
data: {
47+
 attributes: {
48+
  status: status,
49+
},
50+
type: 'Letter',
51+
id: id
52+
}
5653

54+
};
55+
return requestBody;
56+
}
5757

58-
export const apiPatchMessageRequestTestData: APIPatchMessageRequestTestCase[] = [
59-
{
60-
testCase: '200 response if record is updated with status REJECTED',
61-
id: '00c61654-24f0-410e-a77e-04deef7d1eeb',
62-
body: {
63-
data: {
64-
type: 'Letter',
65-
id: '00c61654-24f0-410e-a77e-04deef7d1eeb',
66-
attributes: {
67-
reasonCode: 123,
68-
reasonText: 'Test Reason Text',
69-
status: 'REJECTED',
70-
},
71-
}
72-
},
73-
expectedStatus: 200,
74-
expectedResponse: {
75-
data: {
76-
type: 'Letter',
77-
id: '00c61654-24f0-410e-a77e-04deef7d1eeb',
78-
attributes: {
79-
reasonCode: 123,
80-
reasonText: 'Test Reason Text',
81-
status: 'REJECTED',
82-
specificationId:'specification-id',
83-
groupId:'group-id'
84-
},
85-
}
86-
},
87-
},
58+
export async function patchFailureRequestBody (id: string, status: string) : Promise<PatchMessageRequestBody>{
59+
let requestBody: PatchMessageRequestBody;
8860

89-
{
90-
testCase: '200 response if record is updated with status ACCEPTED',
91-
id: '00c61654-24f0-410e-a77e-04deef7d1eeb',
92-
body: {
93-
data: {
94-
type: 'Letter',
95-
id: '00c61654-24f0-410e-a77e-04deef7d1eeb',
96-
attributes: {
97-
status: 'ACCEPTED',
98-
},
99-
}
100-
},
101-
expectedStatus: 200,
102-
expectedResponse: {
103-
data: {
104-
type: 'Letter',
105-
id: '00c61654-24f0-410e-a77e-04deef7d1eeb',
106-
attributes: {
107-
reasonCode: 123,
108-
reasonText: 'Test Reason Text',
109-
status: 'ACCEPTED',
110-
specificationId:'specification-id',
111-
groupId:'group-id'
112-
},
113-
}
114-
},
115-
},
61+
requestBody = {
62+
data: {
63+
 attributes: {
64+
  status: status,
65+
reasonCode: 123,
66+
reasonText: 'Test Reason'
11667

117-
{
118-
testCase: '400 response if request body is invalid',
119-
id: '00c61654-24f0-410e-a77e-04deef7d1eeb',
120-
body: {
121-
data: {
122-
type: 'Letter',
123-
id: '00c61654-24f0-410e-a77e-04deef7d1eeb',
124-
attributes: {
125-
reasonCode: 123,
126-
reasonText: 'Test Reason Text',
127-
status: '',
128-
},
129-
}
130-
},
131-
expectedStatus: 400,
132-
expectedResponse: {
133-
errors: [{
134-
id: '1234',
135-
code: 'NOTIFY_INVALID_REQUEST',
136-
links: {
137-
about: "https://digital.nhs.uk/developer/api-catalogue/nhs-notify-supplier"
138-
},
139-
status: '400',
140-
title: 'Invalid request',
141-
detail: 'The request body is invalid'
142-
}]
143-
},
144-
},
68+
},
69+
type: 'Letter',
70+
id: id
71+
}
14572

146-
{
147-
testCase: '500 response if id doesnt exist supplierid',
148-
id: '0',
149-
body: {
150-
data: {
151-
type: 'Letter',
152-
id: '0',
153-
attributes: {
154-
status: 'ACCEPTED',
155-
},
156-
}
157-
},
158-
expectedStatus: 500,
159-
expectedResponse: {
160-
errors: [{
161-
id: '1234',
162-
code: 'NOTIFY_INTERNAL_SERVER_ERROR',
163-
links: {
164-
about: "https://digital.nhs.uk/developer/api-catalogue/nhs-notify-supplier"
165-
},
166-
status: '500',
167-
title: 'Internal server error',
168-
detail: 'Letter with id 0 not found for supplier supplier-id'
169-
}]
170-
},
171-
},
172-
];
73+
};
74+
return requestBody;
75+
}
Lines changed: 105 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,118 @@
11
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';
33
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';
68

79
let baseUrl: string;
810

911
test.beforeAll(async () => {
10-
const region = AWS_REGION;
11-
baseUrl = await getRestApiGatewayBaseUrl(SUPPLIER_API_GATEWAY_NAME, region);
12+
baseUrl = await getRestApiGatewayBaseUrl();
1213
});
1314

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,
1931
data: body
20-
},
21-
);
22-
const res = await response.json();
32+
});
2333

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+
}
2646
});
47+
48+
await deleteLettersBySupplier(letter.id);
2749
});
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+
});
28118
});

tests/config/playwright.base.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineConfig, PlaywrightTestConfig } from '@playwright/test';
2+
import 'dotenv/config';
23

34
const baseUrl = process.env.NHSD_APIM_PROXY_URL || 'http://localhost:3000/';
45
const envMaxInstances = Number.parseInt(process.env.WORKERS_MAX_INST!) || 10;

tests/constants/api_constants.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
export const SUPPLIER_API_GATEWAY_NAME = 'nhs-main-supapi';
21
export const SUPPLIER_LETTERS = 'letters';
32
export const SUPPLIER_API_URL = 'https://internal-dev.api.service.nhs.uk/nhs-notify-supplier/';
43
export const SUPPLIER_API_URL_SANDBOX = 'https://internal-dev-sandbox.api.service.nhs.uk/nhs-notify-supplier';
54
export const AWS_REGION = 'eu-west-2';
5+
6+
export const envName = process.env.PR_NUMBER ?? 'main';
7+
export const API_NAME = `nhs-${envName}-supapi`;
8+
export const LETTERSTABLENAME = `nhs-${envName}-supapi-letters`;
9+
10+
// Test
11+
export const supplierId = 'TestSupplier1';

0 commit comments

Comments
 (0)