2017-12-27 22:32:02 +00:00
|
|
|
import os
|
2018-06-10 08:55:16 +00:00
|
|
|
|
2017-12-24 18:15:21 +00:00
|
|
|
import pytest
|
|
|
|
from django.urls import reverse
|
|
|
|
|
|
|
|
|
2017-12-27 22:32:02 +00:00
|
|
|
DATA_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
2017-12-24 18:15:21 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"route,method",
|
|
|
|
[
|
|
|
|
("api:v1:tags-list", "get"),
|
|
|
|
("api:v1:tracks-list", "get"),
|
|
|
|
("api:v1:artists-list", "get"),
|
|
|
|
("api:v1:albums-list", "get"),
|
|
|
|
],
|
|
|
|
)
|
2018-03-25 20:27:38 +00:00
|
|
|
def test_can_restrict_api_views_to_authenticated_users(
|
2018-06-09 13:36:16 +00:00
|
|
|
db, route, method, preferences, client
|
|
|
|
):
|
2017-12-24 18:15:21 +00:00
|
|
|
url = reverse(route)
|
2018-06-09 13:36:16 +00:00
|
|
|
preferences["common__api_authentication_required"] = True
|
2017-12-24 18:15:21 +00:00
|
|
|
response = getattr(client, method)(url)
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
|
|
|
|
2018-09-22 12:29:30 +00:00
|
|
|
def test_upload_url_is_restricted_to_authenticated_users(
|
2018-06-09 13:36:16 +00:00
|
|
|
api_client, factories, preferences
|
|
|
|
):
|
|
|
|
preferences["common__api_authentication_required"] = True
|
2018-09-22 12:29:30 +00:00
|
|
|
upload = factories["music.Upload"](library__privacy_level="instance")
|
|
|
|
assert upload.audio_file is not None
|
|
|
|
url = upload.track.listen_url
|
2018-03-25 20:27:38 +00:00
|
|
|
response = api_client.get(url)
|
2017-12-24 18:15:21 +00:00
|
|
|
assert response.status_code == 401
|
|
|
|
|
2018-03-25 20:27:38 +00:00
|
|
|
|
2018-09-22 12:29:30 +00:00
|
|
|
def test_upload_url_is_accessible_to_authenticated_users(
|
2018-06-09 13:36:16 +00:00
|
|
|
logged_in_api_client, factories, preferences
|
|
|
|
):
|
2018-09-06 18:35:02 +00:00
|
|
|
actor = logged_in_api_client.user.create_actor()
|
2018-06-09 13:36:16 +00:00
|
|
|
preferences["common__api_authentication_required"] = True
|
2018-09-25 20:18:02 +00:00
|
|
|
upload = factories["music.Upload"](library__actor=actor, import_status="finished")
|
2018-09-22 12:29:30 +00:00
|
|
|
assert upload.audio_file is not None
|
|
|
|
url = upload.track.listen_url
|
2018-03-25 20:27:38 +00:00
|
|
|
response = logged_in_api_client.get(url)
|
2017-12-24 18:15:21 +00:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
2018-09-22 12:29:30 +00:00
|
|
|
assert response["X-Accel-Redirect"] == "/_protected{}".format(upload.audio_file.url)
|