diff --git a/services/app-api/handlers/banners/create.test.ts b/services/app-api/handlers/banners/create.test.ts index 7989eb48..37f826c8 100644 --- a/services/app-api/handlers/banners/create.test.ts +++ b/services/app-api/handlers/banners/create.test.ts @@ -27,6 +27,13 @@ const testEvent: APIGatewayProxyEvent = { headers: { "cognito-identity-id": "test" }, }; +const testEventWithInvalidData: APIGatewayProxyEvent = { + ...proxyEvent, + body: `{"description":"test description","link":"test link","startDate":"1000","endDate":2000}`, + pathParameters: { bannerId: "testKey" }, + headers: { "cognito-identity-id": "test" }, +}; + describe("Test createBanner API method", () => { beforeEach(() => { jest.clearAllMocks(); @@ -74,4 +81,9 @@ describe("Test createBanner API method", () => { expect(res.statusCode).toBe(StatusCodes.BadRequest); expect(res.body).toContain(error.MISSING_DATA); }); + + test("Test invalid data causes internal server error", async () => { + const res = await createBanner(testEventWithInvalidData); + expect(res.statusCode).toBe(StatusCodes.InternalServerError); + }); }); diff --git a/services/app-api/handlers/banners/create.ts b/services/app-api/handlers/banners/create.ts index abc4d6ce..621ec664 100644 --- a/services/app-api/handlers/banners/create.ts +++ b/services/app-api/handlers/banners/create.ts @@ -10,6 +10,17 @@ import { import { canWriteBanner } from "../../utils/authorization"; import { parseBannerId } from "../../libs/param-lib"; import { BannerData } from "../../types/banner"; +import { number, object, string } from "yup"; +import { validateData } from "../../utils/validation"; + +const validationSchema = object().shape({ + key: string().required(), + title: string().required(), + description: string().required(), + link: string().url().notRequired(), + startDate: number().required(), + endDate: number().required(), +}); export const createBanner = handler(parseBannerId, async (request) => { const { bannerId } = request.parameters; @@ -25,9 +36,13 @@ export const createBanner = handler(parseBannerId, async (request) => { const unvalidatedPayload = request.body; - //TO DO: add validation & validation test back + const validatedPayload = await validateData( + validationSchema, + unvalidatedPayload + ); + const { title, description, link, startDate, endDate } = - unvalidatedPayload as BannerData; + validatedPayload as BannerData; const currentTime = Date.now();