Skip to content

Commit

Permalink
CMDCT-4190: adding validation for banner create operation
Browse files Browse the repository at this point in the history
  • Loading branch information
angelaco11 committed Dec 23, 2024
1 parent 4e059c0 commit b44c122
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
12 changes: 12 additions & 0 deletions services/app-api/handlers/banners/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
});
19 changes: 17 additions & 2 deletions services/app-api/handlers/banners/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down

0 comments on commit b44c122

Please sign in to comment.