Skip to content

Commit

Permalink
fix: Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Oct 3, 2023
1 parent 7279188 commit 409d2ca
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 57 deletions.
17 changes: 13 additions & 4 deletions api.planx.uk/inviteToPay/createPaymentSendEvents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const mockedCreateScheduledEvent = createScheduledEvent as jest.MockedFunction<
typeof createScheduledEvent
>;

const mockScheduledEventResponse = {
message: "success",
event_id: "abc123",
} as const;

describe("Create payment send events webhook", () => {
const ENDPOINT = "/webhooks/hasura/create-payment-send-events";

Expand Down Expand Up @@ -83,29 +88,33 @@ describe("Create payment send events webhook", () => {
});

it("returns a 200 on successful event setup", async () => {
mockedCreateScheduledEvent.mockResolvedValue("test-event-id");
mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse);

await supertest(app)
.post(ENDPOINT)
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
.send({ payload: { sessionId: "123" } })
.expect(200)
.then((response) => {
expect(response.body).toMatchObject({ email: "test-event-id" });
expect(response.body).toMatchObject({
email: mockScheduledEventResponse,
});
});
});

it("passes the correct arguments along to createScheduledEvent", async () => {
const body = { createdAt: new Date(), payload: { sessionId: "123" } };
mockedCreateScheduledEvent.mockResolvedValue("test-event-id");
mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse);

await supertest(app)
.post(ENDPOINT)
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
.send(body)
.expect(200)
.then((response) => {
expect(response.body).toMatchObject({ email: "test-event-id" });
expect(response.body).toMatchObject({
email: mockScheduledEventResponse,
});
});

const mockArgs = mockedCreateScheduledEvent.mock.calls[0][0];
Expand Down
27 changes: 19 additions & 8 deletions api.planx.uk/modules/webhooks/_old/lowcalSessionEvents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const mockedCreateScheduledEvent = createScheduledEvent as jest.MockedFunction<
typeof createScheduledEvent
>;

const mockScheduledEventResponse = {
message: "success",
event_id: "abc123",
} as const;

describe("Create reminder event webhook", () => {
const ENDPOINT = "/webhooks/hasura/create-reminder-event";

Expand Down Expand Up @@ -41,7 +46,7 @@ describe("Create reminder event webhook", () => {

it("returns a 200 on successful event setup", async () => {
const body = { createdAt: new Date(), payload: { sessionId: "123" } };
mockedCreateScheduledEvent.mockResolvedValue("test");
mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse);

await post(ENDPOINT)
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
Expand All @@ -50,20 +55,26 @@ describe("Create reminder event webhook", () => {
.then((response) => {
// it's queued up x2 reminders for 7 days and 1 day from expiry
expect(response.body).toHaveLength(2);
expect(response.body).toStrictEqual(["test", "test"]);
expect(response.body).toStrictEqual([
mockScheduledEventResponse,
mockScheduledEventResponse,
]);
});
});

it("passes the correct arguments along to createScheduledEvent", async () => {
const body = { createdAt: new Date(), payload: { sessionId: "123" } };
mockedCreateScheduledEvent.mockResolvedValue("test");
mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse);

await post(ENDPOINT)
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
.send(body)
.expect(200)
.then((response) => {
expect(response.body).toStrictEqual(["test", "test"]);
expect(response.body).toStrictEqual([
mockScheduledEventResponse,
mockScheduledEventResponse,
]);
});

const mockArgs = mockedCreateScheduledEvent.mock.calls[0][0];
Expand Down Expand Up @@ -129,27 +140,27 @@ describe("Create expiry event webhook", () => {

it("returns a 200 on successful event setup", async () => {
const body = { createdAt: new Date(), payload: { sessionId: "123" } };
mockedCreateScheduledEvent.mockResolvedValue("test");
mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse);

await post(ENDPOINT)
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
.send(body)
.expect(200)
.then((response) => {
expect(response.body).toBe("test");
expect(response.body).toStrictEqual(mockScheduledEventResponse);
});
});

it("passes the correct arguments along to createScheduledEvent", async () => {
const body = { createdAt: new Date(), payload: { sessionId: "123" } };
mockedCreateScheduledEvent.mockResolvedValue("test");
mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse);

await post(ENDPOINT)
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
.send(body)
.expect(200)
.then((response) => {
expect(response.body).toBe("test");
expect(response.body).toStrictEqual(mockScheduledEventResponse);
});
const mockArgs = mockedCreateScheduledEvent.mock.calls[0][0];
expect(mockArgs.webhook).toBe("{{HASURA_PLANX_API_URL}}/send-email/expiry");
Expand Down
18 changes: 10 additions & 8 deletions api.planx.uk/modules/webhooks/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,16 @@ components:
content:
application/json:
schema:
type: object
properties:
message:
type: string
enum: ["success"]
event_id:
type: string
description: Internal Hasura ID of the generated event
type: array
items:
type: object
properties:
message:
type: string
enum: ["success"]
event_id:
type: string
description: Internal Hasura ID of the generated event
paths:
/webhooks/hasura/sendSlackNotification:
post:
Expand Down
6 changes: 2 additions & 4 deletions api.planx.uk/modules/webhooks/paymentRequestEvents/schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { z } from "zod";
import { ValidatedRequestHandler } from "../../../shared/middleware/validate";

// TODO: Make this better
type Response = [any, any] | any[];
import { ScheduledEventResponse } from "../../../hasura/metadata";

export const createPaymentEventSchema = z.object({
body: z.object({
Expand All @@ -19,5 +17,5 @@ export type CreatePaymentEvent = z.infer<

export type CreatePaymentEventController = ValidatedRequestHandler<
typeof createPaymentEventSchema,
Response
ScheduledEventResponse[]
>;
34 changes: 1 addition & 33 deletions api.planx.uk/modules/webhooks/sendNotification/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ describe("Send Slack notifications endpoint", () => {
expect(mockAdmin.session.find).toHaveBeenCalledTimes(1);
expect(response.body.message).toBe("Posted to Slack");
expect(response.body.data).toMatch(/abc123/);
});
});

it("adds a status to the Slack message for a disability exemption", async () => {
Expand Down Expand Up @@ -259,39 +260,6 @@ describe("Send Slack notifications endpoint", () => {
});
});

it("adds an exemption status if there's no fee for the session", async () => {
process.env.APP_ENVIRONMENT = "production";
mockAdmin.session.find = jest
.fn()
.mockResolvedValue(mockSessionWithoutFee);

await post(ENDPOINT)
.query({ type: "uniform-submission" })
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
.send(body)
.expect(200)
.then((response) => {
expect(response.body.data).toMatch(/abc123/);
expect(response.body.data).toMatch(/test-council/);
expect(response.body.data).toMatch(/[Exempt]/);
});
});

it("handles missing sessions", async () => {
process.env.APP_ENVIRONMENT = "production";
mockAdmin.session.find = jest.fn().mockResolvedValueOnce(null);

await post(ENDPOINT)
.query({ type: "uniform-submission" })
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
.send(body)
.expect(500)
.then((response) => {
expect(mockAdmin.session.find).toHaveBeenCalledTimes(1);
expect(response.body.error).toMatch(/Failed to send/);
});
});

it("returns error when Slack fails", async () => {
process.env.APP_ENVIRONMENT = "production";
mockSend.mockRejectedValue("Fail!");
Expand Down

0 comments on commit 409d2ca

Please sign in to comment.