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

test: add tests for verification model #99

Merged
merged 1 commit into from
Aug 7, 2020
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
126 changes: 126 additions & 0 deletions tests/unit/backend/models/verification.server.model.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const dbHandler = require('../helpers/db-handler')
const mongoose = require('mongoose')
const Verification = spec(
'dist/backend/app/models/verification.server.model',
).default(mongoose)
const { omit, merge, pick } = require('lodash')
const { ObjectId } = require('bson-ext')

const VFN_FIELD_DEFAULTS = {
signedData: null,
hashedOtp: null,
hashCreatedAt: null,
hashRetries: 0,
}

const generateFieldParams = () => {
const mockParams = {
fieldType: 'mockField',
_id: String(new ObjectId()),
}
return merge({}, VFN_FIELD_DEFAULTS, mockParams)
}

const VFN_PARAMS = {
formId: new ObjectId(),
}

const VFN_DEFAULTS = {
fields: [],
}

describe('Verification Model', () => {
beforeAll(async () => await dbHandler.connect())
afterEach(async () => await dbHandler.clearDatabase())
afterAll(async () => await dbHandler.closeDatabase())

describe('Schema', () => {
it('should create and save successfully with defaults', async () => {
const verification = new Verification(VFN_PARAMS)
const verificationSaved = await verification.save()
expect(verificationSaved._id).toBeDefined()
expect(verificationSaved.expireAt).toBeInstanceOf(Date)
const actualSavedFields = omit(verificationSaved.toObject(), [
'_id',
'expireAt',
'__v',
])
const expectedSavedFields = merge({}, VFN_DEFAULTS, VFN_PARAMS)
expect(actualSavedFields).toEqual(expectedSavedFields)
})

it('should create and save successfully with expireAt specified', async () => {
const params = merge({}, VFN_PARAMS, { expireAt: new Date() })
const verification = new Verification(params)
const verificationSaved = await verification.save()
expect(verificationSaved._id).toBeDefined()
const actualSavedFields = omit(verificationSaved.toObject(), [
'_id',
'__v',
])
const expectedSavedFields = merge({}, VFN_DEFAULTS, params)
expect(actualSavedFields).toEqual(expectedSavedFields)
})

it('should create and save successfully with default fields', async () => {
const vfnParams = merge({}, VFN_PARAMS, {
fields: [generateFieldParams(), generateFieldParams()],
})
const verification = new Verification(vfnParams)
const verificationSaved = await verification.save()
expect(verificationSaved._id).toBeDefined()
const actualSavedFields = omit(verificationSaved.toObject(), [
'_id',
'expireAt',
'__v',
])
const expectedSavedFields = merge({}, VFN_DEFAULTS, VFN_PARAMS, vfnParams)
expect(actualSavedFields).toEqual(expectedSavedFields)
})

it('should create and save successfully with field keys specified', async () => {
const field = merge({}, generateFieldParams(), {
signedData: 'signedData',
hashedOtp: 'hashedOtp',
hashCreatedAt: new Date(),
hashRetries: 5,
})
const vfnParams = merge({}, VFN_PARAMS, { fields: [field] })
const verification = new Verification(vfnParams)
const verificationSaved = await verification.save()
expect(verificationSaved._id).toBeDefined()
const actualSavedFields = omit(verificationSaved.toObject(), [
'_id',
'expireAt',
'__v',
])
const expectedSavedFields = merge({}, VFN_DEFAULTS, VFN_PARAMS, vfnParams)
expect(actualSavedFields).toEqual(expectedSavedFields)
})

it('should reject attempts to save identical field IDs', async () => {
const field = generateFieldParams()
const vfnParams = merge({}, VFN_PARAMS, {
fields: [field, field],
})
const verification = new Verification(vfnParams)
await expectAsync(verification.save()).toBeRejected()
})
})

describe('Statics', () => {
describe('findTransactionMetadata', () => {
it('should only return non-sensitive fields', async () => {
const verification = new Verification(VFN_PARAMS)
const verificationSaved = await verification.save()
expect(verificationSaved._id).toBeDefined()
const found = await Verification.findTransactionMetadata(
verificationSaved._id,
)
const actual = found.toObject()
const expected = pick(verificationSaved, ['formId', 'expireAt', '_id'])
expect(actual).toEqual(expected)
})
})
})
})