@@ -3,6 +3,7 @@ import express from 'express';
3
3
import auth from './auth' ;
4
4
import request from 'supertest' ;
5
5
import notifications from './notifications' ;
6
+ import { NotificationChannelType } from '@dm3-org/dm3-lib-delivery' ;
6
7
7
8
const keysA = {
8
9
encryptionKeyPair : {
@@ -20,7 +21,7 @@ const keysA = {
20
21
21
22
describe ( 'Notifications' , ( ) => {
22
23
describe ( 'get NotificationChannels' , ( ) => {
23
- it ( 'Returns 200 and an empty array when the user has no chanels set up ' , async ( ) => {
24
+ it ( 'Returns empty array as global notification is turned off ' , async ( ) => {
24
25
const app = express ( ) ;
25
26
app . use ( bodyParser . json ( ) ) ;
26
27
app . use ( notifications ( ) ) ;
@@ -46,6 +47,8 @@ describe('Notifications', () => {
46
47
return { } ;
47
48
} ,
48
49
getIdEnsName : async ( ensName : string ) => ensName ,
50
+ getGlobalNotification : async ( ensName : string ) =>
51
+ Promise . resolve ( { isEnabled : false } ) ,
49
52
getUsersNotificationChannels : async ( ensName : string ) =>
50
53
Promise . resolve ( [ ] ) ,
51
54
} ;
@@ -59,22 +62,151 @@ describe('Notifications', () => {
59
62
. set ( {
60
63
authorization : `Bearer ${ token } ` ,
61
64
} )
65
+ . send ( ) ;
66
+
67
+ expect ( status ) . toBe ( 200 ) ;
68
+ expect ( body ) . toEqual ( {
69
+ notificationChannels : [ ] ,
70
+ } ) ;
71
+ } ) ;
72
+
73
+ it ( 'Returns 200 with empty notification channels as global notification is turned on' , async ( ) => {
74
+ const app = express ( ) ;
75
+ app . use ( bodyParser . json ( ) ) ;
76
+ app . use ( notifications ( ) ) ;
77
+
78
+ const token = await createAuthToken ( ) ;
79
+
80
+ app . locals . db = {
81
+ getSession : async ( ensName : string ) =>
82
+ Promise . resolve ( {
83
+ challenge : '123' ,
84
+ token,
85
+ signedUserProfile : {
86
+ profile : {
87
+ publicSigningKey :
88
+ keysA . signingKeyPair . publicKey ,
89
+ } ,
90
+ } ,
91
+ } ) ,
92
+ setSession : async ( _ : string , __ : any ) => {
93
+ return ( _ : any , __ : any , ___ : any ) => { } ;
94
+ } ,
95
+ getUserStorage : async ( addr : string ) => {
96
+ return { } ;
97
+ } ,
98
+ getIdEnsName : async ( ensName : string ) => ensName ,
99
+ getUsersNotificationChannels : async ( ensName : string ) =>
100
+ Promise . resolve ( [ ] ) ,
101
+ getGlobalNotification : async ( ensName : string ) =>
102
+ Promise . resolve ( { isEnabled : true } ) ,
103
+ } ;
62
104
105
+ app . locals . web3Provider = {
106
+ resolveName : async ( ) =>
107
+ '0x71CB05EE1b1F506fF321Da3dac38f25c0c9ce6E1' ,
108
+ } ;
109
+ const { status, body } = await request ( app )
110
+ . get ( `/bob.eth` )
111
+ . set ( {
112
+ authorization : `Bearer ${ token } ` ,
113
+ } )
63
114
. send ( ) ;
64
115
65
116
expect ( status ) . toBe ( 200 ) ;
66
- expect ( body ) . toEqual ( [ ] ) ;
117
+ expect ( body ) . toEqual ( {
118
+ notificationChannels : [ ] ,
119
+ } ) ;
67
120
} ) ;
68
121
} ) ;
69
122
70
123
describe ( 'setUserStorage' , ( ) => {
71
- it ( 'User can setup email notifications' , async ( ) => {
124
+ it ( 'Returns 400 on setup email notifications as email ID is invalid' , async ( ) => {
125
+ const app = express ( ) ;
126
+ app . use ( bodyParser . json ( ) ) ;
127
+ app . use ( notifications ( ) ) ;
128
+
129
+ const token = await createAuthToken ( ) ;
130
+ const addUsersNotificationChannelMock = jest . fn ( ) ;
131
+
132
+ app . locals . db = {
133
+ getSession : async ( ensName : string ) =>
134
+ Promise . resolve ( {
135
+ challenge : '123' ,
136
+ token,
137
+ } ) ,
138
+ setSession : async ( _ : string , __ : any ) => {
139
+ return ( _ : any , __ : any , ___ : any ) => { } ;
140
+ } ,
141
+ setUserStorage : ( _ : string , __ : string ) => { } ,
142
+ getIdEnsName : async ( ensName : string ) => ensName ,
143
+ addUsersNotificationChannel : addUsersNotificationChannelMock ,
144
+ } ;
145
+
146
+ app . locals . web3Provider = {
147
+ resolveName : async ( ) =>
148
+ '0x71CB05EE1b1F506fF321Da3dac38f25c0c9ce6E1' ,
149
+ } ;
150
+
151
+ const { status } = await request ( app )
152
+ . post ( `/bob.eth` )
153
+ . set ( {
154
+ authorization : `Bearer ${ token } ` ,
155
+ } )
156
+ . send ( {
157
+ recipientValue : 'bob.eth' ,
158
+ notificationChannelType : NotificationChannelType . EMAIL ,
159
+ } ) ;
160
+
161
+ expect ( status ) . toBe ( 400 ) ;
162
+ } ) ;
163
+
164
+ it ( 'Returns 400 on setup email notifications as notificationChannelType is invalid' , async ( ) => {
72
165
const app = express ( ) ;
73
166
app . use ( bodyParser . json ( ) ) ;
74
167
app . use ( notifications ( ) ) ;
75
168
76
169
const token = await createAuthToken ( ) ;
170
+ const addUsersNotificationChannelMock = jest . fn ( ) ;
171
+
172
+ app . locals . db = {
173
+ getSession : async ( ensName : string ) =>
174
+ Promise . resolve ( {
175
+ challenge : '123' ,
176
+ token,
177
+ } ) ,
178
+ setSession : async ( _ : string , __ : any ) => {
179
+ return ( _ : any , __ : any , ___ : any ) => { } ;
180
+ } ,
181
+ setUserStorage : ( _ : string , __ : string ) => { } ,
182
+ getIdEnsName : async ( ensName : string ) => ensName ,
183
+ addUsersNotificationChannel : addUsersNotificationChannelMock ,
184
+ } ;
77
185
186
+ app . locals . web3Provider = {
187
+ resolveName : async ( ) =>
188
+ '0x71CB05EE1b1F506fF321Da3dac38f25c0c9ce6E1' ,
189
+ } ;
190
+
191
+ const { status } = await request ( app )
192
+ . post ( `/bob.eth` )
193
+ . set ( {
194
+ authorization : `Bearer ${ token } ` ,
195
+ } )
196
+ . send ( {
197
+ recipientValue :
'[email protected] ' ,
198
+ notificationChannelType : '' ,
199
+ } ) ;
200
+
201
+ expect ( status ) . toBe ( 400 ) ;
202
+ } ) ;
203
+
204
+ it ( 'Returns 400 on setup email notifications as globalNotifications is turned off' , async ( ) => {
205
+ const app = express ( ) ;
206
+ app . use ( bodyParser . json ( ) ) ;
207
+ app . use ( notifications ( ) ) ;
208
+
209
+ const token = await createAuthToken ( ) ;
78
210
const addUsersNotificationChannelMock = jest . fn ( ) ;
79
211
80
212
app . locals . db = {
@@ -88,20 +220,66 @@ describe('Notifications', () => {
88
220
} ,
89
221
setUserStorage : ( _ : string , __ : string ) => { } ,
90
222
getIdEnsName : async ( ensName : string ) => ensName ,
223
+ getGlobalNotification : async ( ensName : string ) =>
224
+ Promise . resolve ( { isEnabled : false } ) ,
91
225
addUsersNotificationChannel : addUsersNotificationChannelMock ,
92
226
} ;
227
+
93
228
app . locals . web3Provider = {
94
229
resolveName : async ( ) =>
95
230
'0x71CB05EE1b1F506fF321Da3dac38f25c0c9ce6E1' ,
96
231
} ;
97
232
98
233
const { status } = await request ( app )
99
- . post ( `/email/bob.eth` )
234
+ . post ( `/bob.eth` )
235
+ . set ( {
236
+ authorization : `Bearer ${ token } ` ,
237
+ } )
238
+ . send ( {
239
+ recipientValue :
'[email protected] ' ,
240
+ notificationChannelType : NotificationChannelType . EMAIL ,
241
+ } ) ;
242
+
243
+ expect ( status ) . toBe ( 400 ) ;
244
+ } ) ;
245
+
246
+ it ( 'User can setup email notifications' , async ( ) => {
247
+ const app = express ( ) ;
248
+ app . use ( bodyParser . json ( ) ) ;
249
+ app . use ( notifications ( ) ) ;
250
+
251
+ const token = await createAuthToken ( ) ;
252
+
253
+ const addUsersNotificationChannelMock = jest . fn ( ) ;
254
+
255
+ app . locals . db = {
256
+ getSession : async ( ensName : string ) =>
257
+ Promise . resolve ( {
258
+ challenge : '123' ,
259
+ token,
260
+ } ) ,
261
+ setSession : async ( _ : string , __ : any ) => {
262
+ return ( _ : any , __ : any , ___ : any ) => { } ;
263
+ } ,
264
+ setUserStorage : ( _ : string , __ : string ) => { } ,
265
+ getIdEnsName : async ( ensName : string ) => ensName ,
266
+ getGlobalNotification : async ( ensName : string ) =>
267
+ Promise . resolve ( { isEnabled : true } ) ,
268
+ addUsersNotificationChannel : addUsersNotificationChannelMock ,
269
+ } ;
270
+ app . locals . web3Provider = {
271
+ resolveName : async ( ) =>
272
+ '0x71CB05EE1b1F506fF321Da3dac38f25c0c9ce6E1' ,
273
+ } ;
274
+
275
+ const { status, body } = await request ( app )
276
+ . post ( `/bob.eth` )
100
277
. set ( {
101
278
authorization : `Bearer ${ token } ` ,
102
279
} )
103
280
. send ( {
104
- recipientAddress : 'bob.eth' ,
281
+ recipientValue :
'[email protected] ' ,
282
+ notificationChannelType : NotificationChannelType . EMAIL ,
105
283
} ) ;
106
284
107
285
expect ( status ) . toBe ( 200 ) ;
@@ -110,7 +288,7 @@ describe('Notifications', () => {
110
288
{
111
289
type : 'EMAIL' ,
112
290
config : {
113
- recipientValue : 'bob.eth ' ,
291
+ recipientValue : 'bob@gmail.com ' ,
114
292
} ,
115
293
} ,
116
294
) ;
0 commit comments