Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps-dev): bump @types/busboy from 0.3.0 to 0.3.1 #2979

Merged
merged 4 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
"@opengovsg/mockpass": "^2.7.9",
"@types/bcrypt": "^5.0.0",
"@types/bluebird": "^3.5.36",
"@types/busboy": "^0.3.0",
"@types/busboy": "^0.3.1",
"@types/compression": "^1.7.2",
"@types/connect-datadog": "0.0.6",
"@types/convict": "^6.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Busboy from 'busboy'
import FormData from 'form-data'
import { createReadStream, readFileSync } from 'fs'
import { IncomingHttpHeaders } from 'http'
import { pick } from 'lodash'
import { omit, pick } from 'lodash'
import { mocked } from 'ts-jest/utils'

import {
Expand All @@ -22,12 +22,15 @@ jest.mock('busboy')
const MockBusboy = mocked(Busboy, true)
const RealBusboy = jest.requireActual('busboy') as typeof Busboy

const MOCK_HEADERS = { key: 'value' }
const MOCK_HEADERS: IncomingHttpHeaders = {
'content-type': 'anything',
key: 'value',
}

const MOCK_BUSBOY_ON = jest.fn().mockReturnThis()
const MOCK_BUSBOY = {
on: MOCK_BUSBOY_ON,
} as unknown as busboy.Busboy
} as unknown as Busboy.Busboy

const VALID_FILE_PATH = 'tests/unit/backend/resources/'
const VALID_FILENAME_1 = 'valid.txt'
Expand All @@ -40,13 +43,15 @@ const VALID_FILE_CONTENT_2 = readFileSync(
)

describe('email-submission.receiver', () => {
beforeEach(() => {
MockBusboy.mockReset()
})
describe('createMultipartReceiver', () => {
it('should call Busboy constructor with the correct params', () => {
MockBusboy.mockReturnValueOnce(MOCK_BUSBOY)

const result = EmailSubmissionReceiver.createMultipartReceiver(
MOCK_HEADERS as IncomingHttpHeaders,
)
const result =
EmailSubmissionReceiver.createMultipartReceiver(MOCK_HEADERS)

expect(MockBusboy).toHaveBeenCalledWith({
headers: MOCK_HEADERS,
Expand All @@ -58,14 +63,25 @@ describe('email-submission.receiver', () => {
expect(result._unsafeUnwrap()).toEqual(MOCK_BUSBOY)
})

it('should return error headers are missing content-type key-value', () => {
const result = EmailSubmissionReceiver.createMultipartReceiver(
omit(MOCK_HEADERS, 'content-type'),
)

// Should have failed type guard and not have been called.
expect(MockBusboy).not.toHaveBeenCalled()
expect(result._unsafeUnwrapErr()).toEqual(
new InitialiseMultipartReceiverError(),
)
})

it('should return error when busboy constructor errors', () => {
MockBusboy.mockImplementationOnce(() => {
throw new Error()
})

const result = EmailSubmissionReceiver.createMultipartReceiver(
MOCK_HEADERS as IncomingHttpHeaders,
)
const result =
EmailSubmissionReceiver.createMultipartReceiver(MOCK_HEADERS)

expect(MockBusboy).toHaveBeenCalledWith({
headers: MOCK_HEADERS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Busboy from 'busboy'
import Busboy, { BusboyHeaders } from 'busboy'
import { IncomingHttpHeaders } from 'http'
import { err, ok, Result, ResultAsync } from 'neverthrow'

Expand All @@ -20,13 +20,30 @@ import {

const logger = createLoggerWithLabel(module)

const isBusboyHeaders = (
headers: IncomingHttpHeaders,
): headers is BusboyHeaders => {
return !!headers['content-type']
}

/**
* Initialises a Busboy object to receive the submission stream
* @param headers HTTP request headers
*/
export const createMultipartReceiver = (
headers: IncomingHttpHeaders,
): Result<Busboy.Busboy, InitialiseMultipartReceiverError> => {
if (!isBusboyHeaders(headers)) {
logger.error({
message: "Busboy cannot be init due to missing headers['content-type']",
meta: {
action: 'createMultipartReceiver',
headers,
},
})
return err(new InitialiseMultipartReceiverError())
}

try {
const busboy = new Busboy({
headers,
Expand Down