1
1
import { NotificationChannel , NotificationChannelType } from './notifications' ;
2
- import { addNewNotificationChannel , resendOtp } from './Notification' ;
3
- import { ChannelNotSupportedError } from './errors/ChannelNotSupportedError' ;
4
- const nodemailer = require ( 'nodemailer' ) ;
2
+ import {
3
+ RESEND_VERIFICATION_OTP_TIME_PERIOD ,
4
+ addNewNotificationChannel ,
5
+ resendOtp ,
6
+ } from './Notification' ;
5
7
6
8
jest . mock ( 'nodemailer' ) ;
7
9
8
- const sendMailMock = jest . fn ( ) ;
9
- nodemailer . createTransport . mockReturnValue ( {
10
- sendMail : sendMailMock ,
11
- close : ( ) => { } ,
12
- } ) ;
13
-
14
10
describe ( 'Notification' , ( ) => {
15
11
describe ( 'AddEmailNotificationChannel' , ( ) => {
16
12
it ( 'adds email notification channel' , async ( ) => {
@@ -41,6 +37,13 @@ describe('Notification', () => {
41
37
} ,
42
38
] ;
43
39
40
+ const nodemailer = require ( 'nodemailer' ) ;
41
+ const sendMailMock = jest . fn ( ) ;
42
+ nodemailer . createTransport . mockReturnValue ( {
43
+ sendMail : sendMailMock ,
44
+ close : ( ) => { } ,
45
+ } ) ;
46
+
44
47
const db = {
45
48
getUsersNotificationChannels,
46
49
addUsersNotificationChannel,
@@ -62,6 +65,22 @@ describe('Notification', () => {
62
65
} ) ;
63
66
64
67
describe ( 'Resend Email verification OTP' , ( ) => {
68
+ const notificationChannels : NotificationChannel [ ] = [
69
+ {
70
+ type : NotificationChannelType . EMAIL ,
71
+ config : {
72
+ smtpHost : 'smtp.gmail.com' ,
73
+ smtpPort : 587 ,
74
+
75
+ smtpUsername :
'[email protected] ' ,
76
+ smtpPassword : 'abcd1234' ,
77
+ } ,
78
+ } ,
79
+ ] ;
80
+
81
+ const getOtp = jest . fn ( ) ;
82
+ const setOtp = jest . fn ( ) ;
83
+
65
84
it ( 'Should throw error as notification channel is not supported' , async ( ) => {
66
85
const getUsersNotificationChannels = ( ) =>
67
86
Promise . resolve ( [
@@ -74,21 +93,6 @@ describe('Notification', () => {
74
93
} ,
75
94
} ,
76
95
] ) ;
77
- const getOtp = jest . fn ( ) ;
78
- const setOtp = jest . fn ( ) ;
79
-
80
- const notificationChannels : NotificationChannel [ ] = [
81
- {
82
- type : NotificationChannelType . EMAIL ,
83
- config : {
84
- smtpHost : 'smtp.gmail.com' ,
85
- smtpPort : 587 ,
86
-
87
- smtpUsername :
'[email protected] ' ,
88
- smtpPassword : 'abcd1234' ,
89
- } ,
90
- } ,
91
- ] ;
92
96
93
97
const db = {
94
98
getUsersNotificationChannels,
@@ -122,21 +126,116 @@ describe('Notification', () => {
122
126
} ,
123
127
} ,
124
128
] ) ;
125
- const getOtp = jest . fn ( ) ;
126
- const setOtp = jest . fn ( ) ;
127
129
128
- const notificationChannels : NotificationChannel [ ] = [
129
- {
130
+ const db = {
131
+ getUsersNotificationChannels,
132
+ getOtp,
133
+ setOtp,
134
+ } ;
135
+
136
+ try {
137
+ await resendOtp (
138
+ '0x71cb05ee1b1f506ff321da3dac38f25c0c9ce6e1' ,
139
+ NotificationChannelType . EMAIL ,
140
+ notificationChannels ,
141
+ db ,
142
+ ) ;
143
+ } catch ( error : any ) {
144
+ expect ( error . message ) . toBe (
145
+ 'EMAIL notification channel not configured' ,
146
+ ) ;
147
+ }
148
+ } ) ;
149
+
150
+ it ( 'Should throw error as notification channel is not enabled' , async ( ) => {
151
+ const getUsersNotificationChannels = ( ) =>
152
+ Promise . resolve ( [
153
+ {
154
+ type : NotificationChannelType . EMAIL ,
155
+ config : {
156
+ recipientValue :
'[email protected] ' ,
157
+ isVerified : false ,
158
+ isEnabled : false ,
159
+ } ,
160
+ } ,
161
+ ] ) ;
162
+
163
+ const db = {
164
+ getUsersNotificationChannels,
165
+ getOtp,
166
+ setOtp,
167
+ } ;
168
+
169
+ try {
170
+ await resendOtp (
171
+ '0x71cb05ee1b1f506ff321da3dac38f25c0c9ce6e1' ,
172
+ NotificationChannelType . EMAIL ,
173
+ notificationChannels ,
174
+ db ,
175
+ ) ;
176
+ } catch ( error : any ) {
177
+ expect ( error . message ) . toBe (
178
+ 'EMAIL notification channel is not enabled' ,
179
+ ) ;
180
+ }
181
+ } ) ;
182
+
183
+ it ( 'Should throw error as notification channel is already verified' , async ( ) => {
184
+ const getUsersNotificationChannels = ( ) =>
185
+ Promise . resolve ( [
186
+ {
187
+ type : NotificationChannelType . EMAIL ,
188
+ config : {
189
+ recipientValue :
'[email protected] ' ,
190
+ isVerified : true ,
191
+ isEnabled : true ,
192
+ } ,
193
+ } ,
194
+ ] ) ;
195
+
196
+ const db = {
197
+ getUsersNotificationChannels,
198
+ getOtp,
199
+ setOtp,
200
+ } ;
201
+
202
+ try {
203
+ await resendOtp (
204
+ '0x71cb05ee1b1f506ff321da3dac38f25c0c9ce6e1' ,
205
+ NotificationChannelType . EMAIL ,
206
+ notificationChannels ,
207
+ db ,
208
+ ) ;
209
+ } catch ( error : any ) {
210
+ expect ( error . message ) . toBe (
211
+ 'EMAIL notification channel is already verified' ,
212
+ ) ;
213
+ }
214
+ } ) ;
215
+
216
+ it ( 'Should throw error as new OTP can not be sent before RESEND_VERIFICATION_OTP_TIME_PERIOD' , async ( ) => {
217
+ const getOtp = async (
218
+ ensName : string ,
219
+ channelType : NotificationChannelType ,
220
+ ) => {
221
+ return Promise . resolve ( {
222
+ otp : '19283' ,
130
223
type : NotificationChannelType . EMAIL ,
131
- config : {
132
- smtpHost : 'smtp.gmail.com' ,
133
- smtpPort : 587 ,
134
-
135
- smtpUsername :
'[email protected] ' ,
136
- smtpPassword : 'abcd1234' ,
224
+ generatedAt : new Date ( ) ,
225
+ } ) ;
226
+ } ;
227
+
228
+ const getUsersNotificationChannels = ( ) =>
229
+ Promise . resolve ( [
230
+ {
231
+ type : NotificationChannelType . EMAIL ,
232
+ config : {
233
+ recipientValue :
'[email protected] ' ,
234
+ isVerified : false ,
235
+ isEnabled : true ,
236
+ } ,
137
237
} ,
138
- } ,
139
- ] ;
238
+ ] ) ;
140
239
141
240
const db = {
142
241
getUsersNotificationChannels,
@@ -153,9 +252,49 @@ describe('Notification', () => {
153
252
) ;
154
253
} catch ( error : any ) {
155
254
expect ( error . message ) . toBe (
156
- 'EMAIL notification channel not configured ' ,
255
+ 'New OTP can be generated after 1 minutes of last OTP generated ' ,
157
256
) ;
158
257
}
159
258
} ) ;
259
+
260
+ it ( 'Should resend new OTP email error for notification channel' , async ( ) => {
261
+ const nodemailer = require ( 'nodemailer' ) ;
262
+ const sendMailMock = jest . fn ( ) ;
263
+ nodemailer . createTransport . mockReturnValue ( {
264
+ sendMail : sendMailMock ,
265
+ close : ( ) => { } ,
266
+ } ) ;
267
+
268
+ const getUsersNotificationChannels = ( ) =>
269
+ Promise . resolve ( [
270
+ {
271
+ type : NotificationChannelType . EMAIL ,
272
+ config : {
273
+ recipientValue :
'[email protected] ' ,
274
+ isVerified : false ,
275
+ isEnabled : true ,
276
+ } ,
277
+ } ,
278
+ ] ) ;
279
+
280
+ const db = {
281
+ getUsersNotificationChannels,
282
+ getOtp,
283
+ setOtp,
284
+ } ;
285
+
286
+ await resendOtp (
287
+ '0x71cb05ee1b1f506ff321da3dac38f25c0c9ce6e1' ,
288
+ NotificationChannelType . EMAIL ,
289
+ notificationChannels ,
290
+ db ,
291
+ ) ;
292
+
293
+ expect ( sendMailMock ) . toHaveBeenCalled ( ) ;
294
+ } ) ;
295
+
296
+ it ( 'Resend OTP time period should be 60 seconds' , async ( ) => {
297
+ expect ( RESEND_VERIFICATION_OTP_TIME_PERIOD ) . toBe ( 60 ) ;
298
+ } ) ;
160
299
} ) ;
161
300
} ) ;
0 commit comments