Skip to content

Commit

Permalink
fix: add check to block webhooks updates on mrf
Browse files Browse the repository at this point in the history
fix: switch form to original form

test: add test cases for mrf webhook blocks

chore: update comments for clarity
  • Loading branch information
KenLSM committed Apr 5, 2024
1 parent 16484da commit 71ab0c5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
DatabaseError,
DatabasePayloadSizeError,
DatabaseValidationError,
MalformedParametersError,
} from 'src/app/modules/core/core.errors'
import { MissingUserError } from 'src/app/modules/user/user.errors'
import * as UserService from 'src/app/modules/user/user.service'
Expand Down Expand Up @@ -1419,6 +1420,47 @@ describe('admin-form.service', () => {
)
expect(MOCK_UPDATED_FORM.getSettings).toHaveBeenCalledTimes(0)
})

it('should not allow webhooks updates for MRF', async () => {
const MOCK_MULTIRESPONDENT_FORM = jest.mocked({
_id: new ObjectId(),
status: FormStatus.Public,
responseMode: FormResponseMode.Multirespondent,
} as unknown as IPopulatedForm)
const settingsToUpdate: SettingsUpdateDto = {
webhook: {
url: 'does not matter',
},
}

// Act
const actualResult = await AdminFormService.updateFormSettings(
MOCK_MULTIRESPONDENT_FORM,
settingsToUpdate,
)

// Assert
expect(actualResult._unsafeUnwrapErr()).toBeInstanceOf(
MalformedParametersError,
)
})

it('should allow webhooks updates for encrypt form', async () => {
const settingsToUpdate: SettingsUpdateDto = {
webhook: {
url: 'does not matter',
},
}

// Act
const actualResult = await AdminFormService.updateFormSettings(
MOCK_ENCRYPT_FORM,
settingsToUpdate,
)

// Assert
expect(actualResult.isOk()).toBeTrue()
})
})

describe('updateFormField', () => {
Expand Down
10 changes: 10 additions & 0 deletions src/app/modules/form/admin-form/admin-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@ export const updateFormCollaborators = (
* @param body the subset of form settings to update
* @returns ok(updated form settings) on success
* @returns err(MalformedParametersError) if auth type update is attempted for a multi-respondent form
* @returns err(MalformedParametersError) if webhook update is attempted for a multi-respondent form
* @returns err(database errors) if db error is thrown during form setting update
*/
export const updateFormSettings = (
Expand All @@ -1063,6 +1064,15 @@ export const updateFormSettings = (
return errAsync(new MalformedParametersError('Invalid authentication type'))
}

if (
originalForm.responseMode === FormResponseMode.Multirespondent &&
Boolean(body.webhook?.url)
) {
return errAsync(
new MalformedParametersError('Webhooks not supported on MRF'),
)
}

const dotifiedSettingsToUpdate = dotifyObject(body)
const ModelToUse = getFormModelByResponseMode(originalForm.responseMode)

Expand Down

0 comments on commit 71ab0c5

Please sign in to comment.