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

feat(incoming-encrypt-submission): add more tests #2211

Merged
merged 1 commit into from
Jun 20, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import { mocked } from 'ts-jest/utils'

import {
generateDefaultField,
generateProcessedSingleAnswerResponse,
generateSingleAnswerResponse,
} from '../../../../../../tests/unit/backend/helpers/generate-form-data'
import * as LogicUtil from '../../../../../shared/util/logic'
import {
BasicField,
IPopulatedEncryptedForm,
IPreventSubmitLogicSchema,
LogicType,
ResponseMode,
} from '../../../../../types'
import { checkIsEncryptedEncoding } from '../../../../utils/encryption'
import { ConflictError } from '../../submission.errors'
import {
ConflictError,
ProcessingError,
ValidateFieldError,
} from '../../submission.errors'
import IncomingEncryptSubmission from '../IncomingEncryptSubmission.class'

jest.mock('../../../../utils/encryption')
Expand Down Expand Up @@ -63,4 +71,95 @@ describe('IncomingEncryptSubmission', () => {
new ConflictError('Some form fields are missing'),
)
})

it('should allow responses for encrypt mode hidden fields', async () => {
mockCheckIsEncryptedEncoding.mockReturnValueOnce(ok(true))
// Only check for mobile and email fields, since the other fields are
// e2e encrypted from the browser.
const mobileField = generateDefaultField(BasicField.Mobile)
const emailField = generateDefaultField(BasicField.Email)
// Add answers to both mobile and email fields
const mobileResponse = generateSingleAnswerResponse(
mobileField,
'+6587654321',
)

const emailResponse = generateSingleAnswerResponse(
emailField,
'[email protected]',
)

const mobileProcessedResponse = generateProcessedSingleAnswerResponse(
mobileField,
'+6587654321',
)
mobileProcessedResponse.isVisible = false

const emailProcessedResponse = generateProcessedSingleAnswerResponse(
emailField,
'[email protected]',
)
emailProcessedResponse.isVisible = false

const responses = [mobileResponse, emailResponse]

const result = IncomingEncryptSubmission.init(
{
responseMode: ResponseMode.Encrypt,
form_fields: [mobileField, emailField],
} as unknown as IPopulatedEncryptedForm,
responses,
'',
)

expect(result.isOk()).toEqual(true)
expect(result._unsafeUnwrap().responses).toEqual(responses)
})

it('should return error when any responses are not valid for encrypted form submission', async () => {
mockCheckIsEncryptedEncoding.mockReturnValueOnce(ok(true))
// Only mobile and email fields are parsed, since the other fields are
// e2e encrypted from the browser.
const mobileField = generateDefaultField(BasicField.Mobile)
const mobileResponse = generateSingleAnswerResponse(mobileField, 'invalid')

const result = IncomingEncryptSubmission.init(
{
responseMode: ResponseMode.Encrypt,
form_fields: [mobileField],
} as unknown as IPopulatedEncryptedForm,
[mobileResponse],
'',
)

expect(result.isErr()).toEqual(true)
expect(result._unsafeUnwrapErr()).toEqual([
new ValidateFieldError('Invalid answer submitted'),
])
})

it('should return error when encrypted form submission is prevented by logic', async () => {
mockCheckIsEncryptedEncoding.mockReturnValueOnce(ok(true))
// Mock logic util to return non-empty to check if error is thrown
jest.spyOn(LogicUtil, 'getLogicUnitPreventingSubmit').mockReturnValueOnce({
preventSubmitMessage: 'mock prevent submit',
conditions: [],
logicType: LogicType.PreventSubmit,
_id: 'some id',
} as unknown as IPreventSubmitLogicSchema)

const result = IncomingEncryptSubmission.init(
{
responseMode: ResponseMode.Encrypt,
form_fields: [],
} as unknown as IPopulatedEncryptedForm,
[],
'',
)

expect(result.isErr()).toEqual(true)
expect(result._unsafeUnwrapErr()).toEqual(
new ProcessingError('Submission prevented by form logic'),
)
})
})