Skip to content

Commit

Permalink
chore(deps-dev): bump @types/busboy from 0.3.0 to 0.3.1 (#2979)
Browse files Browse the repository at this point in the history
* chore(deps-dev): bump @types/busboy from 0.3.0 to 0.3.1

Bumps [@types/busboy](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/busboy) from 0.3.0 to 0.3.1.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/busboy)

---
updated-dependencies:
- dependency-name: "@types/busboy"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

* fix: update busboy namespace back to lowercased variant

* feat: add typeguard for busboy headers

* test(email-submission): add test for typeguard in busboy headers

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Kar Rui Lau <[email protected]>
  • Loading branch information
dependabot[bot] and karrui authored Oct 25, 2021
1 parent 7d206a5 commit 690026e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
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

0 comments on commit 690026e

Please sign in to comment.