Skip to content

Commit df1261c

Browse files
checks for supplier data
1 parent 1adba99 commit df1261c

File tree

8 files changed

+143
-30
lines changed

8 files changed

+143
-30
lines changed

tests/component-tests/apiGateway-tests/get-letter-status.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ let baseUrl: string;
1212

1313
test.beforeAll(async () => {
1414
baseUrl = await getRestApiGatewayBaseUrl();
15-
console.log(`Base URL for API Gateway: ${baseUrl}`);
1615
});
1716

1817
test.describe("API Gateway Tests to Verify Get Letter Status Endpoint", () => {
@@ -75,6 +74,6 @@ test.describe("API Gateway Tests to Verify Get Letter Status Endpoint", () => {
7574

7675
const responseBody = await response.json();
7776
expect(response.status()).toBe(500);
78-
expect(responseBody).toMatchObject(error500ResponseBody(id));
77+
expect(responseBody).toMatchObject(error500ResponseBody());
7978
});
8079
});

tests/config/global-setup.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {
2+
checkSupplierExists,
3+
createSupplierEntry,
4+
} from "../helpers/generate-fetch-test-data";
5+
import { SUPPLIERID } from "../constants/api-constants";
6+
7+
async function globalSetup() {
8+
console.log("");
9+
console.log("*** BEGINNING GLOBAL SETUP ***");
10+
console.log("");
11+
12+
// check supplier exists
13+
const supplier = await checkSupplierExists(SUPPLIERID);
14+
if (supplier) {
15+
console.log(`Supplier with ID ${SUPPLIERID} already exists.`);
16+
console.log("");
17+
console.log("*** GLOBAL SETUP COMPLETE ***");
18+
console.log("");
19+
return;
20+
}
21+
22+
console.log(`Creating supplier entry with ID: ${SUPPLIERID}`);
23+
await createSupplierEntry(SUPPLIERID);
24+
25+
console.log("");
26+
console.log("*** GLOBAL SETUP COMPLETE ***");
27+
console.log("");
28+
}
29+
30+
export default globalSetup;

tests/config/main.config.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import {defineConfig, PlaywrightTestConfig } from '@playwright/test';
2-
import baseConfig from './playwright.base.config';
3-
import { getReporters } from './reporters';
4-
import path from 'path';
1+
import { PlaywrightTestConfig, defineConfig } from "@playwright/test";
2+
import path from "node:path";
3+
import baseConfig from "./playwright.base.config";
4+
import { getReporters } from "./reporters";
55

66
const localConfig: PlaywrightTestConfig = {
77
...baseConfig,
8+
globalSetup: path.resolve(__dirname, "./global-setup.ts"),
89
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
9-
reporter: getReporters('api-test'),
10+
reporter: getReporters("api-test"),
1011
projects: [
1112
{
12-
name: 'component-tests',
13-
testDir: path.resolve(__dirname, '../component-tests'),
14-
testMatch: '**/*.spec.ts',
13+
name: "component-tests",
14+
testDir: path.resolve(__dirname, "../component-tests"),
15+
testMatch: "**/*.spec.ts",
1516
},
1617
],
1718
};

tests/config/playwright.base.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { defineConfig, PlaywrightTestConfig } from '@playwright/test';
2-
import 'dotenv/config';
1+
/* eslint-disable radix */
2+
import { PlaywrightTestConfig, defineConfig } from "@playwright/test";
3+
import "dotenv/config";
34

