2018-03-18 20:31:22 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
|
|
from django.http import Http404
|
|
|
|
|
|
|
|
from funkwhale_api.common import permissions
|
|
|
|
|
|
|
|
|
|
|
|
def test_owner_permission_owner_field_ok(nodb_factories, api_request):
|
2018-06-09 13:36:16 +00:00
|
|
|
playlist = nodb_factories["playlists.Playlist"]()
|
2018-03-18 20:31:22 +00:00
|
|
|
view = APIView.as_view()
|
|
|
|
permission = permissions.OwnerPermission()
|
2018-06-09 13:36:16 +00:00
|
|
|
request = api_request.get("/")
|
|
|
|
setattr(request, "user", playlist.user)
|
2018-03-18 20:31:22 +00:00
|
|
|
check = permission.has_object_permission(request, view, playlist)
|
|
|
|
|
|
|
|
assert check is True
|
|
|
|
|
|
|
|
|
2018-03-25 15:18:44 +00:00
|
|
|
def test_owner_permission_owner_field_not_ok(
|
2018-06-09 13:36:16 +00:00
|
|
|
anonymous_user, nodb_factories, api_request
|
|
|
|
):
|
|
|
|
playlist = nodb_factories["playlists.Playlist"]()
|
2018-03-18 20:31:22 +00:00
|
|
|
view = APIView.as_view()
|
|
|
|
permission = permissions.OwnerPermission()
|
2018-06-09 13:36:16 +00:00
|
|
|
request = api_request.get("/")
|
|
|
|
setattr(request, "user", anonymous_user)
|
2018-03-18 20:31:22 +00:00
|
|
|
|
|
|
|
with pytest.raises(Http404):
|
|
|
|
permission.has_object_permission(request, view, playlist)
|
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
def test_owner_permission_read_only(anonymous_user, nodb_factories, api_request):
|
|
|
|
playlist = nodb_factories["playlists.Playlist"]()
|
2018-03-18 20:31:22 +00:00
|
|
|
view = APIView.as_view()
|
2018-06-09 13:36:16 +00:00
|
|
|
setattr(view, "owner_checks", ["write"])
|
2018-03-18 20:31:22 +00:00
|
|
|
permission = permissions.OwnerPermission()
|
2018-06-09 13:36:16 +00:00
|
|
|
request = api_request.get("/")
|
|
|
|
setattr(request, "user", anonymous_user)
|
2018-03-18 20:31:22 +00:00
|
|
|
check = permission.has_object_permission(request, view, playlist)
|
|
|
|
|
|
|
|
assert check is True
|