2018-01-07 21:13:32 +00:00
|
|
|
from django.urls import reverse
|
|
|
|
|
2018-05-22 19:10:48 +00:00
|
|
|
from funkwhale_api.music.serializers import TrackSerializer
|
2018-06-10 08:55:16 +00:00
|
|
|
from funkwhale_api.radios import filters, serializers
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
def test_can_list_config_options(logged_in_api_client):
|
2018-06-09 13:36:16 +00:00
|
|
|
url = reverse("api:v1:radios:radios-filters")
|
2018-06-16 12:42:26 +00:00
|
|
|
response = logged_in_api_client.get(url)
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
payload = response.data
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
expected = [f for f in filters.registry.values() if f.expose_in_api]
|
|
|
|
assert len(payload) == len(expected)
|
|
|
|
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
def test_can_validate_config(logged_in_api_client, factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
artist1 = factories["music.Artist"]()
|
|
|
|
artist2 = factories["music.Artist"]()
|
|
|
|
factories["music.Track"].create_batch(3, artist=artist1)
|
|
|
|
factories["music.Track"].create_batch(3, artist=artist2)
|
|
|
|
candidates = artist1.tracks.order_by("pk")
|
|
|
|
f = {"filters": [{"type": "artist", "ids": [artist1.pk]}]}
|
|
|
|
url = reverse("api:v1:radios:radios-validate")
|
2018-06-16 12:42:26 +00:00
|
|
|
response = logged_in_api_client.post(url, f, format="json")
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
payload = response.data
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
expected = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"count": candidates.count(),
|
|
|
|
"sample": TrackSerializer(candidates, many=True).data,
|
2018-01-07 21:13:32 +00:00
|
|
|
}
|
2018-06-09 13:36:16 +00:00
|
|
|
assert payload["filters"][0]["candidates"] == expected
|
|
|
|
assert payload["filters"][0]["errors"] == []
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
def test_can_validate_config_with_wrong_config(logged_in_api_client, factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
f = {"filters": [{"type": "artist", "ids": [999]}]}
|
|
|
|
url = reverse("api:v1:radios:radios-validate")
|
2018-06-16 12:42:26 +00:00
|
|
|
response = logged_in_api_client.post(url, f, format="json")
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
payload = response.data
|
2018-01-07 21:13:32 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
expected = {"count": None, "sample": None}
|
|
|
|
assert payload["filters"][0]["candidates"] == expected
|
|
|
|
assert len(payload["filters"][0]["errors"]) == 1
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
def test_saving_radio_sets_user(logged_in_api_client, factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
artist = factories["music.Artist"]()
|
|
|
|
f = {"name": "Test", "config": [{"type": "artist", "ids": [artist.pk]}]}
|
|
|
|
url = reverse("api:v1:radios:radios-list")
|
2018-06-16 12:42:26 +00:00
|
|
|
response = logged_in_api_client.post(url, f, format="json")
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
radio = logged_in_api_client.user.radios.latest("id")
|
2018-06-09 13:36:16 +00:00
|
|
|
assert radio.name == "Test"
|
2018-06-16 12:42:26 +00:00
|
|
|
assert radio.user == logged_in_api_client.user
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
def test_user_can_detail_his_radio(logged_in_api_client, factories):
|
|
|
|
radio = factories["radios.Radio"](user=logged_in_api_client.user)
|
2018-06-09 13:36:16 +00:00
|
|
|
url = reverse("api:v1:radios:radios-detail", kwargs={"pk": radio.pk})
|
2018-06-16 12:42:26 +00:00
|
|
|
response = logged_in_api_client.get(url)
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
def test_user_can_detail_public_radio(logged_in_api_client, factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
radio = factories["radios.Radio"](is_public=True)
|
|
|
|
url = reverse("api:v1:radios:radios-detail", kwargs={"pk": radio.pk})
|
2018-06-16 12:42:26 +00:00
|
|
|
response = logged_in_api_client.get(url)
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
def test_user_cannot_detail_someone_else_radio(logged_in_api_client, factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
radio = factories["radios.Radio"](is_public=False)
|
|
|
|
url = reverse("api:v1:radios:radios-detail", kwargs={"pk": radio.pk})
|
2018-06-16 12:42:26 +00:00
|
|
|
response = logged_in_api_client.get(url)
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
def test_user_can_edit_his_radio(logged_in_api_client, factories):
|
|
|
|
radio = factories["radios.Radio"](user=logged_in_api_client.user)
|
2018-06-09 13:36:16 +00:00
|
|
|
url = reverse("api:v1:radios:radios-detail", kwargs={"pk": radio.pk})
|
2018-06-16 12:42:26 +00:00
|
|
|
response = logged_in_api_client.put(
|
|
|
|
url, {"name": "new", "config": []}, format="json"
|
2018-06-09 13:36:16 +00:00
|
|
|
)
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
radio.refresh_from_db()
|
|
|
|
assert response.status_code == 200
|
2018-06-09 13:36:16 +00:00
|
|
|
assert radio.name == "new"
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
def test_user_cannot_edit_someone_else_radio(logged_in_api_client, factories):
|
|
|
|
radio = factories["radios.Radio"](is_public=True)
|
2018-06-09 13:36:16 +00:00
|
|
|
url = reverse("api:v1:radios:radios-detail", kwargs={"pk": radio.pk})
|
2018-06-16 12:42:26 +00:00
|
|
|
response = logged_in_api_client.put(
|
|
|
|
url, {"name": "new", "config": []}, format="json"
|
2018-06-09 13:36:16 +00:00
|
|
|
)
|
2018-01-07 21:13:32 +00:00
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
2018-06-16 12:42:26 +00:00
|
|
|
def test_user_cannot_delete_someone_else_radio(logged_in_api_client, factories):
|
|
|
|
radio = factories["radios.Radio"](is_public=True)
|
|
|
|
url = reverse("api:v1:radios:radios-detail", kwargs={"pk": radio.pk})
|
|
|
|
response = logged_in_api_client.delete(url)
|
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
2018-01-07 21:13:32 +00:00
|
|
|
def test_clean_config_is_called_on_serializer_save(mocker, factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
user = factories["users.User"]()
|
|
|
|
artist = factories["music.Artist"]()
|
|
|
|
data = {"name": "Test", "config": [{"type": "artist", "ids": [artist.pk]}]}
|
|
|
|
spied = mocker.spy(filters.registry["artist"], "clean_config")
|
2018-01-07 21:13:32 +00:00
|
|
|
serializer = serializers.RadioSerializer(data=data)
|
|
|
|
assert serializer.is_valid()
|
|
|
|
instance = serializer.save(user=user)
|
2018-06-09 13:36:16 +00:00
|
|
|
spied.assert_called_once_with(data["config"][0])
|
|
|
|
assert instance.config[0]["names"] == [artist.name]
|