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

fix: return storage mode submission version when when retrieving from server #2112

Merged
merged 5 commits into from
Jun 9, 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
78 changes: 37 additions & 41 deletions src/app/models/__tests__/encrypt-submission.server.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import moment from 'moment-timezone'
import mongoose from 'mongoose'

import getSubmissionModel, {
getEmailSubmissionModel,
getEncryptSubmissionModel,
} from 'src/app/models/submission.server.model'
import {
IEmailSubmissionSchema,
IEncryptedSubmissionSchema,
ISubmissionSchema,
SubmissionMetadata,
SubmissionType,
} from 'src/types'

import dbHandler from 'tests/unit/backend/helpers/jest-db'

const Submission = getSubmissionModel(mongoose)
const EmailSubmission = getEmailSubmissionModel(mongoose)
const EncryptSubmission = getEncryptSubmissionModel(mongoose)

describe('Encrypt Submission Model', () => {
Expand All @@ -33,15 +33,14 @@ describe('Encrypt Submission Model', () => {
const validFormId = new ObjectId().toHexString()
const createdDate = new Date()
// Add valid encrypt submission.
const validSubmission =
await Submission.create<IEncryptedSubmissionSchema>({
form: validFormId,
myInfoFields: [],
submissionType: SubmissionType.Encrypt,
encryptedContent: MOCK_ENCRYPTED_CONTENT,
version: 1,
created: createdDate,
})
const validSubmission = await EncryptSubmission.create({
form: validFormId,
myInfoFields: [],
submissionType: SubmissionType.Encrypt,
encryptedContent: MOCK_ENCRYPTED_CONTENT,
version: 1,
created: createdDate,
})

// Act
const result = await EncryptSubmission.findSingleMetadata(
Expand Down Expand Up @@ -110,7 +109,7 @@ describe('Encrypt Submission Model', () => {
// Arrange
// Add 3 valid encrypt submission.
const validSubmissionPromises = times(3, (idx) =>
Submission.create<IEncryptedSubmissionSchema>({
EncryptSubmission.create({
form: VALID_FORM_ID,
myInfoFields: [],
submissionType: SubmissionType.Encrypt,
Expand All @@ -119,9 +118,8 @@ describe('Encrypt Submission Model', () => {
created: MOCK_CREATED_DATES_ASC[idx],
}),
)
const validSubmissions: ISubmissionSchema[] = await Promise.all(
validSubmissionPromises,
)
const validSubmissions: IEncryptedSubmissionSchema[] =
await Promise.all(validSubmissionPromises)

// Act
const actual = await EncryptSubmission.findAllMetadataByFormId(
Expand Down Expand Up @@ -149,7 +147,7 @@ describe('Encrypt Submission Model', () => {
// Arrange
// Add 3 valid encrypt submission.
const validSubmissionPromises = times(3, (idx) =>
Submission.create<IEncryptedSubmissionSchema>({
EncryptSubmission.create({
form: VALID_FORM_ID,
myInfoFields: [],
submissionType: SubmissionType.Encrypt,
Expand All @@ -158,9 +156,8 @@ describe('Encrypt Submission Model', () => {
created: MOCK_CREATED_DATES_ASC[idx],
}),
)
const validSubmissions: ISubmissionSchema[] = await Promise.all(
validSubmissionPromises,
)
const validSubmissions: IEncryptedSubmissionSchema[] =
await Promise.all(validSubmissionPromises)

// Act
const actual = await EncryptSubmission.findAllMetadataByFormId(
Expand Down Expand Up @@ -192,7 +189,7 @@ describe('Encrypt Submission Model', () => {
// Arrange
// Add 3 valid encrypt submission.
const validSubmissionPromises = times(3, (idx) =>
Submission.create<IEncryptedSubmissionSchema>({
EncryptSubmission.create({
form: VALID_FORM_ID,
myInfoFields: [],
submissionType: SubmissionType.Encrypt,
Expand All @@ -201,9 +198,8 @@ describe('Encrypt Submission Model', () => {
created: MOCK_CREATED_DATES_ASC[idx],
}),
)
const validSubmissions: ISubmissionSchema[] = await Promise.all(
validSubmissionPromises,
)
const validSubmissions: IEncryptedSubmissionSchema[] =
await Promise.all(validSubmissionPromises)

// Act
const actual = await EncryptSubmission.findAllMetadataByFormId(
Expand Down Expand Up @@ -235,7 +231,7 @@ describe('Encrypt Submission Model', () => {
// Arrange
// Add 3 valid encrypt submission.
const validSubmissionPromises = times(3, (idx) =>
Submission.create<IEncryptedSubmissionSchema>({
EncryptSubmission.create({
form: VALID_FORM_ID,
myInfoFields: [],
submissionType: SubmissionType.Encrypt,
Expand All @@ -244,9 +240,8 @@ describe('Encrypt Submission Model', () => {
created: MOCK_CREATED_DATES_ASC[idx],
}),
)
const validSubmissions: ISubmissionSchema[] = await Promise.all(
validSubmissionPromises,
)
const validSubmissions: IEncryptedSubmissionSchema[] =
await Promise.all(validSubmissionPromises)

// Act
const actual = await EncryptSubmission.findAllMetadataByFormId(
Expand Down Expand Up @@ -288,11 +283,11 @@ describe('Encrypt Submission Model', () => {
it('should return cursor that contains all the submissions', async () => {
// Arrange
const validFormId = new ObjectId().toHexString()
const validSubmission = await Submission.create({
const validSubmission = await EncryptSubmission.create({
submissionType: SubmissionType.Encrypt,
form: validFormId,
encryptedContent: 'mock encrypted content abc',
version: 1,
version: 3,
})
const expectedSubmission = pick(
validSubmission,
Expand All @@ -301,6 +296,7 @@ describe('Encrypt Submission Model', () => {
'verifiedContent',
'encryptedContent',
'submissionType',
'version',
)

// Act
Expand Down Expand Up @@ -344,11 +340,11 @@ describe('Encrypt Submission Model', () => {
it('should return correct submission by its id', async () => {
// Arrange
const validFormId = new ObjectId().toHexString()
const validSubmission = await Submission.create({
const validSubmission = await EncryptSubmission.create({
submissionType: SubmissionType.Encrypt,
form: validFormId,
encryptedContent: 'mock encrypted content abc',
version: 1,
version: 33,
attachmentMetadata: { someFileName: 'some url of attachment' },
})

Expand All @@ -360,15 +356,16 @@ describe('Encrypt Submission Model', () => {

// Assert
const expected = pick(
validSubmission.toObject(),
validSubmission.toJSON(),
'_id',
'attachmentMetadata',
'created',
'encryptedContent',
'submissionType',
'version',
)
expect(actual).not.toBeNull()
expect(actual?.toObject()).toEqual(expected)
expect(actual?.toJSON()).toEqual(expected)
})

it('should return null when submission id does not exist', async () => {
Expand All @@ -390,14 +387,13 @@ describe('Encrypt Submission Model', () => {
it('should return null when type of submission with given id is not SubmissionType.Encrypt', async () => {
// Arrange
const validFormId = new ObjectId().toHexString()
const validEmailSubmission =
await Submission.create<IEmailSubmissionSchema>({
submissionType: SubmissionType.Email,
form: validFormId,
recipientEmails: ['[email protected]'],
responseHash: 'any hash',
responseSalt: 'any salt',
})
const validEmailSubmission = await EmailSubmission.create({
submissionType: SubmissionType.Email,
form: validFormId,
recipientEmails: ['[email protected]'],
responseHash: 'any hash',
responseSalt: 'any salt',
})

// Act
const actual = await EncryptSubmission.findEncryptedSubmissionById(
Expand Down
2 changes: 2 additions & 0 deletions src/app/models/submission.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ EncryptSubmissionSchema.statics.getSubmissionCursorByFormId = function (
verifiedContent: 1,
attachmentMetadata: 1,
created: 1,
version: 1,
id: 1,
})
.batchSize(2000)
Expand All @@ -384,6 +385,7 @@ EncryptSubmissionSchema.statics.findEncryptedSubmissionById = function (
verifiedContent: 1,
attachmentMetadata: 1,
created: 1,
version: 1,
})
.exec()
}
Expand Down
29 changes: 17 additions & 12 deletions src/app/modules/form/admin-form/__tests__/admin-form.routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3609,7 +3609,7 @@ describe('admin-form.routes', () => {
encryptedContent: 'any encrypted content',
verifiedContent: 'any verified content',
}
const submission = await createSubmission({
const submission = await createEncryptSubmission({
form: defaultForm,
...expectedSubmissionParams,
})
Expand All @@ -3629,6 +3629,7 @@ describe('admin-form.routes', () => {
refNo: String(submission._id),
submissionTime: expect.any(String),
verified: expectedSubmissionParams.verifiedContent,
version: submission.version,
})
})

Expand All @@ -3642,7 +3643,7 @@ describe('admin-form.routes', () => {
['fieldId2', 'some.other.attachment.url'],
]),
}
const submission = await createSubmission({
const submission = await createEncryptSubmission({
form: defaultForm,
...expectedSubmissionParams,
})
Expand All @@ -3669,6 +3670,7 @@ describe('admin-form.routes', () => {
refNo: String(submission._id),
submissionTime: expect.any(String),
verified: expectedSubmissionParams.verifiedContent,
version: submission.version,
})
})

Expand Down Expand Up @@ -3819,7 +3821,7 @@ describe('admin-form.routes', () => {
jest
.spyOn(EncryptSubmissionModel, 'findEncryptedSubmissionById')
.mockRejectedValueOnce(new Error('ohno'))
const submission = await createSubmission({
const submission = await createEncryptSubmission({
form: defaultForm,
encryptedContent: 'any encrypted content',
verifiedContent: 'any verified content',
Expand Down Expand Up @@ -3848,7 +3850,7 @@ describe('admin-form.routes', () => {
.spyOn(aws.s3, 'getSignedUrlPromise')
.mockRejectedValueOnce(new Error('something went wrong'))

const submission = await createSubmission({
const submission = await createEncryptSubmission({
form: defaultForm,
encryptedContent: 'any encrypted content',
verifiedContent: 'any verified content',
Expand Down Expand Up @@ -4349,7 +4351,7 @@ describe('admin-form.routes', () => {
// Create 11 submissions
const submissions = await Promise.all(
times(11, (count) =>
createSubmission({
createEncryptSubmission({
form: defaultForm,
encryptedContent: `any encrypted content ${count}`,
verifiedContent: `any verified content ${count}`,
Expand Down Expand Up @@ -4384,7 +4386,7 @@ describe('admin-form.routes', () => {
it('should return 200 with empty results if query.page does not have metadata', async () => {
// Arrange
// Create single submission
await createSubmission({
await createEncryptSubmission({
form: defaultForm,
encryptedContent: `any encrypted content`,
verifiedContent: `any verified content`,
Expand Down Expand Up @@ -4412,7 +4414,7 @@ describe('admin-form.routes', () => {
// Create 3 submissions
const submissions = await Promise.all(
times(3, (count) =>
createSubmission({
createEncryptSubmission({
form: defaultForm,
encryptedContent: `any encrypted content ${count}`,
verifiedContent: `any verified content ${count}`,
Expand Down Expand Up @@ -4603,7 +4605,7 @@ describe('admin-form.routes', () => {
// Arrange
const submissions = await Promise.all(
times(11, (count) =>
createSubmission({
createEncryptSubmission({
form: defaultForm,
encryptedContent: `any encrypted content ${count}`,
verifiedContent: `any verified content ${count}`,
Expand Down Expand Up @@ -4639,6 +4641,7 @@ describe('admin-form.routes', () => {
encryptedContent: s.encryptedContent,
verifiedContent: s.verifiedContent,
created: s.created,
version: s.version,
}),
)
.sort((a, b) => String(a._id).localeCompare(String(b._id)))
Expand All @@ -4659,7 +4662,7 @@ describe('admin-form.routes', () => {
// Arrange
const submissions = await Promise.all(
times(5, (count) =>
createSubmission({
createEncryptSubmission({
form: defaultForm,
encryptedContent: `any encrypted content ${count}`,
verifiedContent: `any verified content ${count}`,
Expand Down Expand Up @@ -4695,6 +4698,7 @@ describe('admin-form.routes', () => {
encryptedContent: s.encryptedContent,
verifiedContent: s.verifiedContent,
created: s.created,
version: s.version,
}),
)
.sort((a, b) => String(a._id).localeCompare(String(b._id)))
Expand Down Expand Up @@ -4723,7 +4727,7 @@ describe('admin-form.routes', () => {
// Arrange
const submissions = await Promise.all(
times(5, (count) =>
createSubmission({
createEncryptSubmission({
form: defaultForm,
encryptedContent: `any encrypted content ${count}`,
verifiedContent: `any verified content ${count}`,
Expand Down Expand Up @@ -4774,6 +4778,7 @@ describe('admin-form.routes', () => {
encryptedContent: s.encryptedContent,
verifiedContent: s.verifiedContent,
created: s.created,
version: s.version,
}),
)
.filter((s) => expectedSubmissionIds.includes(s._id))
Expand Down Expand Up @@ -5385,7 +5390,7 @@ describe('admin-form.routes', () => {
})

// Helper utils
const createSubmission = ({
const createEncryptSubmission = ({
form,
encryptedContent,
verifiedContent,
Expand All @@ -5398,7 +5403,7 @@ const createSubmission = ({
verifiedContent?: string
created?: Date
}) => {
return SubmissionModel.create({
return EncryptSubmissionModel.create({
submissionType: SubmissionType.Encrypt,
form: form._id,
authType: form.authType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,6 @@ export const createEncryptedSubmissionDto = (
content: submissionData.encryptedContent,
verified: submissionData.verifiedContent,
attachmentMetadata: attachmentPresignedUrls,
version: submissionData.version,
}
}
Loading