File size: 15,981 Bytes
6b725f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import unittest
import sys
import os
app_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.insert(0, app_path)
from request import RequestOTP as req
from response import ResponseOTP as res
from unittest.mock import patch, MagicMock
from datetime import datetime, timedelta
from service.OTPService import createOTP, verifyOTP, createOTPReset, verifyOTPReset

class TestCreateOTPReset(unittest.TestCase):
    @patch('service.OTPService.check_email')
    @patch('service.OTPService.get_user1')
    @patch('service.OTPService.OTPRepository.addOTP')
    @patch('service.OTPService.generate_otp')
    @patch('service.OTPService.sf')
    def test_createOTPReset_success(self,mock_support_function, mock_generate_otp, mock_addOTP, mock_get_user1, mock_check_email):
        mock_get_user1.return_value = MagicMock()
        email = "[email protected]"
        mock_support_function.check_email_empty_invalid.return_value = True
        mock_generate_otp.return_value = "123456"
        response = createOTPReset(email)
        self.assertEqual(response.status, 200)
        self.assertEqual(response.data.check, True)
        self.assertEqual(response.otp, "123456")

    @patch('service.OTPService.check_email')
    @patch('service.OTPService.get_user1')
    @patch('service.OTPService.OTPRepository.addOTP')
    @patch('service.OTPService.sf')
    def test_createOTPReset_email_empty(self,mock_support_function, mock_addOTP, mock_get_user1, mock_check_email):
        email = None
        mock_support_function.check_email_empty_invalid.return_value = res.ReponseError(status=400, data=res.Message(
            message="Email is empty"))
        response = createOTPReset(email)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data, res.Message(message='Email is empty'))

    @patch('service.OTPService.check_email')
    @patch('service.OTPService.sf')
    def test_createOTPReset_email_invalid(self,mock_support_function, mock_check_email):
        email = "201333"
        mock_support_function.check_email_empty_invalid.return_value = res.ReponseError(status=400, data=res.Message(
            message="Email invalid"))
        response = createOTPReset(email)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data, res.Message(message='Email invalid'))

class TestCreateOTPFunctions(unittest.TestCase):
    @patch('service.OTPService.generate_otp')
    @patch('service.OTPService.check_email')
    @patch('service.OTPService.OTPRepository')
    @patch('service.OTPService.sf')
    def test_createOTP_success(self,mock_support_function, mock_addOTP, mock_check_email, mock_generate_otp):
        mock_generate_otp.return_value = "123AB6"
        email = "[email protected]"
        otp = "123AB6"
        mock_support_function.check_email_empty_invalid.return_value = True
        mock_addOTP.addOTP(email, otp)
        request = req.RequestCreateOTP(email=email)
        response = createOTP(request)
        self.assertIsInstance(response, res.ResponseCreateOTP)
        self.assertEqual(response.status, 200)
        self.assertTrue(response.data.check)
        self.assertEqual(response.otp, "123AB6")

    @patch('service.OTPService.check_email')
    @patch('service.OTPService.sf')
    def test_createOTP_failed_empty_email(self,mock_support_function, mock_check_email):
        email = None
        mock_support_function.check_email_empty_invalid.return_value = res.ReponseError(status=400, data=res.Message(
            message="Email is empty"))
        request = req.RequestCreateOTP(email=email)
        response = createOTP(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "Email is empty")
        
    @patch('service.OTPService.check_email')
    @patch('service.OTPService.OTPRepository.addOTP')
    @patch('service.OTPService.sf')
    def test_createOTP_failed_empty_invalid(self,mock_support_function, mock_addOTP, mock_check_email):
        email = "20133"
        mock_support_function.check_email_empty_invalid.return_value = res.ReponseError(status=400, data=res.Message(
            message="Email invalid"))
        request = req.RequestCreateOTP(email=email)
        mock_check_email.return_value = False
        response = createOTP(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "Email invalid")
        mock_addOTP.assert_not_called()

