Spaces:
Sleeping
Sleeping
File size: 2,769 Bytes
2f1a5fb | 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 | import pytest
from src.uploader_cli.auth_service import AuthService
from src.uploader_cli.exceptions import AuthenticationError
def test_authenticate_success_stores_token(monkeypatch: pytest.MonkeyPatch) -> None:
store: dict[tuple[str, str], str] = {}
class FakeApi:
def whoami(self) -> dict[str, str]:
return {"name": "alice", "email": "alice@example.com", "user_id": "u123"}
monkeypatch.setattr("src.uploader_cli.auth_service.HfApi", lambda token: FakeApi())
monkeypatch.setattr(
"src.uploader_cli.auth_service.keyring.set_password",
lambda service, account, token: store.__setitem__((service, account), token),
)
service = AuthService()
user = service.authenticate(" hf_test_token ")
assert user["username"] == "alice"
assert user["email"] == "alice@example.com"
assert user["user_id"] == "u123"
assert store[(service.KEYRING_SERVICE, service.KEYRING_ACCOUNT)] == "hf_test_token"
def test_authenticate_rejects_blank_token() -> None:
with pytest.raises(AuthenticationError, match="Token is required"):
AuthService().authenticate(" ")
def test_authenticate_invalid_token_raises(monkeypatch: pytest.MonkeyPatch) -> None:
class FakeApi:
def whoami(self) -> dict[str, str]:
raise RuntimeError("401 Unauthorized")
monkeypatch.setattr("src.uploader_cli.auth_service.HfApi", lambda token: FakeApi())
with pytest.raises(AuthenticationError, match="Token validation failed"):
AuthService().authenticate("hf_invalid")
def test_require_token_returns_stored_value(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
"src.uploader_cli.auth_service.keyring.get_password",
lambda service, account: "hf_saved_token",
)
assert AuthService().require_token() == "hf_saved_token"
def test_require_token_raises_when_missing(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
"src.uploader_cli.auth_service.keyring.get_password",
lambda service, account: None,
)
with pytest.raises(AuthenticationError, match="No stored token found"):
AuthService().require_token()
def test_clear_token_ignores_missing_password(monkeypatch: pytest.MonkeyPatch) -> None:
class FakePasswordDeleteError(Exception):
pass
class FakeErrors:
PasswordDeleteError = FakePasswordDeleteError
def raise_delete_error(service: str, account: str) -> None:
raise FakePasswordDeleteError("not found")
monkeypatch.setattr("src.uploader_cli.auth_service.keyring.errors", FakeErrors)
monkeypatch.setattr(
"src.uploader_cli.auth_service.keyring.delete_password",
raise_delete_error,
)
AuthService().clear_token()
|