diff --git a/src/gnexus_gauth/dto.py b/src/gnexus_gauth/dto.py index 7cb57f9..c6174d9 100644 --- a/src/gnexus_gauth/dto.py +++ b/src/gnexus_gauth/dto.py @@ -42,6 +42,17 @@ client_access_list: list[ClientAccess] = field(default_factory=list) raw_payload: dict[str, Any] = field(default_factory=dict, repr=False) + @property + def avatar_url(self) -> str | None: + """Direct URL to the user's avatar, or None if not set.""" + return self.profile.get("avatar_url") + + @property + def avatar_variants(self) -> dict[str, str]: + """Map of size variant names to their URLs.""" + variants = self.profile.get("avatar_variants") + return variants if isinstance(variants, dict) else {} + @dataclass(frozen=True) class AuthorizationRequest: diff --git a/tests/unit/test_dto.py b/tests/unit/test_dto.py new file mode 100644 index 0000000..3178a29 --- /dev/null +++ b/tests/unit/test_dto.py @@ -0,0 +1,49 @@ +"""Tests for DTOs.""" + +from gnexus_gauth.dto import AuthenticatedUser + + +class TestAuthenticatedUser: + def test_avatar_url_from_profile(self) -> None: + user = AuthenticatedUser( + user_id="1", + email="user@example.test", + email_verified=True, + profile={"avatar_url": "https://example.com/avatar.jpg"}, + ) + assert user.avatar_url == "https://example.com/avatar.jpg" + + def test_avatar_url_none_when_missing(self) -> None: + user = AuthenticatedUser( + user_id="1", + email="user@example.test", + email_verified=True, + profile={}, + ) + assert user.avatar_url is None + + def test_avatar_variants_from_profile(self) -> None: + user = AuthenticatedUser( + user_id="1", + email="user@example.test", + email_verified=True, + profile={ + "avatar_variants": { + "small": "https://example.com/small.jpg", + "medium": "https://example.com/medium.jpg", + } + }, + ) + assert user.avatar_variants == { + "small": "https://example.com/small.jpg", + "medium": "https://example.com/medium.jpg", + } + + def test_avatar_variants_empty_when_missing(self) -> None: + user = AuthenticatedUser( + user_id="1", + email="user@example.test", + email_verified=True, + profile={}, + ) + assert user.avatar_variants == {}