mirror of
				https://github.com/KevinMidboe/spotify-downloader.git
				synced 2025-10-29 18:00:15 +00:00 
			
		
		
		
	Replace class SpotifyAuthorize with @must_be_authorized (#506)
				
					
				
			* @must_be_authorized decorator for functions in spotify_tools.py * We don't need this * Add tests * Update CHANGELOG.md
This commit is contained in:
		| @@ -1,4 +1,7 @@ | ||||
| from spotdl import spotify_tools | ||||
| from spotdl import const | ||||
|  | ||||
| import spotipy | ||||
|  | ||||
| import os | ||||
| import pytest | ||||
| @@ -6,26 +9,45 @@ import loader | ||||
|  | ||||
| loader.load_defaults() | ||||
|  | ||||
| @pytest.fixture(scope="module") | ||||
| def spotify(): | ||||
|     return spotify_tools.SpotifyAuthorize() | ||||
|  | ||||
| def test_generate_token(spotify): | ||||
|     token = spotify.generate_token() | ||||
| def test_generate_token(): | ||||
|     token = spotify_tools.generate_token() | ||||
|     assert len(token) == 83 | ||||
|  | ||||
|  | ||||
| def test_refresh_token(spotify): | ||||
|     old_instance = spotify.spotify | ||||
|     spotify.refresh_token() | ||||
|     new_instance = spotify.spotify | ||||
|     assert not old_instance == new_instance | ||||
| class TestMustBeAuthorizedDecorator: | ||||
|     def test_spotify_instance_is_unset(self): | ||||
|         spotify_tools.spotify = None | ||||
|  | ||||
|         @spotify_tools.must_be_authorized | ||||
|         def sample_func(): | ||||
|             return True | ||||
|  | ||||
|         assert sample_func() | ||||
|  | ||||
|     def test_spotify_instance_forces_assertion_error(self): | ||||
|         @spotify_tools.must_be_authorized | ||||
|         def sample_func(): | ||||
|             raise AssertionError | ||||
|  | ||||
|         with pytest.raises(AssertionError): | ||||
|             sample_func() | ||||
|  | ||||
|     def test_fake_token_generator(self, monkeypatch): | ||||
|         spotify_tools.spotify = None | ||||
|         monkeypatch.setattr(spotify_tools, "generate_token", lambda: 123123) | ||||
|  | ||||
|         with pytest.raises(spotipy.client.SpotifyException): | ||||
|             spotify_tools.generate_metadata("ncs - spectre") | ||||
|  | ||||
|     def test_correct_token(self): | ||||
|         assert spotify_tools.generate_metadata("ncs - spectre") | ||||
|  | ||||
|  | ||||
| class TestGenerateMetadata: | ||||
|     @pytest.fixture(scope="module") | ||||
|     def metadata_fixture(self, spotify): | ||||
|         metadata = spotify.generate_metadata("ncs - spectre") | ||||
|     def metadata_fixture(self): | ||||
|         metadata = spotify_tools.generate_metadata("ncs - spectre") | ||||
|         return metadata | ||||
|  | ||||
|     def test_len(self, metadata_fixture): | ||||
| @@ -41,7 +63,7 @@ class TestGenerateMetadata: | ||||
|         assert metadata_fixture["duration"] == 230.634 | ||||
|  | ||||
|  | ||||
| def test_get_playlists(spotify): | ||||
| def test_get_playlists(): | ||||
|     expect_playlist_ids = [ | ||||
|         "34gWCK8gVeYDPKcctB6BQJ", | ||||
|         "04wTU2c2WNQG9XE5oSLYfj", | ||||
| @@ -53,15 +75,15 @@ def test_get_playlists(spotify): | ||||
|         for playlist_id in expect_playlist_ids | ||||
|     ] | ||||
|  | ||||
|     playlists = spotify.get_playlists("uqlakumu7wslkoen46s5bulq0") | ||||
|     playlists = spotify_tools.get_playlists("uqlakumu7wslkoen46s5bulq0") | ||||
|     assert playlists == expect_playlists | ||||
|  | ||||
|  | ||||
| def test_write_user_playlist(tmpdir, spotify, monkeypatch): | ||||
| def test_write_user_playlist(tmpdir, monkeypatch): | ||||
|     expect_tracks = 17 | ||||
|     text_file = os.path.join(str(tmpdir), "test_us.txt") | ||||
|     monkeypatch.setattr("builtins.input", lambda x: 1) | ||||
|     spotify.write_user_playlist("uqlakumu7wslkoen46s5bulq0", text_file) | ||||
|     spotify_tools.write_user_playlist("uqlakumu7wslkoen46s5bulq0", text_file) | ||||
|     with open(text_file, "r") as f: | ||||
|         tracks = len(f.readlines()) | ||||
|     assert tracks == expect_tracks | ||||
| @@ -69,8 +91,8 @@ def test_write_user_playlist(tmpdir, spotify, monkeypatch): | ||||
|  | ||||
| class TestFetchPlaylist: | ||||
|     @pytest.fixture(scope="module") | ||||
|     def playlist_fixture(self, spotify): | ||||
|         playlist = spotify.fetch_playlist( | ||||
|     def playlist_fixture(self): | ||||
|         playlist = spotify_tools.fetch_playlist( | ||||
|             "https://open.spotify.com/playlist/0fWBMhGh38y0wsYWwmM9Kt" | ||||
|         ) | ||||
|         return playlist | ||||
| @@ -82,10 +104,10 @@ class TestFetchPlaylist: | ||||
|         assert playlist_fixture["tracks"]["total"] == 14 | ||||
|  | ||||
|  | ||||
| def test_write_playlist(tmpdir, spotify): | ||||
| def test_write_playlist(tmpdir): | ||||
|     expect_tracks = 14 | ||||
|     text_file = os.path.join(str(tmpdir), "test_pl.txt") | ||||
|     spotify.write_playlist( | ||||
|     spotify_tools.write_playlist( | ||||
|         "https://open.spotify.com/playlist/0fWBMhGh38y0wsYWwmM9Kt", text_file | ||||
|     ) | ||||
|     with open(text_file, "r") as f: | ||||
| @@ -96,8 +118,8 @@ def test_write_playlist(tmpdir, spotify): | ||||
| # XXX: Mock this test off if it fails in future | ||||
| class TestFetchAlbum: | ||||
|     @pytest.fixture(scope="module") | ||||
|     def album_fixture(self, spotify): | ||||
|         album = spotify.fetch_album( | ||||
|     def album_fixture(self): | ||||
|         album = spotify_tools.fetch_album( | ||||
|             "https://open.spotify.com/album/499J8bIsEnU7DSrosFDJJg" | ||||
|         ) | ||||
|         return album | ||||
| @@ -112,8 +134,8 @@ class TestFetchAlbum: | ||||
| # XXX: Mock this test off if it fails in future | ||||
| class TestFetchAlbumsFromArtist: | ||||
|     @pytest.fixture(scope="module") | ||||
|     def albums_from_artist_fixture(self, spotify): | ||||
|         albums = spotify.fetch_albums_from_artist( | ||||
|     def albums_from_artist_fixture(self): | ||||
|         albums = spotify_tools.fetch_albums_from_artist( | ||||
|             "https://open.spotify.com/artist/7oPftvlwr6VrsViSDV7fJY" | ||||
|         ) | ||||
|         return albums | ||||
| @@ -134,10 +156,10 @@ class TestFetchAlbumsFromArtist: | ||||
|         assert albums_from_artist_fixture[0]["total_tracks"] == 12 | ||||
|  | ||||
|  | ||||
| def test_write_all_albums_from_artist(tmpdir, spotify): | ||||
| def test_write_all_albums_from_artist(tmpdir): | ||||
|     expect_tracks = 282 | ||||
|     text_file = os.path.join(str(tmpdir), "test_ab.txt") | ||||
|     spotify.write_all_albums_from_artist( | ||||
|     spotify_tools.write_all_albums_from_artist( | ||||
|         "https://open.spotify.com/artist/4dpARuHxo51G3z768sgnrY", text_file | ||||
|     ) | ||||
|     with open(text_file, "r") as f: | ||||
| @@ -145,10 +167,10 @@ def test_write_all_albums_from_artist(tmpdir, spotify): | ||||
|     assert tracks == expect_tracks | ||||
|  | ||||
|  | ||||
| def test_write_album(tmpdir, spotify): | ||||
| def test_write_album(tmpdir): | ||||
|     expect_tracks = 15 | ||||
|     text_file = os.path.join(str(tmpdir), "test_al.txt") | ||||
|     spotify.write_album( | ||||
|     spotify_tools.write_album( | ||||
|         "https://open.spotify.com/album/499J8bIsEnU7DSrosFDJJg", text_file | ||||
|     ) | ||||
|     with open(text_file, "r") as f: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user