|
| 1 | +import { createContext, createEvent } from "@lambda-middleware/utils"; |
| 2 | +import { APIGatewayEvent } from "aws-lambda"; |
| 3 | +import { Type, Static } from "@sinclair/typebox"; |
| 4 | +import { typeboxValidator } from "./typeboxValidator"; |
| 5 | + |
| 6 | +const NameBodySchema = Type.Object({ |
| 7 | + firstName: Type.String(), |
| 8 | + lastName: Type.String(), |
| 9 | +}); |
| 10 | + |
| 11 | +type NameBody = Static<typeof NameBodySchema>; |
| 12 | + |
| 13 | +describe("typeboxValidator", () => { |
| 14 | + describe("with valid input", () => { |
| 15 | + const body = JSON.stringify({ |
| 16 | + firstName: "John", |
| 17 | + lastName: "Doe", |
| 18 | + }); |
| 19 | + |
| 20 | + it("sets the body to the validated value", async () => { |
| 21 | + const handler = jest.fn(); |
| 22 | + await typeboxValidator(NameBodySchema)(handler)( |
| 23 | + createEvent({ body }), |
| 24 | + createContext() |
| 25 | + ); |
| 26 | + expect(handler).toHaveBeenCalledWith( |
| 27 | + expect.objectContaining({ |
| 28 | + body: { |
| 29 | + firstName: "John", |
| 30 | + lastName: "Doe", |
| 31 | + }, |
| 32 | + }), |
| 33 | + expect.anything() |
| 34 | + ); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe("with superfluous input", () => { |
| 39 | + const body = JSON.stringify({ |
| 40 | + firstName: "John", |
| 41 | + lastName: "Doe", |
| 42 | + injection: "malicious", |
| 43 | + }); |
| 44 | + |
| 45 | + it("omits the superfluous input", async () => { |
| 46 | + const handler = jest.fn(); |
| 47 | + await typeboxValidator(NameBodySchema)(handler)( |
| 48 | + createEvent({ body }), |
| 49 | + createContext() |
| 50 | + ); |
| 51 | + expect(handler).toHaveBeenCalledWith( |
| 52 | + expect.objectContaining({ |
| 53 | + body: { |
| 54 | + firstName: "John", |
| 55 | + lastName: "Doe", |
| 56 | + }, |
| 57 | + }), |
| 58 | + expect.anything() |
| 59 | + ); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe("with invalid input", () => { |
| 64 | + const body = JSON.stringify({ |
| 65 | + firstName: "John", |
| 66 | + }); |
| 67 | + |
| 68 | + it("throws an error with statusCode 400", async () => { |
| 69 | + const handler = jest.fn(); |
| 70 | + await expect( |
| 71 | + typeboxValidator(NameBodySchema)(handler)( |
| 72 | + createEvent({ body }), |
| 73 | + createContext() |
| 74 | + ) |
| 75 | + ).rejects.toMatchObject({ |
| 76 | + statusCode: 400, |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("with null input", () => { |
| 82 | + const body = null; |
| 83 | + |
| 84 | + it("throws an error with statusCode 400", async () => { |
| 85 | + const handler = jest.fn(); |
| 86 | + await expect( |
| 87 | + typeboxValidator(NameBodySchema)(handler)( |
| 88 | + createEvent({ body }), |
| 89 | + createContext() |
| 90 | + ) |
| 91 | + ).rejects.toMatchObject({ |
| 92 | + statusCode: 400, |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("with an empty body and optional validation", () => { |
| 98 | + const body = ""; |
| 99 | + |
| 100 | + const OptionalNameBodySchema = Type.Object({ |
| 101 | + firstName: Type.Optional(Type.String()), |
| 102 | + lastName: Type.Optional(Type.String()), |
| 103 | + }); |
| 104 | + |
| 105 | + type OptionalNameBody = Static<typeof OptionalNameBodySchema>; |
| 106 | + |
| 107 | + it("returns the handler's response", async () => { |
| 108 | + const expectedResponse = { |
| 109 | + statusCode: 200, |
| 110 | + body: "Done", |
| 111 | + }; |
| 112 | + const handler = jest.fn().mockResolvedValue(expectedResponse); |
| 113 | + const actualResponse = await typeboxValidator(OptionalNameBodySchema)( |
| 114 | + handler |
| 115 | + )(createEvent({ body }), createContext()); |
| 116 | + expect(actualResponse).toEqual(expectedResponse); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments