Skip to content

Commit

Permalink
feat: add more webhook tests to check for undefined params (#41)
Browse files Browse the repository at this point in the history
* feat: update webhook's generateSignature undefined checks

* chore: update FormField type link in README
  • Loading branch information
karrui committed Jul 30, 2020
1 parent 4c4af7f commit 41a20ea
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ the shape

<pre>
{
responses: <a href="https://github.com/opengovsg/formsg-javascript-sdk/blob/master/src/types/index.d.ts#L27">FormField[]</a>
responses: <a href="./src/types.ts#L30">FormField</a>[]
verified?: <a href="https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkt">Record&lt;string, any&gt;</a>
}
</pre>
Expand Down
52 changes: 52 additions & 0 deletions spec/webhooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,58 @@ describe('Webhooks', () => {
).toThrow(MissingSecretKeyError)
})

it('should throw error if generateSignature is called with undefined uri', () => {
expect(() =>
webhooks.generateSignature({
// For TypeScript to accept it since signature only accepts strings, but
// still want to test for undefined behavior.
uri: undefined!,
submissionId,
formId,
epoch: Date.now(),
})
).toThrow(TypeError)
})

it('should throw error if generateSignature is called with undefined submissionId', () => {
expect(() =>
webhooks.generateSignature({
uri,
// For TypeScript to accept it since signature only accepts strings, but
// still want to test for undefined behavior.
submissionId: undefined!,
formId,
epoch: Date.now(),
})
).toThrow(TypeError)
})

it('should throw error if generateSignature is called with undefined formId', () => {
expect(() =>
webhooks.generateSignature({
uri,
submissionId,
// For TypeScript to accept it since signature only accepts strings, but
// still want to test for undefined behavior.
formId: undefined!,
epoch: Date.now(),
})
).toThrow(TypeError)
})

it('should throw error if generateSignature is called with undefined epoch', () => {
expect(() =>
webhooks.generateSignature({
uri,
submissionId,
formId,
// For TypeScript to accept it since signature only accepts strings, but
// still want to test for undefined behavior.
epoch: undefined!,
})
).toThrow(TypeError)
})

it('should throw error if constructHeader is called without a secret key in class instantiation', () => {
const testEpoch = Date.now()
const validSignature = generateTestSignature(testEpoch)
Expand Down
7 changes: 6 additions & 1 deletion src/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default class Webhooks {
* @param params.epoch Number of milliseconds since Jan 1, 1970
* @returns the generated signature
* @throws {MissingSecretKeyError} if a secret key is not provided when instantiating this class
* @throws {TypeError} if any parameters are undefined
*/
generateSignature = ({
uri,
Expand All @@ -71,14 +72,18 @@ export default class Webhooks {
epoch,
}: {
uri: string
submissionId: Object
submissionId: string
formId: string
epoch: number
}) => {
if (!this.secretKey) {
throw new MissingSecretKeyError()
}

if (!submissionId || !uri || !formId || !epoch) {
throw new TypeError('submissionId, uri, formId, or epoch must be provided to generate a webhook signature')
}

const baseString = `${
url.parse(uri).href
}.${submissionId}.${formId}.${epoch}`
Expand Down

0 comments on commit 41a20ea

Please sign in to comment.