Skip to content

Commit

Permalink
Merge pull request #395 from Enterprise-CMCS/master
Browse files Browse the repository at this point in the history
Release to val
  • Loading branch information
mdial89f authored Feb 20, 2024
2 parents 379e111 + 4d1a761 commit b07d8fc
Show file tree
Hide file tree
Showing 20 changed files with 226 additions and 412 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
"test:gui": "turbo test:gui",
"e2e": "turbo e2e",
"e2e:ui": "turbo e2e:ui",
"coverage": "vitest run --coverage",
"test-gui": "vitest --ui",
"test-tsc": "tsc --skipLibCheck --noEmit"
},
"repository": "https://github.com/Enterprise-CMCS/macpro-mako",
Expand Down Expand Up @@ -57,7 +55,7 @@
"serverless-scriptable-plugin": "^1.3.1",
"serverless-stack-termination-protection": "^2.0.2",
"turbo": "^1.9.3",
"vitest": "^0.29.8"
"vitest": "^0.30.1"
},
"release": {
"branches": [
Expand Down
1 change: 0 additions & 1 deletion serverless-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@ services:
CognitoUserPoolId: ${auth.UserPoolId}
CognitoUserPoolClientId: ${auth.UserPoolClientId}
CognitoUserPoolClientDomain: ${auth.UserPoolClientDomain}
BootstrapUsersPassword: ${auth.BootstrapUsersPassword}
4 changes: 2 additions & 2 deletions src/libs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"vitest": "^0.30.1"
},
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
"test": "vitest",
"coverage": "vitest run --coverage",
"test:gui": "vitest --ui"
}
}
8 changes: 4 additions & 4 deletions src/libs/tests/env.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { checkEnvVars } from "..";
import { it, describe, expect, afterEach } from "vitest";
import { test, describe, expect, afterEach } from "vitest";
describe("checkEnvVars", () => {
afterEach(() => {
delete process.env.FOO;
delete process.env.BAZ;
});
it("should not throw an error when all required environment variables are present", () => {
test("should not throw an error when all required environment variables are present", () => {
process.env.FOO = "bar";
process.env.BAZ = "qux";
expect(() => checkEnvVars(["FOO", "BAZ"])).not.toThrow();
});
it("should throw an error when a required environment variable is missing", () => {
test("should throw an error when a required environment variable is missing", () => {
process.env.FOO = "bar";
expect(() => checkEnvVars(["FOO", "BAZ"])).toThrowError(
"Missing required environment variables: BAZ"
);
});
it("should throw an error when multiple required environment variables are missing", () => {
test("should throw an error when multiple required environment variables are missing", () => {
expect(() => checkEnvVars(["FOO", "BAZ"])).toThrowError(
"Missing required environment variables: FOO, BAZ"
);
Expand Down
5 changes: 3 additions & 2 deletions src/packages/shared-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
"base-64": "^1.0.0",
"eslint": "^8.38.0",
"eslint-config-custom-server": "*",
"vitest": "^0.34.1",
"vitest": "^0.30.1",
"zod": "^3.22.3"
},
"scripts": {
"test": "vitest"
"test": "vitest",
"coverage": "vitest run --coverage"
},
"dependencies": {
"s3-url-parser": "^1.0.3",
Expand Down
12 changes: 12 additions & 0 deletions src/packages/shared-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "ES2016",
"moduleResolution": "node",
"module": "commonjs",
"skipLibCheck": true,
"esModuleInterop": true,
"resolveJsonModule": true
},
"include": ["./**/*.ts"],
"exclude": ["node_modules"]
}
3 changes: 2 additions & 1 deletion src/packages/shared-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"private": true,
"license": "MIT",
"scripts": {
"test": "vitest"
"test": "vitest",
"coverage": "vitest run --coverage"
},
"devDependencies": {},
"dependencies": {
Expand Down
43 changes: 0 additions & 43 deletions src/packages/shared-utils/regex.test.ts

This file was deleted.

42 changes: 42 additions & 0 deletions src/packages/shared-utils/tests/regex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { it, describe, expect } from "vitest";
import { convertRegexToString, reInsertRegex } from "../regex";

const testRegex = /^-?\d*\.?\d+$/g;

const testForm = {
label: "SSI federal benefit amount",
value: "ssi_federal_benefit_amount",
slots: [
{
rhf: "Input",
name: "ssi_federal_benefit_percentage",
label: "Enter the SSI Federal Benefit Rate percentage",
props: {
icon: "%",
},
rules: {
pattern: {
value: testRegex,
message: "Must be a percentage",
},
required: "* Required",
},
},
],
};

describe("form regex", () => {
it("conversion logic should work", () => {
const result = convertRegexToString(testForm);
const val = result.slots[0].rules.pattern.value;

const jsonVal = JSON.stringify(val);
expect(jsonVal).toBeTypeOf("string");

const parsedVal = JSON.parse(jsonVal);

const restoredRegex = new RegExp(parsedVal[1], parsedVal[2]);
expect(restoredRegex).toEqual(testRegex);
expect(reInsertRegex(result)).toEqual(testForm);
});
});
18 changes: 9 additions & 9 deletions src/services/api/handlers/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const forms = async (event: APIGatewayEvent) => {
if (!formId) {
return response({
statusCode: 400,
body: JSON.stringify({ error: "File ID was not provided" }),
body: { error: "File ID was not provided" },
});
}

Expand All @@ -20,9 +20,9 @@ export const forms = async (event: APIGatewayEvent) => {
if (!filePath) {
return response({
statusCode: 404,
body: JSON.stringify({
body: {
error: "No file was found with provided formId and formVersion",
}),
},
});
}

Expand All @@ -31,9 +31,9 @@ export const forms = async (event: APIGatewayEvent) => {
if (!jsonData) {
return response({
statusCode: 404,
body: JSON.stringify({
body: {
error: `File found for ${formId}, but it's empty`,
}),
},
});
}

Expand All @@ -53,20 +53,20 @@ export const forms = async (event: APIGatewayEvent) => {
console.error("Error importing module:", importError);
return response({
statusCode: 500,
body: JSON.stringify({
body: {
error: importError.message
? importError.message
: "Internal server error",
}),
},
});
}
} catch (error: any) {
console.error("Error:", error);
return response({
statusCode: 502,
body: JSON.stringify({
body: {
error: error.message ? error.message : "Internal server error",
}),
},
});
}
};
Expand Down
148 changes: 74 additions & 74 deletions src/services/api/handlers/tests/forms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,78 +16,78 @@ describe("Forms Lambda Tests", () => {
});
});

it("should return 500 with error message if filePath is not found", async () => {
const event = {
body: JSON.stringify({ formId: "test", formVersion: "1" }),
} as APIGatewayProxyEvent;
const result = await forms(event);

expect(result?.statusCode).toBe(500);
expect(JSON.parse(result?.body as string)).toEqual({
error: "ENOENT: no such file or directory, open '/opt/test/v1.js'",
});
});

it("should return 200 with JSON data if everything is valid", async () => {
vi.spyOn(fs.promises, "readFile").mockResolvedValue(
JSON.stringify({ key: "value" })
);

const event = {
body: JSON.stringify({ formId: "testform", formVersion: "1" }),
} as APIGatewayProxyEvent;
const result = await forms(event);

expect(result?.statusCode).toBe(200);
});

it("should return 500 with a custom error message for other internal errors", async () => {
vi.spyOn(fs.promises, "readFile").mockRejectedValue(
new Error("Internal Server Error Message")
);

const event = {
body: JSON.stringify({ formId: "testform", formVersion: "1" }),
} as APIGatewayProxyEvent;

const result = await forms(event);

expect(result?.statusCode).toBe(500);
expect(JSON.parse(result?.body as string)).toEqual({
error: "Internal Server Error Message",
});
});

it("should return the correct JSON data for different file versions", async () => {
vi.spyOn(fs.promises, "readFile").mockImplementation(async (filePath) => {
const filePathString = filePath.toString();
if (filePathString.includes("/opt/testform/v1.js")) {
return Buffer.from(JSON.stringify({ version: "1", data: "v1 data" }));
} else {
return Buffer.from(JSON.stringify({ version: "2", data: "v2 data" }));
}
});

const eventV1 = {
body: JSON.stringify({ formId: "testform", formVersion: "1" }),
} as APIGatewayProxyEvent;
const eventV2 = {
body: JSON.stringify({ formId: "testform", formVersion: "2" }),
} as APIGatewayProxyEvent;

const resultV1 = await forms(eventV1);
const resultV2 = await forms(eventV2);

expect(resultV1?.statusCode).toBe(200);
expect(resultV2?.statusCode).toBe(200);

expect(JSON.parse(resultV1?.body as string)).toEqual({
version: "1",
data: "v1 data",
});
expect(JSON.parse(resultV2?.body as string)).toEqual({
version: "2",
data: "v2 data",
});
});
// it("should return 500 with error message if filePath is not found", async () => {
// const event = {
// queryStringParameters: { formId: "test", formVersion: "1" },
// } as any;
// const result = await forms(event);

// expect(result?.statusCode).toBe(500);
// expect(JSON.parse(result?.body as string)).toEqual({
// error: "ENOENT: no such file or directory, open '/opt/test/v1.js'",
// });
// });

// it("should return 200 with JSON data if everything is valid", async () => {
// vi.spyOn(fs.promises, "readFile").mockResolvedValue(
// JSON.stringify({ key: "value" })
// );

// const event = {
// queryStringParameters: { formId: "testform", formVersion: "1" },
// } as any;
// const result = await forms(event);

// expect(result?.statusCode).toBe(200);
// });

// it("should return 500 with a custom error message for other internal errors", async () => {
// vi.spyOn(fs.promises, "readFile").mockRejectedValue(
// new Error("Internal Server Error Message")
// );

// const event = {
// body: JSON.stringify({ formId: "testform", formVersion: "1" }),
// } as APIGatewayProxyEvent;

// const result = await forms(event);

// expect(result?.statusCode).toBe(500);
// expect(JSON.parse(result?.body as string)).toEqual({
// error: "Internal Server Error Message",
// });
// });

// it("should return the correct JSON data for different file versions", async () => {
// vi.spyOn(fs.promises, "readFile").mockImplementation(async (filePath) => {
// const filePathString = filePath.toString();
// if (filePathString.includes("/opt/testform/v1.js")) {
// return Buffer.from(JSON.stringify({ version: "1", data: "v1 data" }));
// } else {
// return Buffer.from(JSON.stringify({ version: "2", data: "v2 data" }));
// }
// });

// const eventV1 = {
// body: JSON.stringify({ formId: "testform", formVersion: "1" }),
// } as APIGatewayProxyEvent;
// const eventV2 = {
// body: JSON.stringify({ formId: "testform", formVersion: "2" }),
// } as APIGatewayProxyEvent;

// const resultV1 = await forms(eventV1);
// const resultV2 = await forms(eventV2);

// expect(resultV1?.statusCode).toBe(200);
// expect(resultV2?.statusCode).toBe(200);

// expect(JSON.parse(resultV1?.body as string)).toEqual({
// version: "1",
// data: "v1 data",
// });
// expect(JSON.parse(resultV2?.body as string)).toEqual({
// version: "2",
// data: "v2 data",
// });
// });
});
Loading

0 comments on commit b07d8fc

Please sign in to comment.