1
+ var expect = require ( "expect.js" ) ;
2
+ var NotificationClient = require ( "../../../lib/notification_client" ) ;
3
+ var nock = require ( 'nock' ) ;
4
+
5
+ describe ( "NativeNotificationClient" , function ( ) {
6
+ var client ;
7
+
8
+ beforeEach ( function ( ) {
9
+ client = new NotificationClient ( { appId : 1234 , key : "f00d" , secret : "beef" } ) ;
10
+ nock . cleanAll ( ) ;
11
+ nock . disableNetConnect ( ) ;
12
+ } ) ;
13
+
14
+ afterEach ( function ( ) {
15
+ nock . cleanAll ( ) ;
16
+ nock . enableNetConnect ( ) ;
17
+ } ) ;
18
+
19
+ xit ( "should send in the success case" , function ( done ) {
20
+ var mock = nock ( "nativepushclient-cluster1.pusher.com:80" )
21
+ client . notify ( [ 'yolo' ] , {
22
+ 'apns' : {
23
+ 'aps' : {
24
+ 'alert' :{
25
+ 'title' : 'yolo' ,
26
+ 'body' : 'woot'
27
+ }
28
+ }
29
+ } ,
30
+ 'gcm' : {
31
+ 'notification' : {
32
+ 'title' : 'huzzah' ,
33
+ 'icon' : 'woot'
34
+ }
35
+ }
36
+
37
+ } , function ( ) {
38
+ expect ( mock . isDone ( ) ) . to . be ( true ) ;
39
+ done ( ) ;
40
+ } ) ;
41
+ } ) ;
42
+
43
+ it ( "should remove restricted GCM keys" , function ( ) {
44
+ var notification = {
45
+ 'gcm' : {
46
+ 'to' : 'woot' ,
47
+ 'registration_ids' : [ 'woot' , 'bla' ] ,
48
+ 'notification' : {
49
+ 'title' : 'yipee' ,
50
+ 'icon' : 'huh'
51
+ }
52
+ }
53
+ } ;
54
+
55
+ client . validateNotification ( notification ) ;
56
+ expect ( notification ) . to . eql ( { gcm : { notification : { title : 'yipee' , icon : 'huh' } } } ) ;
57
+ } ) ;
58
+
59
+ it ( "should validate that either apns or gcm are present" , function ( ) {
60
+ var notification = { } ;
61
+ expect ( function ( ) {
62
+ client . validateNotification ( notification ) ;
63
+ } ) . to . throwException ( / N o t i f i c a t i o n m u s t h a v e f i e l d s A P N S o r G C M / ) ;
64
+ } ) ;
65
+
66
+ it ( "should validate that only one interest is sent to" , function ( ) {
67
+ expect ( function ( ) {
68
+ client . notify ( [ 'yolo' , 'woot' ] , { } ) ;
69
+ } ) . to . throwException ( / C u r r e n t l y s e n d i n g t o m o r e t h a n o n e i n t e r e s t i s u n s u p p o r t e d / ) ;
70
+ } )
71
+
72
+ it ( "should invalidate certain gcm payloads" , function ( ) {
73
+ var invalidGcmPayloads = [
74
+ {
75
+ 'gcm' : {
76
+ 'time_to_live' : - 1 ,
77
+ 'notification' : {
78
+ 'title' : 'yipee' ,
79
+ 'icon' : 'huh'
80
+ }
81
+ } ,
82
+ exception : / G C M t i m e _ t o _ l i v e m u s t b e b e t w e e n 0 a n d 2 4 1 9 2 0 \( 4 w e e k s \) /
83
+ } ,
84
+ {
85
+ 'gcm' : {
86
+ 'time_to_live' : 241921 ,
87
+ 'notification' : {
88
+ 'title' : 'yipee' ,
89
+ 'icon' : 'huh'
90
+ }
91
+ } ,
92
+ exception : / G C M t i m e _ t o _ l i v e m u s t b e b e t w e e n 0 a n d 2 4 1 9 2 0 \( 4 w e e k s \) /
93
+ } ,
94
+ {
95
+ 'gcm' : {
96
+ 'notification' : {
97
+ 'title' : 'yipee' ,
98
+ }
99
+ } ,
100
+ exception : / G C M i c o n m u s t b e a s t r i n g a n d n o t e m p t y /
101
+ } ,
102
+ {
103
+ 'gcm' : {
104
+ 'notification' : {
105
+ 'icon' : 'huh'
106
+ }
107
+ } ,
108
+ exception : / G C M t i t l e m u s t b e a s t r i n g a n d n o t e m p t y /
109
+ } ,
110
+ {
111
+ 'gcm' : {
112
+ 'notification' : {
113
+ 'title' : '' ,
114
+ 'icon' : 'huh'
115
+ }
116
+ } ,
117
+ exception : / G C M t i t l e m u s t b e a s t r i n g a n d n o t e m p t y /
118
+ } ,
119
+ {
120
+ 'gcm' : {
121
+ 'notification' : {
122
+ 'title' : 'yipee' ,
123
+ 'icon' : ''
124
+ }
125
+ } ,
126
+ exception : / G C M i c o n m u s t b e a s t r i n g a n d n o t e m p t y /
127
+ }
128
+ ]
129
+
130
+ for ( var index in invalidGcmPayloads ) {
131
+ var invalidPayload = invalidGcmPayloads [ index ] ;
132
+ expect ( function ( ) {
133
+ client . notify ( [ 'yolo' ] , invalidPayload ) ;
134
+ } ) . to . throwException ( invalidPayload . exception || / % / ) ;
135
+ }
136
+ } ) ;
137
+
138
+ it ( "validates webhook config" , function ( ) {
139
+ invalidWebhookConfig = [
140
+ {
141
+ 'webhook_level' : 'DEBUG' ,
142
+ 'apns' : {
143
+ 'alert' : {
144
+ 'title' : 'yolo' ,
145
+ 'body' : 'woot'
146
+ }
147
+ } ,
148
+ 'gcm' : {
149
+ 'notification' : {
150
+ 'title' : 'yipee' ,
151
+ 'icon' : 'huh'
152
+ }
153
+ } ,
154
+ exception : 'webhook_level cannot be used without a webhook_url'
155
+ } ,
156
+ {
157
+ 'webhook_level' : 'FOOBAR' ,
158
+ 'webhook_url' : 'http://webhook.com' ,
159
+ 'apns' : {
160
+ 'alert' : {
161
+ 'title' : 'yolo' ,
162
+ 'body' : 'woot'
163
+ }
164
+ } ,
165
+ 'gcm' : {
166
+ 'notification' : {
167
+ 'title' : 'yipee' ,
168
+ 'icon' : 'huh'
169
+ }
170
+ } ,
171
+ exception : 'webhook_level must be either INFO or DEBUG. Blank will default to INFO'
172
+ } ] ;
173
+
174
+ for ( var index in invalidWebhookConfig ) {
175
+ var invalidPayload = invalidWebhookConfig [ index ] ;
176
+ expect ( function ( ) {
177
+ client . notify ( [ 'yolo' ] , invalidPayload ) ;
178
+ } ) . to . throwException ( new RegExp ( invalidPayload . exception ) ) ;
179
+ }
180
+ } )
181
+ } )
0 commit comments