Spaces:
Sleeping
Sleeping
File size: 14,049 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 |
import os
import sys
from unittest.mock import patch, MagicMock
from fastapi.testclient import TestClient
app_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.insert(0, app_path)
from controller import OTPController
client = TestClient(OTPController.router)
@patch('service.OTPService.check_email')
@patch('service.OTPService.get_user1')
@patch('repository.OTPRepository.addOTP')
@patch('service.OTPService.generate_otp')
@patch('service.OTPService.sf')
def test_createOTPReset_success(mock_support_function, mock_generate_otp,mock_addOTP, mock_get_user1, mock_check_email):
mock_get_user1.return_value = MagicMock()
email = "user@example.com"
mock_support_function.check_email_empty_invalid.return_value = True
mock_generate_otp.return_value = "123456"
response = client.post("/create_otp", json={"email": email})
assert response.json()['status'] == 200
assert response.json()['data']['check'] == True
assert response.json()['otp'] == "123456"
def test_createOTPReset_email_empty():
email = ""
response = client.post("/create_otp", json={"email": email})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email is required."
def test_createOTPReset_email_invalid():
email = "201333"
response =client.post("/create_otp", json={"email": email})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email must be a string, not a number."
@patch('service.OTPService.generate_otp')
@patch('service.OTPService.check_email')
@patch('repository.OTPRepository.addOTP')
@patch('service.OTPService.sf')
def test_createOTP_success(mock_support_function,mock_addOTP, mock_check_email, mock_generate_otp):
mock_generate_otp.return_value = "123AB6"
email = "20133118@gmail.com"
otp = "123AB6"
mock_support_function.check_email_empty_invalid.return_value = True
mock_addOTP(email,otp)
response =client.post("/create_otp", json={"email": email})
assert response.json()['status'] == 200
assert response.json()['data']['check'] == True
assert response.json()['otp'] == "123AB6"
def test_createOTP_failed_empty_email():
email = ""
response =client.post("/create_otp", json={"email": email})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email is required."
def test_createOTP_failed_empty_invalid():
email = "20133"
response =client.post("/create_otp", json={"email": email})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email must be a string, not a number."
from datetime import datetime,timedelta
@patch('service.OTPService.sf.check_email_empty_invalid', return_value=True)
@patch('repository.OTPRepository.getOtpByEmail')
@patch('repository.OTPRepository.deleteOTP')
def test_verifyOTP_success(mock_deleteOTP, mock_getOtpByEmail, mock_check_email):
current_time = datetime.now()
mock_getOtpByEmail.return_value = MagicMock(otp="abcdef", created_at=current_time - timedelta(minutes=5))
email = "user@example.com"
otp = "abcdef"
response = client.post("/verify_otp", json={"email": email,"otp": otp})
mock_deleteOTP(email,otp)
assert response.json()['status'] == 200
assert response.json()['data']['message'] == "OTP is valid"
@patch('service.OTPService.sf.check_email_empty_invalid', return_value=True)
@patch('repository.OTPRepository.getOtpByEmail')
def test_verifyOTP_failed_invalid_otp(mock_getOtpByEmail, mock_check_email):
current_time = datetime.now()
mock_getOtpByEmail.return_value = MagicMock(otp="654321", created_at=current_time - timedelta(minutes=5))
email = "user@example.com"
otp = "123456"
response =client.post("/verify_otp", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP must be a string, not a number."
def test_verifyOTP_failed_invalid_email():
email = "invalidemail"
otp = "123456"
response =client.post("/verify_otp", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP must be a string, not a number."
def test_verifyOTP_failed_empty_email():
email = None
otp = "123456"
response =client.post("/verify_otp", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email is required."
def test_verifyOTP_failed_empty_otp():
email = "user@example.com"
otp = None
response = client.post("/verify_otp", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP is required"
@patch('service.OTPService.sf.check_email_empty_invalid', return_value=True)
@patch('repository.OTPRepository.getOtpByEmail')
def test_verifyOTP_failed_no_otp_found(mock_getOtpByEmail, mock_check_email):
mock_getOtpByEmail.return_value = None
email = "user@example.com"
otp = "1234ab"
response =client.post("/verify_otp", json={"email": email,"otp": otp})
assert response.json()['status'] == 404
assert response.json()['data']['message'] == "No OTP found for this email"
@patch('service.OTPService.sf.check_email_empty_invalid', return_value=True)
@patch('repository.OTPRepository.getOtpByEmail')
def test_verifyOTP_has_expired( mock_getOtpByEmail, mock_check_email):
current_time = datetime.now()
mock_getOtpByEmail.return_value = MagicMock(otp="1234ab", created_at=current_time - timedelta(minutes=100))
email = "user@example.com"
otp = "1234ab"
response =client.post("/verify_otp", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP has expired"
@patch('service.OTPService.sf.check_email_empty_invalid', return_value=True)
@patch('repository.OTPRepository.getOtpByEmail')
@patch('repository.OTPRepository.deleteOTP')
@patch('service.OTPService.auth.get_user_by_email')
@patch('service.OTPService.auth')
@patch('service.OTPService.generate_random_password')
def test_verifyOTPReset1_success( mock_generate_password,mock_update_user, mock_get_user_by_email, mock_deleteOTP, mock_getOtpByEmail, mock_check_email):
current_time = datetime.now()
mock_getOtpByEmail.return_value = MagicMock(otp="abcd12", created_at=current_time - timedelta(minutes=5))
mock_get_user_by_email.return_value = MagicMock(uid="12345")
email = "user@example.com"
otp = "abcd12"
mock_generate_password.return_value = "ABC123"
mock_update_user.update_user(uid="12345",)
mock_deleteOTP("user@example.com", "abcd12")
response = client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 200
assert response.json()['data']['message'] == "New Password send to Email"
assert response.json()['newpassword'] == "ABC123"
@patch('service.OTPService.sf.check_email_empty_invalid', return_value=True)
@patch('repository.OTPRepository.getOtpByEmail')
def test_verifyOTPReset_failed_invalid_otp(mock_getOtpByEmail, mock_check_email):
current_time = datetime.now()
mock_getOtpByEmail.return_value = MagicMock(otp="654321", created_at=current_time - timedelta(minutes=5))
email = "user@example.com"
otp = "abcd12"
response =client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Invalid OTP"
@patch('service.OTPService.sf.check_email_empty_invalid', return_value=True)
@patch('repository.OTPRepository.getOtpByEmail')
def test_verifyOTPReset_failed_no_otp_found(mock_getOtpByEmail, mock_check_email):
mock_getOtpByEmail.return_value = None
email = "user@example.com"
otp = "abcd12"
response = client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 404
assert response.json()['data']['message'] == "No OTP found for this email"
@patch('service.OTPService.sf.check_email_empty_invalid', return_value=True)
@patch('repository.OTPRepository.getOtpByEmail')
def test_verifyOTPReset_has_expired(mock_getOtpByEmail, mock_check_email):
current_time = datetime.now()
mock_getOtpByEmail.return_value = MagicMock(otp="1234ab", created_at=current_time - timedelta(minutes=100))
email = "vonhuy@gmail.com"
otp = "1234ab"
response = client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP has expired"
def test_verifyOTPReset_failed_invalid_email():
email = "invalidemail"
otp = "abcd12"
response =client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email invalid"
def test_verifyOTPReset_failed_empty_email():
email = None
otp = "123456"
response =client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email is required."
def test_verifyOTPReset_failed_empty_otp():
email = "user@example.com"
otp = None
response = client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP is required"
def test_createOTP_email_required():
email = None
response = client.post("/create_otp", json={"email": email})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email is required."
def test_createOTP_email_must_be_string():
email = "20133"
response = client.post("/create_otp", json={"email": email})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email must be a string, not a number."
def test_verifyOTP_email_is_required():
email = None
otp ="123abc"
response = client.post("/verify_otp", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email is required."
def test_verifyOTP_otp_is_required():
email = "test@gmail.com"
otp = None
response = client.post("/verify_otp", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP is required"
def test_verifyOTP_otp_email_must_be_string():
email = "20133"
otp = "123abc"
response = client.post("/verify_otp", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email must be a string, not a number."
def test_verifyOTP_otp_must_be_string():
email = "20133@gmail.com"
otp = "123456"
response = client.post("/verify_otp", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP must be a string, not a number."
def test_verifyOTP_otp_max_length():
email = "20133@gmail.com"
otp = "abcdef1"
response = client.post("/verify_otp", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP max length is 6"
def test_verifyOTPReset_email_is_required():
email = None
otp ="123abc"
response = client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email is required."
def test_verifyOTPReset_otp_is_required():
email = "test@gmail.com"
otp = None
response = client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP is required"
def test_verifyOTPReset_otp_email_must_be_string():
email = "20133"
otp = "123abc"
response = client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "Email must be a string, not a number."
def test_verifyOTPReset_otp_must_be_string():
email = "20133@gmail.com"
otp = "123456"
response = client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP must be a string, not a number."
def test_verifyOTPReset_otp_max_length():
email = "20133@gmail.com"
otp = "abcdef1"
response = client.post("/verify_otp_reset_password", json={"email": email,"otp": otp})
assert response.json()['status'] == 400
assert response.json()['data']['message'] == "OTP max length is 6" |