-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor/is-form-public: migrate isFormPublic middleware to TypeScript (
#791)
- Loading branch information
1 parent
ca57bc0
commit a8ec327
Showing
5 changed files
with
103 additions
and
76 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
src/app/modules/form/public-form/__tests__/public-form.middlewares.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Request } from 'express' | ||
import { StatusCodes } from 'http-status-codes' | ||
|
||
import expressHandler from 'tests/unit/backend/helpers/jest-express' | ||
|
||
import { Status, WithForm } from '../../../../../types' | ||
import { isFormPublicCheck } from '../public-form.middlewares' | ||
|
||
describe('public-form.middlewares', () => { | ||
it('should call next middleware function if form is public', () => { | ||
const mockReq = Object.assign(expressHandler.mockRequest(), { | ||
form: { status: Status.Public }, | ||
}) as WithForm<Request> | ||
const mockRes = expressHandler.mockResponse() | ||
const mockNext = jest.fn() | ||
|
||
isFormPublicCheck(mockReq, mockRes, mockNext) | ||
|
||
expect(mockNext).toBeCalled() | ||
expect(mockRes.sendStatus).not.toBeCalled() | ||
expect(mockRes.status).not.toBeCalled() | ||
expect(mockRes.json).not.toBeCalled() | ||
}) | ||
|
||
it('should return HTTP 404 Not Found if form is private', () => { | ||
const title = 'My private form' | ||
const inactiveMessage = 'The form is not available.' | ||
const form = { | ||
status: Status.Private, | ||
title, | ||
inactiveMessage, | ||
} | ||
|
||
const mockReq = Object.assign(expressHandler.mockRequest(), { | ||
form, | ||
}) as WithForm<Request> | ||
const mockRes = expressHandler.mockResponse() | ||
const mockNext = jest.fn() | ||
|
||
isFormPublicCheck(mockReq, mockRes, mockNext) | ||
|
||
expect(mockNext).not.toBeCalled() | ||
expect(mockRes.sendStatus).not.toBeCalled() | ||
expect(mockRes.status).toBeCalledWith(StatusCodes.NOT_FOUND) | ||
expect(mockRes.json).toBeCalledWith({ | ||
message: inactiveMessage, | ||
isPageFound: true, | ||
formTitle: title, | ||
}) | ||
}) | ||
|
||
it('should return HTTP 410 Gone if form is archived', () => { | ||
const form = { | ||
status: Status.Archived, | ||
} | ||
|
||
const mockReq = Object.assign(expressHandler.mockRequest(), { | ||
form, | ||
}) as WithForm<Request> | ||
const mockRes = expressHandler.mockResponse() | ||
const mockNext = jest.fn() | ||
|
||
isFormPublicCheck(mockReq, mockRes, mockNext) | ||
|
||
expect(mockNext).not.toBeCalled() | ||
expect(mockRes.sendStatus).toBeCalledWith(StatusCodes.GONE) | ||
expect(mockRes.status).not.toBeCalled() | ||
expect(mockRes.json).not.toBeCalledWith() | ||
}) | ||
}) |
29 changes: 29 additions & 0 deletions
29
src/app/modules/form/public-form/public-form.middlewares.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { RequestHandler } from 'express' | ||
import { StatusCodes } from 'http-status-codes' | ||
|
||
import { WithForm } from '../../../../types' | ||
import { FormDeletedError } from '../form.errors' | ||
import { isFormPublic } from '../form.service' | ||
|
||
/** | ||
* Express middleware function that checks if a form attached to the Express request handler is public. | ||
* before allowing downstream middleware to handle the request. Otherwise, it returns a HTTP 404 if the | ||
* form is private, or HTTP 410 if the form has been archived. | ||
* @param req - Express request object | ||
* @param res - Express response object | ||
* @param next - Express next middleware function | ||
*/ | ||
export const isFormPublicCheck: RequestHandler = (req, res, next) => { | ||
const { form } = req as WithForm<typeof req> | ||
return isFormPublic(form) | ||
.map(() => next()) | ||
.mapErr((error) => { | ||
return error instanceof FormDeletedError | ||
? res.sendStatus(StatusCodes.GONE) | ||
: res.status(StatusCodes.NOT_FOUND).json({ | ||
message: form.inactiveMessage, | ||
isPageFound: true, // Flag to prevent default 404 subtext ("please check link") from showing | ||
formTitle: form.title, | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
tests/unit/backend/controllers/public-forms.server.controller.spec.js
This file was deleted.
Oops, something went wrong.