diff --git a/src/blueapi/service/authentication.py b/src/blueapi/service/authentication.py index 17ab9f929..c1cd3d5b0 100644 --- a/src/blueapi/service/authentication.py +++ b/src/blueapi/service/authentication.py @@ -18,7 +18,7 @@ from blueapi.config import OIDCConfig from blueapi.service.model import Cache -BLUEAPI_CACHE_LOCATION = "~/.cache/blueapi_cache" +BLUEAPI_CACHE_LOCATION = "~/.cache/" SCOPES = "openid offline_access" @@ -34,7 +34,7 @@ def delete_cache(self) -> None: ... class SessionCacheManager(CacheManager): def __init__(self, token_path: Path | None) -> None: self._token_path: Path = ( - token_path if token_path else self._get_xdg_cache_path() + token_path if token_path else self._default_token_cache_path() ) @cached_property @@ -56,16 +56,12 @@ def load_cache(self) -> Cache: def delete_cache(self) -> None: Path(self._file_path).unlink(missing_ok=True) - def _get_xdg_cache_path(self) -> Path: + def _default_token_cache_path(self) -> Path: """ - Return the XDG cache file path. + Return the default cache file path. """ - cache_path = os.environ.get("XDG_CACHE_HOME") - if not cache_path: - cache_path = os.path.expanduser(BLUEAPI_CACHE_LOCATION) - elif os.path.isdir(cache_path): - cache_path = Path(cache_path) / "blueapi_cache" - return Path(cache_path) + cache_path = os.environ.get("XDG_CACHE_HOME", BLUEAPI_CACHE_LOCATION) + return Path(cache_path).expanduser() / "blueapi_cache" class SessionManager: diff --git a/tests/unit_tests/service/test_authentication.py b/tests/unit_tests/service/test_authentication.py index 51edd601d..94c489b03 100644 --- a/tests/unit_tests/service/test_authentication.py +++ b/tests/unit_tests/service/test_authentication.py @@ -132,9 +132,8 @@ def test_processes_valid_token( def test_session_cache_manager_returns_writable_file_path(tmp_path): - with patch("os.environ.get") as xdg_directory: - xdg_directory.return_value = tmp_path - cache = SessionCacheManager(token_path=None) - with open(cache._file_path, "xb") as token_file: - assert token_file.writable() - assert cache._file_path == f"{tmp_path}/blueapi_cache" + os.environ["XDG_CACHE_HOME"] = str(tmp_path) + cache = SessionCacheManager(token_path=None) + Path(cache._file_path).touch() + assert os.path.isfile(cache._file_path) + assert cache._file_path == f"{tmp_path}/blueapi_cache"