Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve credentials cache #357

Merged
merged 5 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mopidy_spotify/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ class SpotifyPlaybackProvider(backend.PlaybackProvider):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._cache_location = Extension().get_cache_dir(self.backend._config)
self._data_location = Extension().get_data_dir(self.backend._config)
self._config = self.backend._config["spotify"]

self._credentials_dir = self._data_location / "credentials-cache"
if not self._credentials_dir.exists():
self._credentials_dir.mkdir(mode=0o700)

def on_source_setup(self, source):
for prop in ["username", "password", "bitrate"]:
source.set_property(prop, str(self._config[prop]))
source.set_property("cache-credentials", self._credentials_dir)
if self._config["allow_cache"]:
source.set_property("cache-credentials", self._cache_location)
source.set_property("cache-files", self._cache_location)
source.set_property(
"cache-max-size", self._config["cache_size"] * 1048576
Expand Down
9 changes: 8 additions & 1 deletion tests/test_playback.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from unittest import mock

import pytest
Expand Down Expand Up @@ -35,12 +37,14 @@ def test_on_source_setup_sets_properties(config, provider):
mock_source = mock.MagicMock()
provider.on_source_setup(mock_source)
spotify_cache_dir = backend.Extension.get_cache_dir(config)
spotify_data_dir = backend.Extension.get_data_dir(config)
cred_dir = os.path.join(spotify_data_dir, "credentials-cache")

assert mock_source.set_property.mock_calls == [
mock.call("username", "alice"),
mock.call("password", "password"),
mock.call("bitrate", "160"),
mock.call("cache-credentials", spotify_cache_dir),
mock.call("cache-credentials", cred_dir),
mock.call("cache-files", spotify_cache_dir),
mock.call("cache-max-size", 8589934592),
]
Expand All @@ -50,11 +54,14 @@ def test_on_source_setup_without_caching(config, provider):
config["spotify"]["allow_cache"] = False
mock_source = mock.MagicMock()
provider.on_source_setup(mock_source)
spotify_data_dir = backend.Extension.get_data_dir(config)
cred_dir = os.path.join(spotify_data_dir, "credentials-cache")

assert mock_source.set_property.mock_calls == [
mock.call("username", "alice"),
mock.call("password", "password"),
mock.call("bitrate", "160"),
mock.call("cache-credentials", cred_dir),
]


Expand Down