|
| 1 | +import datetime |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from conduit.core.exceptions import ( |
| 6 | + EmailAlreadyTakenException, |
| 7 | + UserNameAlreadyTakenException, |
| 8 | +) |
| 9 | +from conduit.domain.entities.user import User |
| 10 | + |
| 11 | + |
| 12 | +def test_user_ensure_unique_email_raises() -> None: |
| 13 | + user = User( |
| 14 | + id=1, |
| 15 | + username="user", |
| 16 | + email="user@example.com", |
| 17 | + password_hash="hash", |
| 18 | + bio="", |
| 19 | + image_url=None, |
| 20 | + created_at=datetime.datetime.now(), |
| 21 | + ) |
| 22 | + with pytest.raises(EmailAlreadyTakenException): |
| 23 | + user.ensure_unique_email(exists=True) |
| 24 | + |
| 25 | + |
| 26 | +def test_user_ensure_unique_username_raises() -> None: |
| 27 | + user = User( |
| 28 | + id=1, |
| 29 | + username="user", |
| 30 | + email="user@example.com", |
| 31 | + password_hash="hash", |
| 32 | + bio="", |
| 33 | + image_url=None, |
| 34 | + created_at=datetime.datetime.now(), |
| 35 | + ) |
| 36 | + with pytest.raises(UserNameAlreadyTakenException): |
| 37 | + user.ensure_unique_username(exists=True) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize("invalid_email", ["", "invalid"]) |
| 41 | +def test_user_change_email_validates(invalid_email: str) -> None: |
| 42 | + user = User( |
| 43 | + id=1, |
| 44 | + username="user", |
| 45 | + email="user@example.com", |
| 46 | + password_hash="hash", |
| 47 | + bio="", |
| 48 | + image_url=None, |
| 49 | + created_at=datetime.datetime.now(), |
| 50 | + ) |
| 51 | + with pytest.raises(ValueError): |
| 52 | + user.change_email(invalid_email) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize("invalid_username", ["", " ", "ab"]) |
| 56 | +def test_user_change_username_validates(invalid_username: str) -> None: |
| 57 | + user = User( |
| 58 | + id=1, |
| 59 | + username="user", |
| 60 | + email="user@example.com", |
| 61 | + password_hash="hash", |
| 62 | + bio="", |
| 63 | + image_url=None, |
| 64 | + created_at=datetime.datetime.now(), |
| 65 | + ) |
| 66 | + with pytest.raises(ValueError): |
| 67 | + user.change_username(invalid_username) |
0 commit comments