def test_can_get_libraries_for_music_entities(factories, api_client, entity, preferences): preferences["common__api_authentication_required"] = False upload = factories["music.Upload"](playable=True) # another private library that should not appear factories["music.Upload"](import_status="finished", library__privacy_level="me", track=upload.track).library library = upload.library data = { "artist": upload.track.artist, "album": upload.track.album, "track": upload.track, } url = reverse("api:v1:{}s-libraries".format(entity), kwargs={"pk": data[entity].pk}) response = api_client.get(url) expected = federation_api_serializers.LibrarySerializer(library).data assert response.status_code == 200 assert response.data == { "count": 1, "next": None, "previous": None, "results": [expected], }
def libraries(self, request, *args, **kwargs): obj = self.get_object() actor = utils.get_actor_from_request(request) uploads = models.Upload.objects.all() uploads = filter_uploads(obj, uploads) uploads = uploads.playable_by(actor) qs = models.Library.objects.filter( pk__in=uploads.values_list("library", flat=True), channel=None, ).annotate(_uploads_count=Count("uploads")) qs = qs.prefetch_related("actor") page = self.paginate_queryset(qs) if page is not None: serializer = federation_api_serializers.LibrarySerializer(page, many=True) return self.get_paginated_response(serializer.data) serializer = federation_api_serializers.LibrarySerializer(qs, many=True) return Response(serializer.data)
def test_library_serializer_latest_scan(factories): library = factories["music.Library"](uploads_count=5678) scan = factories["music.LibraryScan"](library=library) setattr(library, "latest_scans", [scan]) expected = api_serializers.LibraryScanSerializer(scan).data serializer = api_serializers.LibrarySerializer(library) assert serializer.data["latest_scan"] == expected
def view(self, request, *args, **kwargs): obj = self.get_object() actor = utils.get_actor_from_request(request) uploads = models.Upload.objects.all() uploads = filter_uploads(obj, uploads) uploads = uploads.playable_by(actor) libraries = models.Library.objects.filter( pk__in=uploads.values_list("library", flat=True)) libraries = libraries.select_related("actor") page = self.paginate_queryset(libraries) if page is not None: serializer = federation_api_serializers.LibrarySerializer( page, many=True) return self.get_paginated_response(serializer.data) serializer = federation_api_serializers.LibrarySerializer(libraries, many=True) return Response(serializer.data)
def test_user_can_fetch_library_using_url(mocker, factories, logged_in_api_client): library = factories["music.Library"]() mocked_retrieve = mocker.patch("funkwhale_api.federation.utils.retrieve", return_value=library) url = reverse("api:v1:federation:libraries-fetch") response = logged_in_api_client.post(url, {"fid": library.fid}) assert mocked_retrieve.call_count == 1 args = mocked_retrieve.call_args assert args[0] == (library.fid, ) assert args[1][ "queryset"].model == views.MusicLibraryViewSet.queryset.model assert args[1]["serializer_class"] == serializers.LibrarySerializer assert response.status_code == 200 assert response.data["results"] == [ api_serializers.LibrarySerializer(library).data ]
def test_library_serializer(factories, to_api_date): library = factories["music.Library"](uploads_count=5678) expected = { "fid": library.fid, "uuid": str(library.uuid), "actor": serializers.APIActorSerializer(library.actor).data, "name": library.name, "description": library.description, "creation_date": to_api_date(library.creation_date), "uploads_count": library.uploads_count, "privacy_level": library.privacy_level, "follow": None, "latest_scan": None, } serializer = api_serializers.LibrarySerializer(library) assert serializer.data == expected
def test_library_serializer_with_follow(factories, to_api_date): library = factories["music.Library"](uploads_count=5678) follow = factories["federation.LibraryFollow"](target=library) setattr(library, "_follows", [follow]) expected = { "fid": library.fid, "uuid": str(library.uuid), "actor": serializers.APIActorSerializer(library.actor).data, "name": library.name, "description": library.description, "creation_date": to_api_date(library.creation_date), "uploads_count": library.uploads_count, "privacy_level": library.privacy_level, "follow": api_serializers.NestedLibraryFollowSerializer(follow).data, "latest_scan": None, } serializer = api_serializers.LibrarySerializer(library) assert serializer.data == expected