-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #523 from twilio-labs/add-tests
Add tests for verify prefill
- Loading branch information
Showing
12 changed files
with
220 additions
and
51 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
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
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
This file was deleted.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
|
@@ -4,6 +4,6 @@ | |
"private": true, | ||
"dependencies": { | ||
"twilio": "^5.0.4", | ||
"node-fetch": "^3.3.2" | ||
"node-fetch": "^2.7.0" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const fetch = require('node-fetch'); | ||
const fetchUserDataFunction = require('../functions/fetch-user-data').handler; | ||
const helpers = require('../../test/test-helper'); | ||
|
||
const testContext = { | ||
LOOKUP_API_KEY: 'foo', | ||
LOOKUP_API_SECRET: 'bar', | ||
}; | ||
|
||
jest.mock('node-fetch', () => jest.fn()); | ||
|
||
describe('verify-prefill/fetch-user-data', () => { | ||
beforeAll(() => { | ||
helpers.setup({}); | ||
}); | ||
afterAll(() => { | ||
helpers.teardown(); | ||
}); | ||
|
||
test('returns success with valid request', (done) => { | ||
const callback = (_err, result) => { | ||
expect(result).toBeDefined(); | ||
expect(result.success).toEqual(true); | ||
expect(result.prefillData).toBeDefined(); | ||
done(); | ||
}; | ||
|
||
const lookupResponse = Promise.resolve({ | ||
json: () => | ||
Promise.resolve({ | ||
pre_fill: 'mydata', // eslint-disable-line camelcase | ||
}), | ||
}); | ||
fetch.mockImplementation(() => lookupResponse); | ||
|
||
const event = { | ||
phoneNumber: '+17341234567', | ||
verificationSid: 'VExxkasjdf', | ||
}; | ||
fetchUserDataFunction(testContext, event, callback); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const sendOtpFunction = require('../functions/send-otp').handler; | ||
const helpers = require('../../test/test-helper'); | ||
|
||
const mockLookup = { | ||
fetch: jest.fn(() => Promise.resolve({ valid: true })), | ||
}; | ||
const mockClient = { | ||
verify: { | ||
v2: { | ||
services: jest.fn(() => ({ | ||
verifications: { | ||
create: jest.fn(), | ||
}, | ||
})), | ||
}, | ||
}, | ||
lookups: { | ||
v2: { | ||
phoneNumbers: jest.fn(() => mockLookup), | ||
}, | ||
}, | ||
}; | ||
|
||
const testContext = { | ||
VERIFY_SERVICE_SID: 'default', | ||
getTwilioClient: () => mockClient, | ||
}; | ||
|
||
describe('verify/send-otp', () => { | ||
beforeAll(() => { | ||
helpers.setup({}); | ||
}); | ||
afterAll(() => { | ||
helpers.teardown(); | ||
}); | ||
|
||
test('returns an error with VERIFY_SERVICE_SID is missing', (done) => { | ||
const callback = (_err, result) => { | ||
expect(result).toBeDefined(); | ||
expect(result.success).toEqual(false); | ||
expect(result.message).toEqual('Missing VERIFY_SERVICE_SID'); | ||
done(); | ||
}; | ||
|
||
const event = { phoneNumber: '+17341234567' }; | ||
const contextWithoutServiceSid = { getTwilioClient: () => mockClient }; | ||
sendOtpFunction(contextWithoutServiceSid, event, callback); | ||
}); | ||
|
||
test('returns an error with invalid phone number', (done) => { | ||
const callback = (_err, result) => { | ||
expect(result.success).toEqual(false); | ||
expect(result.message).toEqual( | ||
'Invalid phone number. Please enter a valid number in E.164 format.' | ||
); | ||
done(); | ||
}; | ||
|
||
mockLookup.fetch.mockImplementationOnce(() => | ||
Promise.resolve({ | ||
valid: false, | ||
}) | ||
); | ||
|
||
const event = { phoneNumber: 'invalid' }; | ||
sendOtpFunction(testContext, event, callback); | ||
}); | ||
|
||
test('sends an otp with valid phone number', (done) => { | ||
const callback = (_err, result) => { | ||
expect(result).toBeDefined(); | ||
expect(result.success).toEqual(true); | ||
expect(result.message).toEqual('Verification sent to +17341234567'); | ||
done(); | ||
}; | ||
|
||
const event = { phoneNumber: '+17341234567' }; | ||
sendOtpFunction(testContext, event, callback); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const verifyOtpFunction = require('../functions/verify-otp').handler; | ||
const helpers = require('../../test/test-helper'); | ||
|
||
const mockCheck = { | ||
create: jest.fn(() => Promise.resolve({ sid: 'VAxxx', status: 'approved' })), | ||
}; | ||
const mockClient = { | ||
verify: { | ||
v2: { | ||
services: jest.fn(() => ({ | ||
verificationChecks: mockCheck, | ||
})), | ||
}, | ||
}, | ||
}; | ||
|
||
const testContext = { | ||
VERIFY_SERVICE_SID: 'default', | ||
getTwilioClient: () => mockClient, | ||
}; | ||
|
||
describe('verify/send-otp', () => { | ||
beforeAll(() => { | ||
helpers.setup({}); | ||
}); | ||
afterAll(() => { | ||
helpers.teardown(); | ||
}); | ||
|
||
test('returns an error with VERIFY_SERVICE_SID is missing', (done) => { | ||
const callback = (_err, result) => { | ||
expect(result).toBeDefined(); | ||
expect(result.success).toEqual(false); | ||
expect(result.message).toEqual('Missing VERIFY_SERVICE_SID'); | ||
done(); | ||
}; | ||
|
||
const event = { phoneNumber: '+17341234567', code: '123456' }; | ||
const contextWithoutServiceSid = { getTwilioClient: () => mockClient }; | ||
verifyOtpFunction(contextWithoutServiceSid, event, callback); | ||
}); | ||
|
||
test('returns an error with invalid code', (done) => { | ||
const callback = (_err, result) => { | ||
expect(result.success).toEqual(false); | ||
expect(result.message).toEqual('Verification failed. Status: pending'); | ||
done(); | ||
}; | ||
|
||
mockCheck.create.mockImplementationOnce(() => | ||
Promise.resolve({ | ||
status: 'pending', | ||
}) | ||
); | ||
|
||
const event = { phoneNumber: '+17341234567', code: '777777' }; | ||
verifyOtpFunction(testContext, event, callback); | ||
}); | ||
|
||
test('returns success with approved otp', (done) => { | ||
const callback = (_err, result) => { | ||
expect(result).toBeDefined(); | ||
expect(result.success).toEqual(true); | ||
expect(result.verificationSid).toEqual('VAxxx'); | ||
done(); | ||
}; | ||
|
||
const event = { phoneNumber: '+17341234567', code: '123456' }; | ||
verifyOtpFunction(testContext, event, callback); | ||
}); | ||
}); |