def test_library_follow_serializer_validates_existing_follow(factories): follow = factories["federation.LibraryFollow"]() serializer = api_serializers.LibraryFollowSerializer( data={"target": follow.target.uuid}, context={"actor": follow.actor}) assert serializer.is_valid() is False assert "target" in serializer.errors
def test_library_follow_serializer_do_not_allow_own_library(factories): actor = factories["federation.Actor"]() library = factories["music.Library"](actor=actor) serializer = api_serializers.LibraryFollowSerializer( context={"actor": actor}) with pytest.raises(api_serializers.serializers.ValidationError, match=r".*own library.*"): serializer.validate_target(library)
def follows(self, request, *args, **kwargs): library = self.get_object() queryset = (library.received_follows.filter( target__actor=self.request.user.actor).select_related( "actor", "target__actor").order_by("-creation_date")) page = self.paginate_queryset(queryset) if page is not None: serializer = federation_api_serializers.LibraryFollowSerializer( page, many=True) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True) return Response(serializer.data)
def test_user_can_list_own_library_follows(factories, logged_in_api_client): actor = logged_in_api_client.user.create_actor() library = factories["music.Library"](actor=actor) another_library = factories["music.Library"](actor=actor) follow = factories["federation.LibraryFollow"](target=library) factories["federation.LibraryFollow"](target=another_library) url = reverse("api:v1:libraries-follows", kwargs={"uuid": library.uuid}) response = logged_in_api_client.get(url) assert response.data == { "count": 1, "next": None, "previous": None, "results": [federation_api_serializers.LibraryFollowSerializer(follow).data], }