Newer
Older
Eliot Berriot
committed
1
2
3
4
5
6
7
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
112
113
114
115
116
117
118
119
120
import json
import pytest
from django.urls import reverse
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_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_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