Skip to content

Commit

Permalink
consolidated kafka mocks into setup
Browse files Browse the repository at this point in the history
  • Loading branch information
thetif committed Jan 9, 2025
1 parent 44bbdd8 commit 90b1ba7
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 756 deletions.
25 changes: 3 additions & 22 deletions lib/lambda/checkConsumerLag.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect, vi, afterEach } from "vitest";
import { describe, it, expect, vi } from "vitest";
import { handler } from "./checkConsumerLag";
import { Context } from "aws-lambda";
import {
Expand All @@ -10,31 +10,12 @@ import {
TEST_MULTIPLE_TOPICS_TOPIC_NAME,
TEST_MISSING_CONSUMER_FUNCTION_NAME,
TEST_MISSING_CONSUMER_TOPIC_NAME,
mockedAdmin,
} from "mocks";

const mockKafkaAdmin = {
connect: vi.fn(),
describeGroups: vi.fn().mockResolvedValue({
groups: [{ state: "Stable" }],
}),
fetchTopicOffsets: vi.fn().mockResolvedValue([{ offset: "100" }]),
fetchOffsets: vi.fn().mockResolvedValue([{ partitions: [{ offset: "100" }] }]),
disconnect: vi.fn(),
};

vi.mock("kafkajs", () => ({
Kafka: vi.fn().mockImplementation(() => ({
admin: vi.fn().mockReturnValue(mockKafkaAdmin),
})),
}));

describe("Lambda Handler", () => {
const callback = vi.fn();

afterEach(() => {
vi.clearAllMocks();
});

it("should handle successful execution with stable and current offsets", async () => {
const event = {
triggers: [
Expand Down Expand Up @@ -113,7 +94,7 @@ describe("Lambda Handler", () => {
brokerString: "broker1,broker2",
};

mockKafkaAdmin.describeGroups.mockRejectedValueOnce(new Error("Kafka admin error"));
mockedAdmin.describeGroups.mockRejectedValueOnce(new Error("Kafka admin error"));

await handler(event, {} as Context, callback);

Expand Down
275 changes: 50 additions & 225 deletions lib/lambda/submit/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,21 @@
import { describe, it, expect } from "vitest";
import { submit } from "./index";
import { APIGatewayEvent } from "node_modules/shared-types";
import { describe, it, expect, vi, afterEach, beforeEach } from "vitest";
import { getRequestContext } from "mocks";
import { getRequestContext, automatedStateSubmitter } from "mocks";
import {
capitatedAmendmentBase,
appkBase,
capitatedInitial,
capitatedRenewal,
contractingAmmendment,
contractingInitial,
contractingRenewal,
newChipSubmission,
newMedicaidSubmission,
respondToRai,
temporaryExtension,
toggleWithdrawRai,
uploadSubsequentDocuments,
withdrawPackage,
withdrawRai,
events,
eventsAttachmentRequired,
eventsAuthorizationRequired,
} from "mocks/data/submit/base";

vi.mock("kafkajs", () => {
const producer = {
connect: vi.fn(),
send: vi.fn(),
disconnect: vi.fn(),
};
const kafka = {
producer: () => producer,
};
return {
Kafka: vi.fn(() => kafka),
Producer: vi.fn(() => producer),
};
});
describe("submit Lambda function", () => {
let brokerString: string | undefined;
beforeEach(() => {
brokerString = process.env.brokerString;
process.env.brokerString = "broker1,broker2";
});

afterEach(() => {
process.env.brokerString = brokerString;
});
it("should have no body", async () => {
const event = {} as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(400);
expect(result.body).toEqual('"Event body required"');
});

it("should have no event in the body", async () => {
const event = {
body: `{"event": ""}`,
Expand All @@ -67,191 +33,50 @@ describe("submit Lambda function", () => {
expect(result.statusCode).toEqual(400);
expect(result.body).toEqual('{"message":"Bad Request - Unknown event type Not a real event"}');
});
it("should start to create an capitated ammendment event", async () => {
const base = JSON.stringify(capitatedAmendmentBase);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an capitated ammendment event", async () => {
const base = JSON.stringify(capitatedAmendmentBase);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an app-k event", async () => {
const base = JSON.stringify(appkBase);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an capitated initial event", async () => {
const base = JSON.stringify(capitatedInitial);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an contracting ammendment event", async () => {
const base = JSON.stringify(contractingAmmendment);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an contracting initial event", async () => {
const base = JSON.stringify(contractingInitial);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an contracting renewal event", async () => {
const base = JSON.stringify(contractingRenewal);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an new chip submission event", async () => {
const base = JSON.stringify(newChipSubmission);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an new chip medicaid submission event", async () => {
const base = JSON.stringify(newMedicaidSubmission);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an new respond to rai event", async () => {
const base = JSON.stringify(respondToRai);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an temporary extension event", async () => {
const base = JSON.stringify(temporaryExtension);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an toggle withdraw event", async () => {
const base = JSON.stringify(toggleWithdrawRai);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an withdraw package event", async () => {
const base = JSON.stringify(withdrawPackage);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an withdraw rai event", async () => {
const base = JSON.stringify(withdrawRai);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an capitated renewal event", async () => {
const base = JSON.stringify(capitatedRenewal);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an contracting initial event", async () => {
const base = JSON.stringify(contractingInitial);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
});
it("should start to create an upload subsequent documents event", async () => {
const base = JSON.stringify(uploadSubsequentDocuments);

const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
describe("successfully submit event types", () => {
it.each(events.map((event) => [event.event, JSON.stringify(event)]))(
"should successfully submit %s event",
async (_, base) => {
const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(200);
expect(result.body).toEqual('{"message":"success"}');
},
);
});

describe("fails if missing attachments", () => {
it.each(
eventsAttachmentRequired.map((event) => [
event.event,
JSON.stringify({ ...event, attachments: {} }),
]),
)("should return a 500 if %s event is missing attachments", async (_, base) => {
const event = {
body: base,
requestContext: getRequestContext(),
} as unknown as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(500);
});
});

describe("fails if not submitter is not authorized", () => {
it.each(eventsAuthorizationRequired.map((event) => [event.event, JSON.stringify(event)]))(
"should return a 500 if not authorized to submit %s event",
async (_, base) => {
const event = {
body: base,
requestContext: getRequestContext(automatedStateSubmitter),
} as APIGatewayEvent;
const result = await submit(event);
expect(result.statusCode).toEqual(500);
expect(result.body).toEqual('{"message":"Internal server error"}');
},
);
});
});
Loading

0 comments on commit 90b1ba7

Please sign in to comment.