class TestVerifyOTPFunctions(unittest.TestCase):
    @patch('service.OTPService.check_email', return_value=True)
    @patch('service.OTPService.OTPRepository.getOtpByEmail')
    @patch('service.OTPService.OTPRepository.deleteOTP')
    @patch('service.OTPService.sf')
    def test_verifyOTP_success(self,mock_support_function, mock_deleteOTP, mock_getOtpByEmail, mock_check_email):
        current_time = datetime.now()
        mock_support_function.check_email_empty_invalid.return_value = True
        mock_getOtpByEmail.return_value = MagicMock(otp="123456", created_at=current_time - timedelta(minutes=5))

        email = "[email protected]"
        otp = "123456"
        request = req.RequestVerifyOTP(email=email, otp=otp)
        response = verifyOTP(request)
        mock_deleteOTP(email, otp)
        self.assertIsInstance(response, res.ResponseVerifyOTPSignUp)
        self.assertEqual(response.status, 200)
        self.assertEqual(response.data.message, "OTP is valid")

    @patch('service.OTPService.check_email', return_value=True)
    @patch('service.OTPService.OTPRepository.getOtpByEmail')
    @patch('service.OTPService.sf')
    def test_verifyOTP_failed_invalid_otp(self,mock_support_function, mock_getOtpByEmail, mock_check_email):
        current_time = datetime.now()
        mock_support_function.check_email_empty_invalid.return_value = True
        mock_getOtpByEmail.return_value = MagicMock(otp="654321", created_at=current_time - timedelta(minutes=5))

        email = "[email protected]"
        otp = "123456"
        request = req.RequestVerifyOTP(email=email, otp=otp)
        response = verifyOTP(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "Invalid OTP")

    @patch('service.OTPService.check_email')
    @patch('service.OTPService.sf')
    def test_verifyOTP_failed_invalid_email(self,mock_support_function, mock_check_email):
        email = "invalidemail"
        otp = "123456"
        mock_support_function.check_email_empty_invalid.return_value =res.ReponseError(status=400, data=res.Message(
            message="Email invalid"))

        request = req.RequestVerifyOTP(email=email, otp=otp)

        response = verifyOTP(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "Email invalid")

    @patch('service.OTPService.sf')
    def test_verifyOTP_failed_empty_email(self,mock_support_function):
        email = None
        otp = "123456"
        mock_support_function.check_email_empty_invalid.return_value = res.ReponseError(status=400, data=res.Message(
            message="Email is empty"))
        request = req.RequestVerifyOTP(email=email, otp=otp)
        response = verifyOTP(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "Email is empty")

    @patch('service.OTPService.check_email')
    @patch('service.OTPService.sf')
    def test_verifyOTP_failed_empty_otp(self,mock_support_function, mock_check_email):
        email = "[email protected]"
        otp = None
        mock_support_function.check_email_empty_invalid.return_value = True
        request = req.RequestVerifyOTP(email=email, otp=otp)
        mock_check_email.return_value = True
        response = verifyOTP(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "otp is empty")

    @patch('service.OTPService.check_email', return_value=True)
    @patch('service.OTPService.OTPRepository.getOtpByEmail')
    @patch('service.OTPService.sf')
    def test_verifyOTP_failed_no_otp_found(self,mock_support_function, mock_getOtpByEmail, mock_check_email):
        mock_getOtpByEmail.return_value = None
        email = "[email protected]"
        otp = "123456"
        mock_support_function.check_email_empty_invalid.return_value = True
        request = req.RequestVerifyOTP(email=email, otp=otp)
        response = verifyOTP(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 404)
        self.assertEqual(response.data.message, "No OTP found for this email")

    @patch('service.OTPService.check_email')
    @patch('service.OTPService.OTPRepository.getOtpByEmail')
    @patch('service.OTPService.sf')
    def test_verifyOTP_has_expired(self,mock_support_function, mock_getOtpByEmail, mock_check_email):
        current_time = datetime.now()
        mock_getOtpByEmail.return_value = MagicMock(otp="123456", created_at=current_time - timedelta(minutes=100))
        email = "[email protected]"
        otp = "123456"
        mock_support_function.check_email_empty_invalid.return_value = True
        request = req.RequestVerifyOTP(email=email, otp=otp)
        response = verifyOTP(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "OTP has expired")

