2018-05-23 17:52:47 +00:00
|
|
|
import django_filters
|
|
|
|
|
|
|
|
from funkwhale_api.common import serializers
|
|
|
|
from funkwhale_api.users import models
|
|
|
|
|
|
|
|
|
|
|
|
class TestActionFilterSet(django_filters.FilterSet):
|
|
|
|
class Meta:
|
|
|
|
model = models.User
|
2018-06-09 13:36:16 +00:00
|
|
|
fields = ["is_active"]
|
2018-05-23 17:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestSerializer(serializers.ActionSerializer):
|
2018-06-21 17:21:51 +00:00
|
|
|
actions = [serializers.Action("test", allow_all=True)]
|
2018-05-23 17:52:47 +00:00
|
|
|
filterset_class = TestActionFilterSet
|
|
|
|
|
|
|
|
def handle_test(self, objects):
|
2018-06-09 13:36:16 +00:00
|
|
|
return {"hello": "world"}
|
2018-05-23 17:52:47 +00:00
|
|
|
|
|
|
|
|
2018-05-29 19:55:00 +00:00
|
|
|
class TestDangerousSerializer(serializers.ActionSerializer):
|
2018-06-21 17:21:51 +00:00
|
|
|
actions = [
|
|
|
|
serializers.Action("test", allow_all=True),
|
|
|
|
serializers.Action("test_dangerous"),
|
|
|
|
]
|
2018-05-29 19:55:00 +00:00
|
|
|
|
|
|
|
def handle_test(self, objects):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handle_test_dangerous(self, objects):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-06-21 17:21:51 +00:00
|
|
|
class TestDeleteOnlyInactiveSerializer(serializers.ActionSerializer):
|
|
|
|
actions = [serializers.Action("test", allow_all=True, filters={"is_active": False})]
|
|
|
|
filterset_class = TestActionFilterSet
|
|
|
|
|
|
|
|
def handle_test(self, objects):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-05-23 17:52:47 +00:00
|
|
|
def test_action_serializer_validates_action():
|
2018-06-09 13:36:16 +00:00
|
|
|
data = {"objects": "all", "action": "nope"}
|
2018-05-23 17:52:47 +00:00
|
|
|
serializer = TestSerializer(data, queryset=models.User.objects.none())
|
|
|
|
|
|
|
|
assert serializer.is_valid() is False
|
2018-06-09 13:36:16 +00:00
|
|
|
assert "action" in serializer.errors
|
2018-05-23 17:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_action_serializer_validates_objects():
|
2018-06-09 13:36:16 +00:00
|
|
|
data = {"objects": "nope", "action": "test"}
|
2018-05-23 17:52:47 +00:00
|
|
|
serializer = TestSerializer(data, queryset=models.User.objects.none())
|
|
|
|
|
|
|
|
assert serializer.is_valid() is False
|
2018-06-09 13:36:16 +00:00
|
|
|
assert "objects" in serializer.errors
|
2018-05-23 17:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_action_serializers_objects_clean_ids(factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
user1 = factories["users.User"]()
|
2018-06-09 15:41:59 +00:00
|
|
|
factories["users.User"]()
|
2018-05-23 17:52:47 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
data = {"objects": [user1.pk], "action": "test"}
|
2018-05-23 17:52:47 +00:00
|
|
|
serializer = TestSerializer(data, queryset=models.User.objects.all())
|
|
|
|
|
2018-06-21 17:21:51 +00:00
|
|
|
assert serializer.is_valid(raise_exception=True) is True
|
2018-06-09 13:36:16 +00:00
|
|
|
assert list(serializer.validated_data["objects"]) == [user1]
|
2018-05-23 17:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_action_serializers_objects_clean_all(factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
user1 = factories["users.User"]()
|
|
|
|
user2 = factories["users.User"]()
|
2018-05-23 17:52:47 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
data = {"objects": "all", "action": "test"}
|
2018-05-23 17:52:47 +00:00
|
|
|
serializer = TestSerializer(data, queryset=models.User.objects.all())
|
|
|
|
|
2018-06-21 17:21:51 +00:00
|
|
|
assert serializer.is_valid(raise_exception=True) is True
|
2018-06-09 13:36:16 +00:00
|
|
|
assert list(serializer.validated_data["objects"]) == [user1, user2]
|
2018-05-23 17:52:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_action_serializers_save(factories, mocker):
|
2018-06-09 13:36:16 +00:00
|
|
|
handler = mocker.spy(TestSerializer, "handle_test")
|
2018-06-09 15:41:59 +00:00
|
|
|
factories["users.User"]()
|
|
|
|
factories["users.User"]()
|
2018-05-23 17:52:47 +00:00
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
data = {"objects": "all", "action": "test"}
|
2018-05-23 17:52:47 +00:00
|
|
|
serializer = TestSerializer(data, queryset=models.User.objects.all())
|
|
|
|
|
2018-06-21 17:21:51 +00:00
|
|
|
assert serializer.is_valid(raise_exception=True) is True
|
2018-05-23 17:52:47 +00:00
|
|
|
result = serializer.save()
|
2018-06-09 13:36:16 +00:00
|
|
|
assert result == {"updated": 2, "action": "test", "result": {"hello": "world"}}
|
2018-05-23 17:52:47 +00:00
|
|
|
handler.assert_called_once()
|
|
|
|
|
|
|
|
|
|
|
|
def test_action_serializers_filterset(factories):
|
2018-06-09 15:41:59 +00:00
|
|
|
factories["users.User"](is_active=False)
|
2018-06-09 13:36:16 +00:00
|
|
|
user2 = factories["users.User"](is_active=True)
|
|
|
|
|
|
|
|
data = {"objects": "all", "action": "test", "filters": {"is_active": True}}
|
2018-05-23 17:52:47 +00:00
|
|
|
serializer = TestSerializer(data, queryset=models.User.objects.all())
|
|
|
|
|
2018-06-21 17:21:51 +00:00
|
|
|
assert serializer.is_valid(raise_exception=True) is True
|
2018-06-09 13:36:16 +00:00
|
|
|
assert list(serializer.validated_data["objects"]) == [user2]
|
2018-05-23 19:50:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_action_serializers_validates_at_least_one_object():
|
2018-06-09 13:36:16 +00:00
|
|
|
data = {"objects": "all", "action": "test"}
|
2018-05-23 19:50:23 +00:00
|
|
|
serializer = TestSerializer(data, queryset=models.User.objects.none())
|
|
|
|
|
|
|
|
assert serializer.is_valid() is False
|
2018-06-09 13:36:16 +00:00
|
|
|
assert "non_field_errors" in serializer.errors
|
2018-05-29 19:55:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_dangerous_actions_refuses_all(factories):
|
2018-06-09 13:36:16 +00:00
|
|
|
factories["users.User"]()
|
|
|
|
data = {"objects": "all", "action": "test_dangerous"}
|
|
|
|
serializer = TestDangerousSerializer(data, queryset=models.User.objects.all())
|
2018-05-29 19:55:00 +00:00
|
|
|
|
|
|
|
assert serializer.is_valid() is False
|
2018-06-09 13:36:16 +00:00
|
|
|
assert "non_field_errors" in serializer.errors
|
2018-05-29 19:55:00 +00:00
|
|
|
|
|
|
|
|
2018-06-21 17:21:51 +00:00
|
|
|
def test_action_serializers_can_require_filter(factories):
|
|
|
|
user1 = factories["users.User"](is_active=False)
|
|
|
|
factories["users.User"](is_active=True)
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
data = {"objects": "all", "action": "test"}
|
2018-06-21 17:21:51 +00:00
|
|
|
serializer = TestDeleteOnlyInactiveSerializer(
|
|
|
|
data, queryset=models.User.objects.all()
|
|
|
|
)
|
2018-05-29 19:55:00 +00:00
|
|
|
|
2018-06-21 17:21:51 +00:00
|
|
|
assert serializer.is_valid(raise_exception=True) is True
|
|
|
|
assert list(serializer.validated_data["objects"]) == [user1]
|