@@ -20,9 +20,11 @@ exports.tearDown = function(callback) {
20
20
restore ( [
21
21
transition . filter ,
22
22
transition . _isMessageEmpty ,
23
+ transition . _isMessageFromGateway ,
23
24
transition . _isFormNotFound ,
24
25
transition . _isConfigFormsOnlyMode ,
25
26
transition . _isReportedAfterStartDate ,
27
+ transition . _isResponseAllowed ,
26
28
transition . _getConfig ,
27
29
transition . _getLocale ,
28
30
transition . _translate ,
@@ -75,54 +77,96 @@ exports['when doc has errors still pass filter'] = function(test) {
75
77
test . done ( ) ;
76
78
} ;
77
79
78
- exports [ 'do not pass filter when message is from gateway' ] = function ( test ) {
79
- sinon . stub ( transition , '_getConfig ' ) . returns ( '+774455558888' ) ;
80
+ exports [ 'filter passes when message is from gateway' ] = function ( test ) {
81
+ sinon . stub ( transition , '_isMessageFromGateway ' ) . returns ( true ) ;
80
82
test . equals ( transition . filter ( {
81
- from : '+222' ,
82
- type : 'data_record' ,
83
- sms_message : {
84
- from : '+774455558888'
85
- }
86
- } ) , false ) ;
83
+ from : 'x' ,
84
+ type : 'data_record'
85
+ } ) , true ) ;
87
86
test . done ( ) ;
88
87
} ;
89
88
90
- exports [ 'do not pass filter when gateway config is missing country code ' ] = function ( test ) {
91
- sinon . stub ( transition , '_getConfig ' ) . returns ( '446681800' ) ;
89
+ exports [ 'filter passes when message is not from gateway ' ] = function ( test ) {
90
+ sinon . stub ( transition , '_isMessageFromGateway ' ) . returns ( false ) ;
92
91
test . equals ( transition . filter ( {
93
- from : '+222' ,
94
- type : 'data_record' ,
95
- sms_message : {
96
- from : '+41446681800'
97
- }
98
- } ) , false ) ;
92
+ from : 'x' ,
93
+ type : 'data_record'
94
+ } ) , true ) ;
95
+ test . done ( ) ;
96
+ } ;
97
+
98
+ exports [ 'describe _isMessageFromGateway' ] = function ( test ) {
99
+ var tests = [
100
+ [ '+774455558888' , '77-44-5555-8888' , true ] ,
101
+ [ '+774455558889' , '77-44-5555-8888' , false ] ,
102
+ // missing country code matches
103
+ [ '+41446681800' , '446681800' , true ]
104
+ ] ;
105
+ _ . each ( tests , function ( t ) {
106
+ var s = sinon . stub ( transition , '_getConfig' ) ;
107
+ s . withArgs ( 'gateway_number' ) . returns ( t [ 0 ] ) ;
108
+ test . equals ( transition . _isMessageFromGateway ( { from : t [ 1 ] } ) , t [ 2 ] ) ;
109
+ s . restore ( ) ;
110
+ } ) ;
99
111
test . done ( ) ;
100
112
} ;
101
113
102
- exports [ 'do not pass filter when numbers are same but different formats' ] = function ( test ) {
103
- sinon . stub ( transition , '_getConfig' ) . returns ( '77-44-5555-8888' ) ;
114
+ exports [ 'filter passes when response is allowed' ] = function ( test ) {
115
+ // Filter passes because message is added with a 'denied' state.
116
+ sinon . stub ( transition , '_isResponseAllowed' ) . returns ( true ) ;
104
117
test . equals ( transition . filter ( {
105
- from : '+222' ,
106
- type : 'data_record' ,
107
- sms_message : {
108
- from : '+774455558888'
109
- }
110
- } ) , false ) ;
118
+ from : 'x' ,
119
+ type : 'data_record'
120
+ } ) , true ) ;
111
121
test . done ( ) ;
112
122
} ;
113
123
114
- exports [ 'pass filter when message is not from gateway' ] = function ( test ) {
115
- sinon . stub ( transition , '_getConfig' ) . returns ( '+774455558889' )
124
+ exports [ 'filter passes when response is not allowed' ] = function ( test ) {
125
+ // Filter passes because message is added with a 'denied' state.
126
+ sinon . stub ( transition , '_isResponseAllowed' ) . returns ( false ) ;
116
127
test . equals ( transition . filter ( {
117
- from : '+222' ,
118
- type : 'data_record' ,
119
- sms_message : {
120
- from : '+774455558888'
121
- }
128
+ from : 'x' ,
129
+ type : 'data_record'
122
130
} ) , true ) ;
123
131
test . done ( ) ;
124
132
} ;
125
133
134
+ exports [ 'describe _isResponseAllowed' ] = function ( test ) {
135
+ /*
136
+ * Support comma separated string config to match an outgoing phone number
137
+ * or MNO (mobile network operator) defined string.
138
+ */
139
+ var tests = [
140
+ // denied
141
+ [ '+123' , '+123' , false ] ,
142
+ [ '+123' , '+123999999' , false ] ,
143
+ [ 'SAFARI' , 'SAFARICOM' , false ] ,
144
+ [ 'Safari' , 'SAFARICOM' , false ] ,
145
+ [ '+123,+456,+789' , '+456' , false ] ,
146
+ [ '+123,+456,+789' , '+4569999999' , false ] ,
147
+ [ 'SAFARI, ORANGE' , 'ORANGE NET' , false ] ,
148
+ [ '0' , '0000123' , false ] ,
149
+ [ '0' , '0' , false ] ,
150
+ // allowed
151
+ [ '+123' , '+999' , true ] ,
152
+ [ 'SAFARI, ORANGE NET' , 'ORANGE' , true ] ,
153
+ [ 'VIVO' , 'EM VIVO' , true ] ,
154
+ [ '0' , '-1' , true ] ,
155
+ // allow falsey inputs
156
+ [ 'snarf' , undefined , true ] ,
157
+ [ 'snarf' , null , true ] ,
158
+ [ '' , '+123' , true ] ,
159
+ [ '' , '' , true ]
160
+ ] ;
161
+ _ . each ( tests , function ( t ) {
162
+ var s = sinon . stub ( transition , '_getConfig' ) ;
163
+ s . withArgs ( 'outgoing_deny_list' ) . returns ( t [ 0 ] ) ;
164
+ test . equals ( transition . _isResponseAllowed ( { from : t [ 1 ] } ) , t [ 2 ] ) ;
165
+ s . restore ( ) ;
166
+ } ) ;
167
+ test . done ( ) ;
168
+ } ;
169
+
126
170
exports [ 'do nothing if reported date is not after config start date' ] = function ( test ) {
127
171
transition . _isReportedAfterStartDate . restore ( ) ;
128
172
sinon . stub ( transition , '_isReportedAfterStartDate' ) . returns ( false ) ;
@@ -162,14 +206,14 @@ exports['when doc has no errors, form is not found returns false'] = function(te
162
206
163
207
exports [ 'isReportedAfterStartDate returns false if config start date is whitespace' ] = function ( test ) {
164
208
transition . _isReportedAfterStartDate . restore ( ) ;
165
- sinon . stub ( transition , '_getConfig' ) . returns ( { start_date : ' ' } ) ;
209
+ sinon . stub ( transition , '_getConfig' ) . withArgs ( 'default_responses' ) . returns ( { start_date : ' ' } ) ;
166
210
test . equals ( transition . _isReportedAfterStartDate ( { } ) , false ) ;
167
211
test . done ( ) ;
168
212
} ;
169
213
170
214
exports [ 'isReportedAfterStartDate returns true when reported date is after start date' ] = function ( test ) {
171
215
transition . _isReportedAfterStartDate . restore ( ) ;
172
- sinon . stub ( transition , '_getConfig' ) . returns ( { start_date : '2014-01-01' } ) ;
216
+ sinon . stub ( transition , '_getConfig' ) . withArgs ( 'default_responses' ) . returns ( { start_date : '2014-01-01' } ) ;
173
217
test . equals ( transition . _isReportedAfterStartDate ( {
174
218
reported_date : 1412641215000
175
219
} ) , true ) ;
@@ -178,7 +222,7 @@ exports['isReportedAfterStartDate returns true when reported date is after start
178
222
179
223
exports [ 'isReportedAfterStartDate returns false when reported date is before start date' ] = function ( test ) {
180
224
transition . _isReportedAfterStartDate . restore ( ) ;
181
- sinon . stub ( transition , '_getConfig' ) . returns ( { start_date : '2014-12-01' } ) ;
225
+ sinon . stub ( transition , '_getConfig' ) . withArgs ( 'default_responses' ) . returns ( { start_date : '2014-12-01' } ) ;
182
226
test . equals ( transition . _isReportedAfterStartDate ( {
183
227
reported_date : 1412641215000
184
228
} ) , false ) ;
@@ -188,7 +232,7 @@ exports['isReportedAfterStartDate returns false when reported date is before sta
188
232
exports [ 'add response if unstructured message and setting enabled' ] = function ( test ) {
189
233
190
234
sinon . stub ( transition , '_isConfigFormsOnlyMode' ) . returns ( false ) ;
191
- sinon . stub ( config , 'get' ) . returns ( [
235
+ sinon . stub ( config , 'get' ) . withArgs ( 'translations' ) . returns ( [
192
236
{
193
237
'key' : 'sms_received' ,
194
238
'default' : 'SMS rcvd, thx!'
@@ -218,8 +262,7 @@ exports['add response if unstructured message and setting enabled'] = function(t
218
262
} ;
219
263
220
264
exports [ 'add response if unstructured message (form prop is undefined)' ] = function ( test ) {
221
- // stub the translations config
222
- sinon . stub ( config , 'get' ) . returns ( [
265
+ sinon . stub ( config , 'get' ) . withArgs ( 'translations' ) . returns ( [
223
266
{
224
267
'key' : 'sms_received' ,
225
268
'default' : 'SMS rcvd, thx!'
@@ -253,7 +296,7 @@ exports['do not add response if valid form'] = function(test) {
253
296
* on different transition.
254
297
*/
255
298
256
- sinon . stub ( config , 'get' ) . returns ( [
299
+ sinon . stub ( config , 'get' ) . withArgs ( 'translations' ) . returns ( [
257
300
{
258
301
'key' : 'sms_received' ,
259
302
'default' : 'SMS rcvd, thx!'
@@ -278,7 +321,7 @@ exports['do not add response if valid form'] = function(test) {
278
321
exports [ 'add response if form not found' ] = function ( test ) {
279
322
sinon . stub ( transition , '_isConfigFormsOnlyMode' ) . returns ( false ) ;
280
323
// stub the translations config
281
- sinon . stub ( config , 'get' ) . returns ( [
324
+ sinon . stub ( config , 'get' ) . withArgs ( 'translations' ) . returns ( [
282
325
{
283
326
'key' : 'sms_received' ,
284
327
'default' : 'SMS rcvd, thx!'
@@ -311,7 +354,7 @@ exports['add response if form not found'] = function(test) {
311
354
exports [ 'add response if form not found and forms_only_mode' ] = function ( test ) {
312
355
sinon . stub ( transition , '_isConfigFormsOnlyMode' ) . returns ( true ) ;
313
356
// stub the translations config
314
- sinon . stub ( config , 'get' ) . returns ( [
357
+ sinon . stub ( config , 'get' ) . withArgs ( 'translations' ) . returns ( [
315
358
{
316
359
'key' : 'form_not_found' ,
317
360
'default' : 'Form was not recognized.'
@@ -345,7 +388,7 @@ exports['add response if form not found and respect locale'] = function(test) {
345
388
sinon . stub ( transition , '_isConfigFormsOnlyMode' ) . returns ( false ) ;
346
389
sinon . stub ( transition , '_getLocale' ) . returns ( 'fr' ) ;
347
390
// stub the translations config
348
- sinon . stub ( config , 'get' ) . returns ( [
391
+ sinon . stub ( config , 'get' ) . withArgs ( 'translations' ) . returns ( [
349
392
{
350
393
key : 'sms_received' ,
351
394
default : 'SMS message rcvd' ,
@@ -393,7 +436,7 @@ exports['add response if form not found and respect locale'] = function(test) {
393
436
exports [ 'add response to empty message' ] = function ( test ) {
394
437
sinon . stub ( transition , '_isConfigFormsOnlyMode' ) . returns ( false ) ;
395
438
// stub the translations config
396
- sinon . stub ( config , 'get' ) . returns ( [
439
+ sinon . stub ( config , 'get' ) . withArgs ( 'translations' ) . returns ( [
397
440
{
398
441
'key' : 'empty' ,
399
442
'default' : 'SMS appears empty.'
@@ -423,3 +466,57 @@ exports['add response to empty message'] = function (test) {
423
466
} ) ;
424
467
} ;
425
468
469
+ exports [ 'add response when recipient is allowed' ] = function ( test ) {
470
+ sinon . stub ( transition , '_isResponseAllowed' ) . returns ( true ) ;
471
+ sinon . stub ( config , 'get' ) . withArgs ( 'translations' ) . returns ( [
472
+ {
473
+ 'key' : 'sms_received' ,
474
+ 'default' : 'ahoy mate'
475
+ }
476
+ ] ) ;
477
+ var messageFn = sinon . spy ( messages , 'addMessage' ) ;
478
+ test . expect ( 4 ) ;
479
+ var doc = {
480
+ from : 'x' ,
481
+ type : 'data_record'
482
+ } ;
483
+ transition . onMatch ( { doc : doc } , { } , { } , function ( err , changed ) {
484
+ test . ok ( messageFn . calledOnce ) ;
485
+ test . ok ( messageFn . calledWith ( {
486
+ doc : doc ,
487
+ phone : 'x' ,
488
+ message : 'ahoy mate'
489
+ } ) ) ;
490
+ test . equals ( err , null ) ;
491
+ test . equals ( changed , true ) ;
492
+ test . done ( ) ;
493
+ } ) ;
494
+ } ;
495
+
496
+ exports [ 'add response with denied state when recipient is denied' ] = function ( test ) {
497
+ sinon . stub ( transition , '_isResponseAllowed' ) . returns ( false ) ;
498
+ sinon . stub ( config , 'get' ) . withArgs ( 'translations' ) . returns ( [
499
+ {
500
+ 'key' : 'sms_received' ,
501
+ 'default' : 'ahoy mate'
502
+ }
503
+ ] ) ;
504
+ var messageFn = sinon . spy ( messages , 'addMessage' ) ;
505
+ test . expect ( 4 ) ;
506
+ var doc = {
507
+ from : 'x' ,
508
+ type : 'data_record'
509
+ } ;
510
+ transition . onMatch ( { doc : doc } , { } , { } , function ( err , changed ) {
511
+ test . ok ( messageFn . calledOnce ) ;
512
+ test . ok ( messageFn . calledWith ( {
513
+ doc : doc ,
514
+ phone : 'x' ,
515
+ message : 'ahoy mate' ,
516
+ state : 'denied'
517
+ } ) ) ;
518
+ test . equals ( err , null ) ;
519
+ test . equals ( changed , true ) ;
520
+ test . done ( ) ;
521
+ } ) ;
522
+ } ;
0 commit comments