Skip to content

Instantly share code, notes, and snippets.

@srveit
Created October 22, 2015 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srveit/d60568ca341b22ec907c to your computer and use it in GitHub Desktop.
Save srveit/d60568ca341b22ec907c to your computer and use it in GitHub Desktop.
var Q = require('q'),
twilio = require('twilio');
var accountVerification = require('../lib/helpers/accountVerification');
describe('accountVerification', function () {
var phoneNumbers, phoneNumbersGet, phoneNumbersGetDefer,
sendMessage, sendMessageDefer,
messages, messagesGet, messagesGetDefer;
beforeEach(function () {
// Mock LookupsClient API
phoneNumbersGetDefer = Q.defer();
phoneNumbersGet = jasmine.createSpy('phoneNumbersGet')
.and.returnValue(phoneNumbersGetDefer.promise);
phoneNumbers = jasmine.createSpy('phoneNumbers')
.and.returnValue({get: phoneNumbersGet});
spyOn(twilio, 'LookupsClient').and.callFake(function () {
this.phoneNumbers = phoneNumbers;
});
// Mock RestClient API
sendMessageDefer = Q.defer();
sendMessage = jasmine.createSpy('sendMessage')
.and.returnValue(sendMessageDefer.promise);
messagesGetDefer = Q.defer();
messagesGet = jasmine.createSpy('messagesGet')
.and.returnValue(messagesGetDefer.promise);
messages = jasmine.createSpy('messages')
.and.returnValue({get: messagesGet});
spyOn(twilio, 'RestClient').and.callFake(function () {
this.sendMessage = sendMessage;
this.accounts = {
messages: messages
};
});
// Don't display console.log
spyOn(console, 'log');
});
it('should exist', function () {
expect(accountVerification).not.toBeUndefined();
});
describe('sendVerificationCode', function () {
var sendVerificationCode,
phone = '3145551212',
email = 'user@example.com';
beforeEach(function () {
sendVerificationCode = accountVerification.sendVerificationCode;
});
it('should be a function', function () {
expect(sendVerificationCode).toEqual(jasmine.any(Function));
});
describe('when called', function () {
var success, failure;
beforeEach(function () {
success = jasmine.createSpy('success');
failure = jasmine.createSpy('failure');
sendVerificationCode(phone, email)
.then(success)
.catch(failure);
});
it('should call LookupsClient', function () {
expect(twilio.LookupsClient).toHaveBeenCalled();
});
it('should call LookupsClient.phoneNumbers', function () {
expect(phoneNumbers).toHaveBeenCalledWith(phone);
});
it('should call LookupsClient.phoneNumbers().get', function () {
expect(phoneNumbersGet).toHaveBeenCalledWith(jasmine.objectContaining({
type: 'carrier'
}));
});
describe('and get succeeds', function () {
var phoneInfo;
beforeEach(function (done) {
phoneInfo = {
country_code: 'US',
phone_number: '+13145551212',
national_format: '(314) 555-1212',
carrier:
{
mobile_country_code: '310',
mobile_network_code: '150',
name: 'AT&T Wireless',
type: 'mobile',
error_code: null,
mobileCountryCode: '310',
mobileNetworkCode: '150',
errorCode: null
},
url: 'https://lookups.twilio.com/v1/PhoneNumbers/+13145551212?Type=carrier',
countryCode: 'US',
phoneNumber: '+13145551212',
nationalFormat: '(314) 555-1212'
};
phoneNumbersGetDefer.resolve(phoneInfo);
process.nextTick(done);
});
it('should create new RestClient', function () {
expect(twilio.RestClient).toHaveBeenCalled();
});
it('should call RestClient.sendMessage', function () {
expect(sendMessage).toHaveBeenCalledWith(jasmine.objectContaining({
to: phoneInfo.phone_number
}));
});
describe('and RestClient.sendMessage succeeds', function () {
var message;
beforeEach(function (done) {
twilio.RestClient.calls.reset();
message = {
sid: 'sid',
status: 'queued',
body: 'verification code',
to: phoneInfo.phone_number
};
sendMessageDefer.resolve(message);
process.nextTick(done);
});
it('should create new RestClient', function () {
expect(twilio.RestClient).toHaveBeenCalled();
});
it('should call RestClient.accounts.messages', function () {
expect(messages).toHaveBeenCalledWith(message.sid);
});
it('should call RestClient.accounts.messages.get', function () {
expect(messagesGet).toHaveBeenCalled();
});
describe('and RestClient.accounts.messages.get succeeds', function () {
beforeEach(function (done) {
message = {
sid: 'sid',
status: 'delivered',
body: 'verification code',
to: phoneInfo.phone_number
};
messagesGetDefer.resolve(message);
process.nextTick(done);
});
it('should succeed', function () {
expect(success).toHaveBeenCalled();
});
it('should not fail', function () {
expect(failure).not.toHaveBeenCalled();
});
describe('and the results', function () {
var results;
beforeEach(function () {
results = success.calls.argsFor(0)[0];
});
it('should have userInfo', function () {
expect(results.userInfo).toEqual(jasmine.objectContaining({
phone: phone,
email: email,
country: 'US',
isActivated: false,
isBlocked: false
}));
});
it('should have code', function () {
expect(results.code).toEqual(jasmine.objectContaining({
string: jasmine.any(String),
isVerified: false
}));
expect(results.code.string.length).toBe(6);
});
});
});
describe('and RestClient.accounts.messages.get fails', function () {
var error;
beforeEach(function (done) {
error = {
message: 'get message failed'
};
messagesGetDefer.reject(error);
process.nextTick(done);
});
it('should not succeed', function () {
expect(success).not.toHaveBeenCalled();
});
it('should fail', function () {
expect(failure).toHaveBeenCalledWith(jasmine.objectContaining({
message: error.message
}));
});
});
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment