This repository was archived by the owner on Aug 8, 2023. It is now read-only.
File tree 6 files changed +108
-23
lines changed
6 files changed +108
-23
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ var cfg = {};
4
4
if ( process . env . NODE_ENV !== 'production' && process . env . NODE_ENV !== 'test' ) {
5
5
dotenv . config ( { path : '.env' } ) ;
6
6
} else {
7
- dotenv . config ( { path : '.env.test ' , silent : true } ) ;
7
+ dotenv . config ( { path : '.env.example ' , silent : true } ) ;
8
8
}
9
9
10
10
// HTTP Port to run our web application
Original file line number Diff line number Diff line change 4
4
"description" : " Send SMS notifications on exceptions thrown by your Node.js application" ,
5
5
"main" : " index.js" ,
6
6
"scripts" : {
7
- "test" : " export NODE_ENV=test && mocha test"
7
+ "test" : " NODE_ENV=test node_modules/.bin/ mocha test"
8
8
},
9
9
"repository" : {
10
10
"type" : " git" ,
24
24
"url" : " https://github.com/TwilioDevEd/server-notifications-node/issues"
25
25
},
26
26
"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
+ },
28
30
"dependencies" : {
29
31
"body-parser" : " ^1.12.0" ,
30
32
"connect-flash" : " ^0.1.1" ,
40
42
"devDependencies" : {
41
43
"chai" : " ^3.5.0" ,
42
44
"mocha" : " ^3.1.2" ,
45
+ "mockery" : " ^2.0.0" ,
46
+ "sinon" : " ^2.1.0" ,
43
47
"supertest" : " ^2.0.1"
44
48
}
45
49
}
Original file line number Diff line number Diff line change 1
1
var expect = require ( 'chai' ) . expect ;
2
2
var supertest = require ( 'supertest' ) ;
3
+ var mockery = require ( 'mockery' ) ;
4
+ var stub = require ( 'sinon' ) . stub ;
3
5
4
6
var app = require ( '../webapp' ) ;
5
7
var config = require ( '../config' ) ;
6
8
7
9
describe ( 'Twilio notifications on error' , function ( ) {
8
10
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
+ } ) ;
9
40
10
41
describe ( 'GET /error' , function ( ) {
11
- it ( 'should return an error' , function ( done ) {
12
- agent
42
+ it ( 'should return an error' , function ( ) {
43
+ return agent
13
44
. get ( '/error' )
14
45
. expect ( function ( res ) {
15
46
expect ( res . status ) . to . equal ( 500 ) ;
16
- } )
17
- . end ( done ) ;
18
- } ) ;
47
+ expect ( msgCreateStub . calledTwice ) . to . be . true ;
48
+ } ) ;
49
+ } ) ;
19
50
} ) ;
20
51
} ) ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change 1
1
var config = require ( './config' ) ;
2
- var client = require ( 'twilio' ) ( config . accountSid , config . authToken ) ;
3
2
4
3
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
+ } ) ;
16
17
} ;
You can’t perform that action at this time.
0 commit comments