Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 2598fb1

Browse files
committed
Update tests with twilio client mocks
1 parent b2375cb commit 2598fb1

File tree

6 files changed

+108
-23
lines changed

6 files changed

+108
-23
lines changed

.env.example

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
TWILIO_ACCOUNT_SID=
2-
TWILIO_AUTH_TOKEN=
3-
TWILIO_NUMBER=
1+
TWILIO_ACCOUNT_SID=your_account_sid
2+
TWILIO_AUTH_TOKEN=your_account_secret
3+
TWILIO_NUMBER=the_twilio_number_you_purchased

config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var cfg = {};
44
if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') {
55
dotenv.config({path: '.env'});
66
} else {
7-
dotenv.config({path: '.env.test', silent: true});
7+
dotenv.config({path: '.env.example', silent: true});
88
}
99

1010
// HTTP Port to run our web application

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Send SMS notifications on exceptions thrown by your Node.js application",
55
"main": "index.js",
66
"scripts": {
7-
"test": "export NODE_ENV=test && mocha test"
7+
"test": "NODE_ENV=test node_modules/.bin/mocha test"
88
},
99
"repository": {
1010
"type": "git",
@@ -24,7 +24,9 @@
2424
"url": "https://github.com/TwilioDevEd/server-notifications-node/issues"
2525
},
2626
"homepage": "https://github.com/TwilioDevEd/server-notifications-node",
27-
"engines" : { "node" : ">=4.1.0 <5.5.0" },
27+
"engines": {
28+
"node": ">=4.1.0"
29+
},
2830
"dependencies": {
2931
"body-parser": "^1.12.0",
3032
"connect-flash": "^0.1.1",
@@ -40,6 +42,8 @@
4042
"devDependencies": {
4143
"chai": "^3.5.0",
4244
"mocha": "^3.1.2",
45+
"mockery": "^2.0.0",
46+
"sinon": "^2.1.0",
4347
"supertest": "^2.0.1"
4448
}
4549
}

test/testTwilioNotifications.js

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,51 @@
11
var expect = require('chai').expect;
22
var supertest = require('supertest');
3+
var mockery = require('mockery');
4+
var stub = require('sinon').stub;
35

46
var app = require('../webapp');
57
var config = require('../config');
68

79
describe('Twilio notifications on error', function() {
810
var agent = supertest(app);
11+
var msgCreateStub;
12+
13+
before(() => {
14+
// mockery.deregisterAll();
15+
mockery.enable({
16+
useCleanCache: true,
17+
warnOnReplace: false,
18+
warnOnUnregistered: false
19+
});
20+
21+
msgCreateStub = stub().returns(Promise.resolve({}));
22+
23+
function TwilioMock() {
24+
return {
25+
api: {
26+
messages: {
27+
create: msgCreateStub,
28+
},
29+
},
30+
}
31+
}
32+
33+
mockery.registerMock('twilio', TwilioMock);
34+
});
35+
36+
after(function () {
37+
mockery.deregisterAll();
38+
mockery.disable();
39+
});
940

1041
describe('GET /error', function() {
11-
it('should return an error', function(done) {
12-
agent
42+
it('should return an error', function() {
43+
return agent
1344
.get('/error')
1445
.expect(function(res) {
1546
expect(res.status).to.equal(500);
16-
})
17-
.end(done);
18-
});
47+
expect(msgCreateStub.calledTwice).to.be.true;
48+
});
49+
});
1950
});
2051
});

test/twilioClient.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var mockery = require('mockery');
2+
var stub = require('sinon').stub;
3+
var expect = require('chai').expect;
4+
5+
describe('twilioClient', function(){
6+
var msgCreateStub;
7+
8+
before(function(){
9+
mockery.enable({
10+
useCleanCache: true,
11+
warnOnReplace: false,
12+
warnOnUnregistered: false
13+
});
14+
15+
msgCreateStub = stub().returns(Promise.resolve({}));
16+
17+
function TwilioMock() {
18+
return {
19+
api: {
20+
messages: {
21+
create: msgCreateStub,
22+
},
23+
},
24+
}
25+
}
26+
27+
mockery.registerMock('twilio', TwilioMock);
28+
});
29+
30+
after(function () {
31+
mockery.deregisterAll();
32+
mockery.disable();
33+
});
34+
35+
it('should send sms message and return promise with result', function() {
36+
// Arrange
37+
var twilioClient = require('../twilioClient');
38+
var toNumber = '+15555555555';
39+
var message = 'test message';
40+
41+
// Act
42+
return twilioClient
43+
.sendSms(toNumber, message)
44+
.then(() => {
45+
// Assert
46+
expect(msgCreateStub.called).to.be.true;
47+
});
48+
});
49+
});

twilioClient.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
var config = require('./config');
2-
var client = require('twilio')(config.accountSid, config.authToken);
32

43
module.exports.sendSms = function(to, message) {
5-
client.messages.create({
6-
body: message,
7-
to: to,
8-
from: config.sendingNumber,
9-
mediaUrl: imageUrl
10-
}).then(function(data) {
11-
console.log('Administrator notified');
12-
}).catch(function(err) {
13-
console.error('Could not notify administrator');
14-
console.error(err);
15-
});
4+
var client = require('twilio')(config.accountSid, config.authToken);
5+
// console.log(client.api.messages.create())
6+
return client.api.messages
7+
.create({
8+
body: message,
9+
to: to,
10+
from: config.sendingNumber,
11+
}).then(function(data) {
12+
console.log('Administrator notified');
13+
}).catch(function(err) {
14+
console.error('Could not notify administrator');
15+
console.error(err);
16+
});
1617
};

0 commit comments

Comments
 (0)