fix(test): AuthService lock file pollution

This commit is contained in:
Type-Delta
2026-04-06 10:55:15 +07:00
parent 7c5a856253
commit 885f35ac2c
2 changed files with 11 additions and 12 deletions

View File

@@ -2,22 +2,21 @@ from viu_media.cli.service.auth.service import AuthService
from viu_media.libs.media_api.types import UserProfile
def test_load_auth_creates_file_when_missing(tmp_path, monkeypatch):
def test_load_auth_creates_file_when_missing(tmp_path):
auth_file = tmp_path / "auth.json"
monkeypatch.setattr("viu_media.cli.service.auth.service.AUTH_FILE", auth_file)
service = AuthService(media_api="anilist")
service = AuthService(media_api="anilist", auth_file=auth_file)
profile = service.get_auth()
assert profile is None
assert auth_file.exists()
assert (tmp_path / "auth.lock").exists() is False
def test_save_and_get_auth_roundtrip(tmp_path, monkeypatch):
def test_save_and_get_auth_roundtrip(tmp_path):
auth_file = tmp_path / "auth.json"
monkeypatch.setattr("viu_media.cli.service.auth.service.AUTH_FILE", auth_file)
service = AuthService(media_api="anilist")
service = AuthService(media_api="anilist", auth_file=auth_file)
user = UserProfile(id=1, name="test-user", avatar_url="https://img/avatar.png")
service.save_user_profile(user, "token-abc")
@@ -28,11 +27,10 @@ def test_save_and_get_auth_roundtrip(tmp_path, monkeypatch):
assert auth.user_profile.name == "test-user"
def test_clear_user_profile_deletes_auth_file(tmp_path, monkeypatch):
def test_clear_user_profile_deletes_auth_file(tmp_path):
auth_file = tmp_path / "auth.json"
monkeypatch.setattr("viu_media.cli.service.auth.service.AUTH_FILE", auth_file)
service = AuthService(media_api="anilist")
service = AuthService(media_api="anilist", auth_file=auth_file)
user = UserProfile(id=2, name="clear-me")
service.save_user_profile(user, "token")
assert auth_file.exists()

View File

@@ -1,5 +1,6 @@
import json
import logging
from pathlib import Path
from typing import Optional
from ....core.constants import APP_DATA_DIR
@@ -13,10 +14,10 @@ AUTH_FILE = APP_DATA_DIR / "auth.json"
class AuthService:
def __init__(self, media_api: str):
self.path = AUTH_FILE
def __init__(self, media_api: str, auth_file: Path | None = None):
self.path = auth_file or AUTH_FILE
self.media_api = media_api
_lock_file = APP_DATA_DIR / "auth.lock"
_lock_file = self.path.with_suffix(".lock")
self._lock = FileLock(_lock_file)
def get_auth(self) -> Optional[AuthProfile]: