-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(sms.service): reworked tests to account for mocking of db
- Loading branch information
Showing
1 changed file
with
15 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { ObjectId } from 'bson' | ||
import _ from 'lodash' | ||
import mongoose from 'mongoose' | ||
|
||
import getFormModel from 'src/app/models/form.server.model' | ||
import { MalformedParametersError } from 'src/app/modules/core/core.errors' | ||
import { | ||
DatabaseError, | ||
MalformedParametersError, | ||
} from 'src/app/modules/core/core.errors' | ||
import { getMongoErrorMessage } from 'src/app/utils/handle-mongo-error' | ||
import { VfnErrors } from 'src/shared/util/verification' | ||
import { FormOtpData, IFormSchema, IUserSchema, ResponseMode } from 'src/types' | ||
|
||
|
@@ -353,84 +356,33 @@ describe('sms.service', () => { | |
}) | ||
|
||
describe('retrieveFreeSmsCounts', () => { | ||
let mockOtpData: FormOtpData | ||
let mockForm: IFormSchema | ||
const VERIFICATION_SMS_COUNT = 3 | ||
|
||
beforeEach(async () => { | ||
mockForm = await FormModel.create({ | ||
title: 'Test Form', | ||
emails: [testUser.email], | ||
admin: testUser._id, | ||
responseMode: ResponseMode.Email, | ||
}) | ||
|
||
mockOtpData = { | ||
form: mockForm._id, | ||
formAdmin: { | ||
email: testUser.email, | ||
userId: testUser._id, | ||
}, | ||
} | ||
|
||
await Promise.all( | ||
_.range(VERIFICATION_SMS_COUNT).map(async () => { | ||
await Promise.all([ | ||
dbHandler.insertVerifiedSms({ | ||
formId: mockForm._id, | ||
formAdmin: mockOtpData.formAdmin, | ||
}), | ||
dbHandler.insertVerifiedSms({ | ||
formId: mockForm._id, | ||
formAdmin: mockOtpData.formAdmin, | ||
isOnboarded: true, | ||
}), | ||
]) | ||
}), | ||
) | ||
}) | ||
|
||
it('should retrieve sms counts correctly for a specified user', async () => { | ||
// Act | ||
const actual = await SmsService.retrieveFreeSmsCounts(testUser._id) | ||
// Assert | ||
// should retrieve counts of users that are NOT onboarded | ||
expect(actual._unsafeUnwrap()).toBe(VERIFICATION_SMS_COUNT) | ||
}) | ||
|
||
it('should not include other forms of sms verifications', async () => { | ||
// Arrange | ||
await SmsService.sendAdminContactOtp( | ||
/* recipient= */ TWILIO_TEST_NUMBER, | ||
/* otp= */ '111111', | ||
/* userId= */ testUser._id, | ||
/* defaultConfig= */ MOCK_VALID_CONFIG, | ||
) | ||
//Arrange | ||
const retrieveSpy = jest.spyOn(SmsCountModel, 'retrieveFreeSmsCounts') | ||
retrieveSpy.mockResolvedValueOnce(VERIFICATION_SMS_COUNT) | ||
|
||
// Act | ||
const actual = await SmsService.retrieveFreeSmsCounts(testUser._id) | ||
|
||
// Assert | ||
// should retrieve counts of users that are NOT onboarded | ||
expect(actual._unsafeUnwrap()).toBe(VERIFICATION_SMS_COUNT) | ||
}) | ||
|
||
it('should not include other users verifications', async () => { | ||
it('should return a database error when retrieval fails', async () => { | ||
// Arrange | ||
const MOCK_USER_ID = new ObjectId() | ||
// Insert a new sms sent under the new user | ||
await dbHandler.insertVerifiedSms({ | ||
formId: mockForm._id, | ||
formAdmin: { | ||
email: '[email protected]', | ||
userId: MOCK_USER_ID, | ||
}, | ||
}) | ||
const retrieveSpy = jest.spyOn(SmsCountModel, 'retrieveFreeSmsCounts') | ||
retrieveSpy.mockRejectedValueOnce('ohno') | ||
|
||
// Act | ||
const actual = await SmsService.retrieveFreeSmsCounts(testUser._id) | ||
|
||
// Assert | ||
expect(actual._unsafeUnwrap()).toBe(VERIFICATION_SMS_COUNT) | ||
expect(actual._unsafeUnwrapErr()).toEqual( | ||
new DatabaseError(getMongoErrorMessage('ohno')), | ||
) | ||
}) | ||
}) | ||
|
||
|