Skip to content

Commit 984151a

Browse files
authored
Merge pull request #357 from beaverking1212/credentials-cache
Improve credentials cache
2 parents 78768aa + 953f12b commit 984151a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

mopidy_spotify/backend.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ class SpotifyPlaybackProvider(backend.PlaybackProvider):
3737
def __init__(self, *args, **kwargs):
3838
super().__init__(*args, **kwargs)
3939
self._cache_location = Extension().get_cache_dir(self.backend._config)
40+
self._data_location = Extension().get_data_dir(self.backend._config)
4041
self._config = self.backend._config["spotify"]
4142

43+
self._credentials_dir = self._data_location / "credentials-cache"
44+
if not self._credentials_dir.exists():
45+
self._credentials_dir.mkdir(mode=0o700)
46+
4247
def on_source_setup(self, source):
4348
for prop in ["username", "password", "bitrate"]:
4449
source.set_property(prop, str(self._config[prop]))
50+
source.set_property("cache-credentials", self._credentials_dir)
4551
if self._config["allow_cache"]:
46-
source.set_property("cache-credentials", self._cache_location)
4752
source.set_property("cache-files", self._cache_location)
4853
source.set_property(
4954
"cache-max-size", self._config["cache_size"] * 1048576

tests/test_playback.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ def test_on_source_setup_sets_properties(config, provider):
3535
mock_source = mock.MagicMock()
3636
provider.on_source_setup(mock_source)
3737
spotify_cache_dir = backend.Extension.get_cache_dir(config)
38+
spotify_data_dir = backend.Extension.get_data_dir(config)
39+
cred_dir = spotify_data_dir / "credentials-cache"
3840

3941
assert mock_source.set_property.mock_calls == [
4042
mock.call("username", "alice"),
4143
mock.call("password", "password"),
4244
mock.call("bitrate", "160"),
43-
mock.call("cache-credentials", spotify_cache_dir),
45+
mock.call("cache-credentials", cred_dir),
4446
mock.call("cache-files", spotify_cache_dir),
4547
mock.call("cache-max-size", 8589934592),
4648
]
@@ -50,11 +52,14 @@ def test_on_source_setup_without_caching(config, provider):
5052
config["spotify"]["allow_cache"] = False
5153
mock_source = mock.MagicMock()
5254
provider.on_source_setup(mock_source)
55+
spotify_data_dir = backend.Extension.get_data_dir(config)
56+
cred_dir = spotify_data_dir / "credentials-cache"
5357

5458
assert mock_source.set_property.mock_calls == [
5559
mock.call("username", "alice"),
5660
mock.call("password", "password"),
5761
mock.call("bitrate", "160"),
62+
mock.call("cache-credentials", cred_dir),
5863
]
5964

6065

0 commit comments

Comments
 (0)