Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release to val #950

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ DS_Store

# tests
coverage
__snapshots__
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
"tailwindCSS.experimental.classRegex": [
["cva\\(([^)]*)\\)", "[\"'`]([^\"'`]*).*?[\"'`]"]
],
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.words": [
"Cpoc",
"Cpocs",
"opensearch"
]
}
Binary file modified bun.lockb
Binary file not shown.
11 changes: 2 additions & 9 deletions lib/lambda/getAllForms.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { describe, it, expect, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { getAllForms } from "./getAllForms";
import { response } from "../libs/handler-lib";

// Mock the dependencies
vi.mock("../libs/handler-lib", () => ({
response: vi.fn((arg) => arg),
}));

vi.mock("../libs/webforms", () => ({
webformVersions: {
Expand All @@ -31,7 +25,6 @@ describe("getAllForms", () => {

const result = await getAllForms();
expect(result?.statusCode).toEqual(200);
expect(result?.body).toEqual(expectedResponse.body);
expect(response).toHaveBeenCalledWith(expectedResponse);
expect(result?.body).toEqual(JSON.stringify(expectedResponse.body));
});
});
63 changes: 21 additions & 42 deletions lib/lambda/getCpocs.test.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,51 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { describe, it, expect } from "vitest";
import { APIGatewayEvent } from "aws-lambda";
import { handler } from "./getCpocs";
import { response } from "libs/handler-lib";
import * as os from "libs/opensearch-lib";

vi.mock("libs/handler-lib", () => ({
response: vi.fn(),
}));

vi.mock("libs/opensearch-lib", () => ({
search: vi.fn(),
}));
import { mockedServiceServer } from "mocks/server";
import { emptyCpocSearchHandler, errorCpocSearchHandler } from "mocks";
import { cpocs } from "mocks/data/cpocs";

describe("getCpocs Handler", () => {
beforeEach(() => {
vi.clearAllMocks();
process.env.osDomain = "test-domain"; // Set the environment variable before each test
process.env.indexNamespace = "test-namespace-"; // Set the environment variable before each test
});

it("should return 400 if event body is missing", async () => {
const event = {} as APIGatewayEvent;

await handler(event);
const res = await handler(event);

expect(response).toHaveBeenCalledWith({
statusCode: 400,
body: { message: "Event body required" },
});
expect(res.statusCode).toEqual(400);
expect(res.body).toEqual(JSON.stringify({ message: "Event body required" }))
});

// TODO - should this be removed? when will the result be empty and not
// just a result with an empty hit array
it("should return 400 if no Cpocs are found", async () => {
(os.search as vi.Mock).mockResolvedValueOnce(null);
mockedServiceServer.use(emptyCpocSearchHandler);

const event = { body: JSON.stringify({}) } as APIGatewayEvent;

await handler(event);
const res = await handler(event);

expect(response).toHaveBeenCalledWith({
statusCode: 400,
body: { message: "No Cpocs found" },
});
expect(res.statusCode).toEqual(400);
expect(res.body).toEqual(JSON.stringify({ message: "No Cpocs found" }))
});

it("should return 200 with the result if Cpocs are found", async () => {
const mockResult = { hits: { hits: [{ _source: { name: "test-cpoc" } }] } };
(os.search as vi.Mock).mockResolvedValueOnce(mockResult);

const event = { body: JSON.stringify({}) } as APIGatewayEvent;

await handler(event);
const res = await handler(event);
const body = JSON.parse(res.body);

expect(response).toHaveBeenCalledWith({
statusCode: 200,
body: mockResult,
});
expect(res.statusCode).toEqual(200);
expect(body.hits.hits).toEqual(cpocs);
});

it("should return 500 if an error occurs during processing", async () => {
(os.search as vi.Mock).mockRejectedValueOnce(new Error("Test error"));
mockedServiceServer.use(errorCpocSearchHandler);

const event = { body: JSON.stringify({}) } as APIGatewayEvent;

await handler(event);
const res = await handler(event);

expect(response).toHaveBeenCalledWith({
statusCode: 500,
body: { message: "Internal server error" },
});
expect(res.statusCode).toEqual(500);
expect(res.body).toEqual(JSON.stringify({ error: "Internal server error", message: "Response Error" }))
});
});
108 changes: 55 additions & 53 deletions lib/lambda/getSubTypes.test.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,91 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { describe, it, expect } from "vitest";
import { APIGatewayEvent } from "aws-lambda";
import { handler } from "./getSubTypes";
import { response } from "libs/handler-lib";
import * as os from "libs/opensearch-lib";

vi.mock("libs/handler-lib", () => ({
response: vi.fn(),
}));

vi.mock("libs/opensearch-lib", () => ({
search: vi.fn(),
}));
import {
MEDICAID_SPA_AUTHORITY_ID,
CHIP_SPA_AUTHORITY_ID,
NOT_FOUND_AUTHORITY_ID,
TYPE_ONE_ID,
TYPE_TWO_ID,
TYPE_THREE_ID,
DO_NOT_USE_TYPE_ID,
ERROR_AUTHORITY_ID,
medicaidSubtypes,
chipSubtypes
} from "mocks/data/types"
import { TestSubtypeItemResult } from "mocks";

describe("getSubTypes Handler", () => {
beforeEach(() => {
vi.clearAllMocks();
process.env.osDomain = "test-domain"; // Set the environment variable before each test
process.env.indexNamespace = "test-namespace-"; // Set the environment variable before each test
});

it("should return 400 if event body is missing", async () => {
const event = {} as APIGatewayEvent;

await handler(event);
const res = await handler(event);

expect(response).toHaveBeenCalledWith({
statusCode: 400,
body: { message: "Event body required" },
});
expect(res.statusCode).toEqual(400);
expect(res.body).toEqual(JSON.stringify({ message: "Event body required" }))
});

it("should return 400 if no subtypes are found", async () => {
(os.search as vi.Mock).mockResolvedValueOnce(null);

// TODO - should this be removed? when will the result be empty and not
// just a result with an empty hit array
it.skip("should return 400 if no subtypes are found", async () => {
const event = {
body: JSON.stringify({
authorityId: "test-authority",
typeIds: ["type1", "type2"],
authorityId: NOT_FOUND_AUTHORITY_ID,
typeIds: [TYPE_ONE_ID, TYPE_TWO_ID],
}),
} as APIGatewayEvent;

await handler(event);
const res = await handler(event);

expect(response).toHaveBeenCalledWith({
statusCode: 400,
body: { message: "No record found for the given authority" },
});
expect(res.statusCode).toEqual(400);
expect(res.body).toEqual(JSON.stringify({ message: "No record found for the given authority" }));
});

it("should return 200 with the result if subtypes are found", async () => {
const mockResult = {
hits: { hits: [{ _source: { name: "test-subtype" } }] },
};
(os.search as vi.Mock).mockResolvedValueOnce(mockResult);

const event = {
body: JSON.stringify({
authorityId: "test-authority",
typeIds: ["type1", "type2"],
authorityId: MEDICAID_SPA_AUTHORITY_ID,
typeIds: [TYPE_ONE_ID, TYPE_TWO_ID],
}),
} as APIGatewayEvent;

await handler(event);
const res = await handler(event);
const body = JSON.parse(res.body);

expect(response).toHaveBeenCalledWith({
statusCode: 200,
body: mockResult,
});
expect(res.statusCode).toEqual(200);
expect(body.hits.hits).toEqual(medicaidSubtypes)
});

it("should return 500 if an error occurs during processing", async () => {
(os.search as vi.Mock).mockRejectedValueOnce(new Error("Test error"));
it("should filter out types with names that include Do Not Use", async () => {
const event = {
body: JSON.stringify({
authorityId: CHIP_SPA_AUTHORITY_ID,
typeIds: [TYPE_THREE_ID, DO_NOT_USE_TYPE_ID ]
})
} as APIGatewayEvent;

const res = await handler(event);
const body = JSON.parse(res.body);

expect(res.statusCode).toEqual(200);
expect(body.hits.hits).toEqual(chipSubtypes);
body.hits.hits.forEach((type: TestSubtypeItemResult) => {
expect(type?._source?.name).toBeTruthy()
expect(type?._source?.name?.match(/Do Not Use/)).toBeFalsy()
})
})

it("should return 500 if an error occurs during processing", async () => {
const event = {
body: JSON.stringify({
authorityId: "test-authority",
typeIds: ["type1", "type2"],
authorityId: ERROR_AUTHORITY_ID,
typeIds: [TYPE_ONE_ID, TYPE_TWO_ID],
}),
} as APIGatewayEvent;

await handler(event);
const res = await handler(event);

expect(response).toHaveBeenCalledWith({
statusCode: 500,
body: { message: "Internal server error" },
});
expect(res.statusCode).toEqual(500);
expect(res.body).toEqual(JSON.stringify({ error: "Internal server error", message: "Response Error" }))
});
});
12 changes: 4 additions & 8 deletions lib/lambda/getSubTypes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { handleOpensearchError } from "./utils";
import { APIGatewayEvent } from "aws-lambda";
import * as os from "libs/opensearch-lib";
import { response } from "libs/handler-lib";
import * as os from "libs/opensearch-lib";

type GetSubTypesBoby = {
type GetSubTypesBody = {
authorityId: string;
typeIds: string[];
};
Expand Down Expand Up @@ -49,11 +49,7 @@ export const querySubTypes = async (authorityId: string, typeIds: string[]) => {
],
};

return await os.search(
process.env.osDomain,
`${process.env.indexNamespace}subtypes`,
query,
);
return await os.search(process.env.osDomain, `${process.env.indexNamespace}subtypes`, query);
};

export const getSubTypes = async (event: APIGatewayEvent) => {
Expand All @@ -63,7 +59,7 @@ export const getSubTypes = async (event: APIGatewayEvent) => {
body: { message: "Event body required" },
});
}
const body = JSON.parse(event.body) as GetSubTypesBoby;
const body = JSON.parse(event.body) as GetSubTypesBody;
try {
const result = await querySubTypes(body.authorityId, body.typeIds);

Expand Down
Loading
Loading