Newer
Older
import datetime
Eliot Berriot
committed
import json
import pytest
from django.utils import timezone
Eliot Berriot
committed
from django.urls import reverse
Eliot Berriot
committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from rest_framework.response import Response
from funkwhale_api.music import models as music_models
from funkwhale_api.music import views as music_views
from funkwhale_api.subsonic import renderers
from funkwhale_api.subsonic import serializers
def render_json(data):
return json.loads(renderers.SubsonicJSONRenderer().render(data))
def test_render_content_json(db, api_client):
url = reverse('api:subsonic-ping')
response = api_client.get(url, {'f': 'json'})
expected = {
'status': 'ok',
'version': '1.16.0'
}
assert response.status_code == 200
assert json.loads(response.content) == render_json(expected)
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_exception_wrong_credentials(f, db, api_client):
url = reverse('api:subsonic-ping')
response = api_client.get(url, {'f': f, 'u': 'yolo'})
expected = {
'status': 'failed',
'error': {
'code': 40,
'message': 'Wrong username or password.'
}
}
assert response.status_code == 200
assert response.data == expected
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_get_license(f, db, logged_in_api_client, mocker):
url = reverse('api:subsonic-get-license')
assert url.endswith('getLicense') is True
now = timezone.now()
mocker.patch('django.utils.timezone.now', return_value=now)
response = logged_in_api_client.get(url, {'f': f})
expected = {
'status': 'ok',
'version': '1.16.0',
'license': {
'valid': 'true',
'email': 'valid@valid.license',
'licenseExpires': now + datetime.timedelta(days=365)
}
}
assert response.status_code == 200
assert response.data == expected
Eliot Berriot
committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_ping(f, db, api_client):
url = reverse('api:subsonic-ping')
response = api_client.get(url, {'f': f})
expected = {
'status': 'ok',
'version': '1.16.0',
}
assert response.status_code == 200
assert response.data == expected
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_get_artists(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-get-artists')
assert url.endswith('getArtists') is True
artists = factories['music.Artist'].create_batch(size=10)
expected = {
'artists': serializers.GetArtistsSerializer(
music_models.Artist.objects.all()
).data
}
response = logged_in_api_client.get(url)
assert response.status_code == 200
assert response.data == expected
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_get_artist(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-get-artist')
assert url.endswith('getArtist') is True
artist = factories['music.Artist']()
albums = factories['music.Album'].create_batch(size=3, artist=artist)
expected = {
'artist': serializers.GetArtistSerializer(artist).data
}
response = logged_in_api_client.get(url, {'id': artist.pk})
assert response.status_code == 200
assert response.data == expected
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_get_artist_info2(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-get-artist-info2')
assert url.endswith('getArtistInfo2') is True
artist = factories['music.Artist']()
expected = {
'artist-info2': {}
}
response = logged_in_api_client.get(url, {'id': artist.pk})
assert response.status_code == 200
assert response.data == expected
Eliot Berriot
committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_get_album(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-get-album')
assert url.endswith('getAlbum') is True
artist = factories['music.Artist']()
album = factories['music.Album'](artist=artist)
tracks = factories['music.Track'].create_batch(size=3, album=album)
expected = {
'album': serializers.GetAlbumSerializer(album).data
}
response = logged_in_api_client.get(url, {'f': f, 'id': album.pk})
assert response.status_code == 200
assert response.data == expected
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_stream(f, db, logged_in_api_client, factories, mocker):
url = reverse('api:subsonic-stream')
mocked_serve = mocker.spy(
music_views, 'handle_serve')
assert url.endswith('stream') is True
artist = factories['music.Artist']()
album = factories['music.Album'](artist=artist)
track = factories['music.Track'](album=album)
tf = factories['music.TrackFile'](track=track)
response = logged_in_api_client.get(url, {'f': f, 'id': track.pk})
mocked_serve.assert_called_once_with(
track_file=tf
)
assert response.status_code == 200
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_star(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-star')
assert url.endswith('star') is True
track = factories['music.Track']()
response = logged_in_api_client.get(url, {'f': f, 'id': track.pk})
assert response.status_code == 200
assert response.data == {'status': 'ok'}
favorite = logged_in_api_client.user.track_favorites.latest('id')
assert favorite.track == track
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_unstar(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-unstar')
assert url.endswith('unstar') is True
track = factories['music.Track']()
favorite = factories['favorites.TrackFavorite'](
track=track, user=logged_in_api_client.user)
response = logged_in_api_client.get(url, {'f': f, 'id': track.pk})
assert response.status_code == 200
assert response.data == {'status': 'ok'}
assert logged_in_api_client.user.track_favorites.count() == 0
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_get_starred2(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-get-starred2')
assert url.endswith('getStarred2') is True
track = factories['music.Track']()
favorite = factories['favorites.TrackFavorite'](
track=track, user=logged_in_api_client.user)
response = logged_in_api_client.get(url, {'f': f, 'id': track.pk})
assert response.status_code == 200
assert response.data == {
'song': serializers.get_starred_tracks_data([favorite])
}
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_get_album_list2(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-get-album-list2')
assert url.endswith('getAlbumList2') is True
album1 = factories['music.Album']()
album2 = factories['music.Album']()
response = logged_in_api_client.get(url, {'f': f, 'type': 'newest'})
assert response.status_code == 200
assert response.data == {
'albumList2': {
'album': serializers.get_album_list2_data([album2, album1])
}
}
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_search3(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-search3')
assert url.endswith('search3') is True
artist = factories['music.Artist'](name='testvalue')
factories['music.Artist'](name='nope')
album = factories['music.Album'](title='testvalue')
factories['music.Album'](title='nope')
track = factories['music.Track'](title='testvalue')
factories['music.Track'](title='nope')
response = logged_in_api_client.get(url, {'f': f, 'query': 'testval'})
artist_qs = music_models.Artist.objects.with_albums_count().filter(
pk=artist.pk).values('_albums_count', 'id', 'name')
assert response.status_code == 200
assert response.data == {
'searchResult3': {
'artist': [serializers.get_artist_data(a) for a in artist_qs],
'album': serializers.get_album_list2_data([album]),
'song': serializers.get_song_list_data([track]),
}
}
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_get_playlists(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-get-playlists')
assert url.endswith('getPlaylists') is True
playlist = factories['playlists.Playlist'](
user=logged_in_api_client.user
)
response = logged_in_api_client.get(url, {'f': f})
qs = playlist.__class__.objects.with_tracks_count()
assert response.status_code == 200
assert response.data == {
'playlists': {
'playlist': [serializers.get_playlist_data(qs.first())],
}
}
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_get_playlist(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-get-playlist')
assert url.endswith('getPlaylist') is True
playlist = factories['playlists.Playlist'](
user=logged_in_api_client.user
)
response = logged_in_api_client.get(url, {'f': f, 'id': playlist.pk})
qs = playlist.__class__.objects.with_tracks_count()
assert response.status_code == 200
assert response.data == {
'playlist': serializers.get_playlist_detail_data(qs.first())
}
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_update_playlist(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-update-playlist')
assert url.endswith('updatePlaylist') is True
playlist = factories['playlists.Playlist'](
user=logged_in_api_client.user
)
plt = factories['playlists.PlaylistTrack'](
index=0, playlist=playlist)
new_track = factories['music.Track']()
response = logged_in_api_client.get(
url, {
'f': f,
'name': 'new_name',
'playlistId': playlist.pk,
'songIdToAdd': new_track.pk,
'songIndexToRemove': 0})
playlist.refresh_from_db()
assert response.status_code == 200
assert playlist.name == 'new_name'
assert playlist.playlist_tracks.count() == 1
assert playlist.playlist_tracks.first().track_id == new_track.pk
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_delete_playlist(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-delete-playlist')
assert url.endswith('deletePlaylist') is True
playlist = factories['playlists.Playlist'](
user=logged_in_api_client.user
)
response = logged_in_api_client.get(
url, {'f': f, 'id': playlist.pk})
assert response.status_code == 200
with pytest.raises(playlist.__class__.DoesNotExist):
playlist.refresh_from_db()
@pytest.mark.parametrize('f', ['xml', 'json'])
def test_create_playlist(f, db, logged_in_api_client, factories):
url = reverse('api:subsonic-create-playlist')
assert url.endswith('createPlaylist') is True
track = factories['music.Track']()
response = logged_in_api_client.get(
url, {'f': f, 'name': 'hello', 'songId': track.pk})
assert response.status_code == 200
playlist = logged_in_api_client.user.playlists.latest('id')
plt = playlist.playlist_tracks.latest('id')
assert playlist.name == 'hello'
assert plt.index == 0
assert plt.track == track
qs = playlist.__class__.objects.with_tracks_count()
assert response.data == {
'playlist': serializers.get_playlist_detail_data(qs.first())
}