2018-09-24 18:44:22 +00:00
|
|
|
import pytest
|
|
|
|
|
2018-09-06 18:35:02 +00:00
|
|
|
from funkwhale_api.federation import api_serializers
|
|
|
|
from funkwhale_api.federation import serializers
|
|
|
|
|
|
|
|
|
|
|
|
def test_library_serializer(factories):
|
2018-09-22 12:29:30 +00:00
|
|
|
library = factories["music.Library"](uploads_count=5678)
|
2018-09-06 18:35:02 +00:00
|
|
|
expected = {
|
|
|
|
"fid": library.fid,
|
|
|
|
"uuid": str(library.uuid),
|
|
|
|
"actor": serializers.APIActorSerializer(library.actor).data,
|
|
|
|
"name": library.name,
|
|
|
|
"description": library.description,
|
|
|
|
"creation_date": library.creation_date.isoformat().split("+")[0] + "Z",
|
2018-09-22 12:29:30 +00:00
|
|
|
"uploads_count": library.uploads_count,
|
2018-09-06 18:35:02 +00:00
|
|
|
"privacy_level": library.privacy_level,
|
|
|
|
"follow": None,
|
2018-09-24 18:44:22 +00:00
|
|
|
"latest_scan": None,
|
2018-09-06 18:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serializer = api_serializers.LibrarySerializer(library)
|
|
|
|
|
|
|
|
assert serializer.data == expected
|
|
|
|
|
|
|
|
|
2018-09-24 18:44:22 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-09-06 18:35:02 +00:00
|
|
|
def test_library_serializer_with_follow(factories):
|
2018-09-22 12:29:30 +00:00
|
|
|
library = factories["music.Library"](uploads_count=5678)
|
2018-09-06 18:35:02 +00:00
|
|
|
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": library.creation_date.isoformat().split("+")[0] + "Z",
|
2018-09-22 12:29:30 +00:00
|
|
|
"uploads_count": library.uploads_count,
|
2018-09-06 18:35:02 +00:00
|
|
|
"privacy_level": library.privacy_level,
|
|
|
|
"follow": api_serializers.NestedLibraryFollowSerializer(follow).data,
|
2018-09-24 18:44:22 +00:00
|
|
|
"latest_scan": None,
|
2018-09-06 18:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serializer = api_serializers.LibrarySerializer(library)
|
|
|
|
|
|
|
|
assert serializer.data == expected
|
|
|
|
|
|
|
|
|
2018-09-24 18:44:22 +00:00
|
|
|
def test_library_follow_serializer_validates_existing_follow(factories):
|
2018-09-06 18:35:02 +00:00
|
|
|
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
|
2018-09-13 15:18:23 +00:00
|
|
|
|
|
|
|
|
2018-09-24 18:44:22 +00:00
|
|
|
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) as e:
|
|
|
|
serializer.validate_target(library)
|
|
|
|
assert "own library" in str(e)
|
|
|
|
|
|
|
|
|
2018-09-22 12:29:30 +00:00
|
|
|
def test_manage_upload_action_read(factories):
|
2018-09-13 15:18:23 +00:00
|
|
|
ii = factories["federation.InboxItem"]()
|
|
|
|
s = api_serializers.InboxItemActionSerializer(queryset=None)
|
|
|
|
|
|
|
|
s.handle_read(ii.__class__.objects.all())
|
|
|
|
|
|
|
|
assert ii.__class__.objects.filter(is_read=False).count() == 0
|