|
| 1 | +import re |
| 2 | +import string |
| 3 | +import random |
| 4 | +from datetime import timedelta |
| 5 | +from urllib.parse import urlparse, parse_qs |
| 6 | +from starlette.datastructures import URLPath |
| 7 | +from main import app |
| 8 | +from utils.auth import ( |
| 9 | + create_access_token, |
| 10 | + create_refresh_token, |
| 11 | + verify_password, |
| 12 | + get_password_hash, |
| 13 | + validate_token, |
| 14 | + generate_password_reset_url, |
| 15 | + PASSWORD_PATTERN |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +def test_password_hashing(): |
| 20 | + password = "Test123!@#" |
| 21 | + hashed = get_password_hash(password) |
| 22 | + assert verify_password(password, hashed) |
| 23 | + assert not verify_password("wrong_password", hashed) |
| 24 | + |
| 25 | + |
| 26 | +def test_token_creation_and_validation(): |
| 27 | + data = { "sub": "[email protected]"} |
| 28 | + |
| 29 | + # Test access token |
| 30 | + access_token = create_access_token(data) |
| 31 | + decoded = validate_token(access_token, "access") |
| 32 | + assert decoded is not None |
| 33 | + assert decoded["sub"] == data["sub"] |
| 34 | + assert decoded["type"] == "access" |
| 35 | + |
| 36 | + # Test refresh token |
| 37 | + refresh_token = create_refresh_token(data) |
| 38 | + decoded = validate_token(refresh_token, "refresh") |
| 39 | + assert decoded is not None |
| 40 | + assert decoded["sub"] == data["sub"] |
| 41 | + assert decoded["type"] == "refresh" |
| 42 | + |
| 43 | + |
| 44 | +def test_expired_token(): |
| 45 | + data = { "sub": "[email protected]"} |
| 46 | + expired_delta = timedelta(minutes=-10) |
| 47 | + expired_token = create_access_token(data, expired_delta) |
| 48 | + decoded = validate_token(expired_token, "access") |
| 49 | + assert decoded is None |
| 50 | + |
| 51 | + |
| 52 | +def test_invalid_token_type(): |
| 53 | + data = { "sub": "[email protected]"} |
| 54 | + access_token = create_access_token(data) |
| 55 | + decoded = validate_token(access_token, "refresh") |
| 56 | + assert decoded is None |
| 57 | + |
| 58 | +def test_password_reset_url_generation(): |
| 59 | + """ |
| 60 | + Tests that the password reset URL is correctly formatted and contains |
| 61 | + the required query parameters. |
| 62 | + """ |
| 63 | + |
| 64 | + test_token = "abc123" |
| 65 | + |
| 66 | + url = generate_password_reset_url(test_email, test_token) |
| 67 | + |
| 68 | + # Parse the URL |
| 69 | + parsed = urlparse(url) |
| 70 | + query_params = parse_qs(parsed.query) |
| 71 | + |
| 72 | + # Get the actual path from the FastAPI app |
| 73 | + reset_password_path: URLPath = app.url_path_for("reset_password") |
| 74 | + |
| 75 | + # Verify URL path |
| 76 | + assert parsed.path == str(reset_password_path) |
| 77 | + |
| 78 | + # Verify query parameters |
| 79 | + assert "email" in query_params |
| 80 | + assert "token" in query_params |
| 81 | + assert query_params["email"][0] == test_email |
| 82 | + assert query_params["token"][0] == test_token |
| 83 | + |
| 84 | +def test_password_pattern(): |
| 85 | + """ |
| 86 | + Tests that the password pattern is correctly defined. to require at least |
| 87 | + one uppercase letter, one lowercase letter, one digit, and one special |
| 88 | + character, and at least 8 characters long. Allowed special characters are: |
| 89 | + !@#$%^&*()_+-=[]{}|;:,.<>? |
| 90 | + """ |
| 91 | + special_characters = "!@#$%^&*()_+-=[]{}|;:,.<>?" |
| 92 | + uppercase_letters = string.ascii_uppercase |
| 93 | + lowercase_letters = string.ascii_lowercase |
| 94 | + digits = string.digits |
| 95 | + |
| 96 | + required_elements = { |
| 97 | + "special": special_characters, |
| 98 | + "uppercase": uppercase_letters, |
| 99 | + "lowercase": lowercase_letters, |
| 100 | + "digit": digits |
| 101 | + } |
| 102 | + required_length = 8 |
| 103 | + |
| 104 | + # Randomized valid password tests |
| 105 | + for _ in range(50): |
| 106 | + password = "" |
| 107 | + for element in required_elements: |
| 108 | + n = random.randint(required_length // len(required_elements), required_length) |
| 109 | + password += ''.join( |
| 110 | + random.choice(required_elements[element]) |
| 111 | + for _ in range(n) |
| 112 | + ) |
| 113 | + # Randomize the order of the characters in the string |
| 114 | + password = ''.join(random.sample(password, len(password))) |
| 115 | + assert re.match(PASSWORD_PATTERN, password) is not None |
| 116 | + |
| 117 | + # Invalid password tests |
| 118 | + |
| 119 | + # Empty password |
| 120 | + password = "" |
| 121 | + assert re.match(PASSWORD_PATTERN, password) is None |
| 122 | + |
| 123 | + # Too short |
| 124 | + password = "aA1!aA1" |
| 125 | + assert re.match(PASSWORD_PATTERN, password) is None |
| 126 | + |
| 127 | + # No uppercase letter |
| 128 | + password = "a1!" * 3 |
| 129 | + assert re.match(PASSWORD_PATTERN, password) is None |
| 130 | + |
| 131 | + # No lowercase letter |
| 132 | + password = "A1!" * 3 |
| 133 | + assert re.match(PASSWORD_PATTERN, password) is None |
| 134 | + |
| 135 | + # No digit |
| 136 | + password = "aA!" * 3 |
| 137 | + assert re.match(PASSWORD_PATTERN, password) is None |
| 138 | + |
| 139 | + # No special character |
| 140 | + password = "aA1" * 3 |
| 141 | + assert re.match(PASSWORD_PATTERN, password) is None |
0 commit comments