class TestVerifyOTPReset(unittest.TestCase):
    @patch('service.OTPService.check_email')
    @patch('service.OTPService.OTPRepository.getOtpByEmail')
    @patch('service.OTPService.OTPRepository')
    @patch('service.OTPService.auth.get_user_by_email')
    @patch('service.OTPService.auth')
    @patch('service.OTPService.sf')
    def test_verifyOTPReset_success(self,mock_support_function, mock_update_user, mock_get_user_by_email, mock_deleteOTP, mock_getOtpByEmail,

                                    mock_check_email):
        current_time = datetime.now()
        mock_support_function.check_email_empty_invalid.return_value = True
        mock_getOtpByEmail.return_value = MagicMock(otp="123456", created_at=current_time - timedelta(minutes=5))
        mock_get_user_by_email.return_value = MagicMock(uid="12345")
        email = "[email protected]"
        otp = "123456"
        mock_check_email.return_value = True
        request = req.RequestVerifyOTP(email=email, otp=otp)
        mock_update_user.update_user(uid="12345", )
        response = verifyOTPReset(request)
        mock_deleteOTP.deleteOTP("[email protected]", "123456")
        self.assertIsInstance(response, res.ResponseVerifyOTP)
        self.assertEqual(response.status, 200)
        self.assertEqual(response.data.message, "New Password send to Email")

    @patch('service.OTPService.check_email')
    @patch('service.OTPService.OTPRepository.getOtpByEmail')
    @patch('service.OTPService.sf')
    def test_verifyOTPReset_failed_invalid_otp(self,mock_support_function, mock_getOtpByEmail, mock_check_email):
        current_time = datetime.now()
        mock_support_function.check_email_empty_invalid.return_value = True
        mock_getOtpByEmail.return_value = MagicMock(otp="654321", created_at=current_time - timedelta(minutes=5))
        email = "[email protected]"
        otp = "123456"
        mock_check_email.return_value = True
        request = req.RequestVerifyOTP(email=email, otp=otp)
        response = verifyOTPReset(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "Invalid OTP")

    @patch('service.OTPService.check_email')
    @patch('service.OTPService.OTPRepository.getOtpByEmail')
    @patch('service.OTPService.sf')
    def test_verifyOTPReset_failed_no_otp_found(self,mock_support_function, mock_getOtpByEmail, mock_check_email):
        mock_getOtpByEmail.return_value = None
        email = "[email protected]"
        otp = "123456"
        request = req.RequestVerifyOTP(email=email, otp=otp)
        mock_support_function.check_email_empty_invalid.return_value = True
        response = verifyOTPReset(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 404)
        self.assertEqual(response.data.message, "No OTP found for this email")

    @patch('service.OTPService.check_email')
    @patch('service.OTPService.OTPRepository.getOtpByEmail')
    @patch('service.OTPService.sf')
    def test_verifyOTPReset_has_expired(self,mock_support_function, mock_getOtpByEmail, mock_check_email):
        current_time = datetime.now()
        mock_getOtpByEmail.return_value = MagicMock(otp="123456", created_at=current_time - timedelta(minutes=100))
        email = "[email protected]"
        otp = "123456"
        mock_support_function.check_email_empty_invalid.return_value = True
        request = req.RequestVerifyOTP(email=email, otp=otp)
        response = verifyOTPReset(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "OTP has expired")

    @patch('service.OTPService.check_email')
    @patch('service.OTPService.sf')
    def test_verifyOTPReset_failed_invalid_email(self,mock_support_function, mock_check_email):
        email = "invalidemail"
        otp = "123456"
        mock_support_function.check_email_empty_invalid.return_value = res.ReponseError(status=400, data=res.Message(
            message="Email invalid"))
        request = req.RequestVerifyOTP(email=email, otp=otp)
        response = verifyOTPReset(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "Email invalid")

    @patch('service.OTPService.check_email')
    @patch('service.OTPService.OTPRepository.getOtpByEmail')
    @patch('service.OTPService.sf')
    def test_verifyOTPReset_failed_empty_email(self,mock_support_function, mock_getOtpByEmail, mock_check_email):
        email = None
        otp = "123456"
        request = req.RequestVerifyOTP(email=email, otp=otp)
        mock_support_function.check_email_empty_invalid.return_value = res.ReponseError(status=400, data=res.Message(
            message="Email is empty"))
        response = verifyOTPReset(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "Email is empty")

    @patch('service.OTPService.check_email', return_value=True)
    @patch('service.OTPService.sf')
    def test_verifyOTPReset_failed_empty_otp(self,mock_support_function, mock_check_email):
        email = "[email protected]"
        otp = None
        mock_support_function.check_email_empty_invalid.return_value = True
        request = req.RequestVerifyOTP(email=email, otp=otp)
        response = verifyOTPReset(request)
        self.assertIsInstance(response, res.ReponseError)
        self.assertEqual(response.status, 400)
        self.assertEqual(response.data.message, "OTP is empty")


if __name__ == '__main__':
    unittest.main()