4-
const baseUrl = process.env.NHSD_APIM_PROXY_URL || 'http://localhost:3000/';
55
const envMaxInstances = Number.parseInt(process.env.WORKERS_MAX_INST!) || 10;
66
/**
77
* See https://playwright.dev/docs/test-configuration.

tests/constants/api-constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export const API_NAME = `nhs-${envName}-supapi`;
77
export const LETTERSTABLENAME = `nhs-${envName}-supapi-letters`;
88
export const SUPPLIERID = "TestSupplier1";
99
export const MI_ENDPOINT = "mi";
10+
export const SUPPLIERTABLENAME = `nhs-${envName}-supapi-suppliers`;

tests/helpers/common-types.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ export type ErrorMessageBody = {
1717
};
1818

1919
export function error404ResponseBody(): ErrorMessageBody {
20-
let responseBody: ErrorMessageBody;
21-
responseBody = {
20+
const responseBody: ErrorMessageBody = {
2221
errors: [
2322
{
2423
id: "12345",
@@ -36,9 +35,8 @@ export function error404ResponseBody(): ErrorMessageBody {
3635
return responseBody;
3736
}
3837

39-
export function error500ResponseBody(id: string): ErrorMessageBody {
40-
let responseBody: ErrorMessageBody;
41-
responseBody = {
38+
export function error500ResponseBody(): ErrorMessageBody {
39+
const responseBody: ErrorMessageBody = {
4240
errors: [
4341
{
4442
id: "12345",
@@ -57,8 +55,7 @@ export function error500ResponseBody(id: string): ErrorMessageBody {
5755
}
5856

5957
export function error400ResponseBody(): ErrorMessageBody {
60-
let responseBody: ErrorMessageBody;
61-
responseBody = {
58+
const responseBody: ErrorMessageBody = {
6259
errors: [
6360
{
6461
id: "12345",
@@ -77,9 +74,7 @@ export function error400ResponseBody(): ErrorMessageBody {
7774
}
7875

7976
export function requestId500Error(): ErrorMessageBody {
80-
let responseBody: ErrorMessageBody;
81-
82-
responseBody = {
77+
const responseBody: ErrorMessageBody = {
8378
errors: [
8479
{
8580
id: "1234",
@@ -105,8 +100,7 @@ export function error403ResponseBody(): { Message: string } {
105100
}
106101

107102
export function error400InvalidDate(): ErrorMessageBody {
108-
let responseBody: ErrorMessageBody;
109-
responseBody = {
103+
const responseBody: ErrorMessageBody = {
110104
errors: [
111105
{
112106
id: "12345",
@@ -126,8 +120,7 @@ export function error400InvalidDate(): ErrorMessageBody {
126120
}
127121

128122
export function error400IdError(): ErrorMessageBody {
129-
let responseBody: ErrorMessageBody;
130-
responseBody = {
123+
const responseBody: ErrorMessageBody = {
131124
errors: [
132125
{
133126
id: "12344",

tests/helpers/generate-fetch-test-data.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import {
77
import {
88
LETTERSTABLENAME,
99
SUPPLIERID,
10+
SUPPLIERTABLENAME,
1011
envName,
1112
} from "../constants/api-constants";
12-
import runCreateLetter from "./pnpm-helpers";
13+
import { createSupplierData, runCreateLetter } from "./pnpm-helpers";
1314

1415
const ddb = new DynamoDBClient({});
1516
const docClient = DynamoDBDocumentClient.from(ddb);
@@ -78,3 +79,34 @@ export const deleteLettersBySupplier = async (id: string) => {
7879
);
7980
return resp.Attributes;
8081
};
82+
83+
export async function checkSupplierExists(
84+
supplierId: string,
85+
): Promise<boolean> {
86+
try {
87+
const params = {
88+
TableName: SUPPLIERTABLENAME,
89+
KeyConditionExpression: "id = :id",
90+
ExpressionAttributeValues: {
91+
":id": supplierId,
92+
},
93+
};
94+
95+
const { Items } = await docClient.send(new QueryCommand(params));
96+
return Items !== undefined && Items.length > 0;
97+
} catch (error) {
98+
console.error("Error checking supplier existence:", error);
99+
return false;
100+
}
101+
}
102+
103+
export async function createSupplierEntry(supplierId: string): Promise<void> {
104+
await createSupplierData({
105+
filter: "nhs-notify-supplier-api-letter-test-data-utility",
106+
supplierId,
107+
apimId: supplierId,
108+
name: "TestSupplier",
109+
environment: envName,
110+
status: "ENABLED",
111+
});
112+
}

tests/helpers/pnpm-helpers.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import path from "node:path";
66
*
77
* @param options Command-line options for the script.
88
*/
9-
export default async function runCreateLetter(options: {
9+
export async function runCreateLetter(options: {
1010
filter?: string;
1111
supplierId: string;
1212
environment: string;
@@ -33,7 +33,6 @@ export default async function runCreateLetter(options: {
3333
);
3434
const cmd = process.platform === "win32" ? "npm.cmd" : "npm";
3535
const root = path.resolve(workspaceRoot);
36-
console.log("Workspace root:", root);
3736

3837
// Build arguments array
3938
const args = [
@@ -79,3 +78,61 @@ export default async function runCreateLetter(options: {
7978
child.on("error", reject);
8079
});
8180
}
81+
82+
export async function createSupplierData(options: {
83+
filter?: string;
84+
supplierId: string;
85+
name: string;
86+
apimId: string;
87+
environment: string;
88+
status: string;
89+
}) {
90+
const { apimId, environment, name, status, supplierId, filter } = options;
91+
92+
const workspaceRoot = path.resolve(
93+
__dirname,
94+
"../../scripts/utilities/supplier-data",
95+
);
96+
const cmd = process.platform === "win32" ? "npm.cmd" : "npm";
97+
const root = path.resolve(workspaceRoot);
98+
99+
// Build arguments array
100+
const args = [
101+
"-w",
102+
String(filter),
103+
// '--filter', String(filter),
104+
"run",
105+
"cli",
106+
"--",
107+
"put-supplier",
108+
"--id",
109+
supplierId,
110+
"--name",
111+
name,
112+
"--apimId",
113+
apimId,
114+
"--status",
115+
status,
116+
"--environment",
117+
environment,
118+
];
119+
120+
await new Promise<void>((resolve, reject) => {
121+
let output = "";
122+
const child = spawn(cmd, args, {
123+
stdio: "inherit",
124+
cwd: root,
125+
shell: false,
126+
});
127+
child.stdout?.on("id", (id) => {
128+
const text = id.toString();
129+
output += text;
130+
process.stdout.write(text);
131+
});
132+
133+
child.on("close", (code) =>
134+
code === 0 ? resolve() : reject(new Error(`pnpm exited with ${code}`)),
135+
);
136+
child.on("error", reject);
137+
});
138+
}

0 commit comments

Comments
 (0)