See #170: proper error handling for username uniqueness in channels

merge-requests/1042/head
Eliot Berriot 2020-02-14 11:56:53 +01:00
rodzic a3505d2099
commit 581c531fca
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6B501DFD73514E14
2 zmienionych plików z 26 dodań i 1 usunięć

Wyświetl plik

@ -6,6 +6,7 @@ from funkwhale_api.common import serializers as common_serializers
from funkwhale_api.common import utils as common_utils
from funkwhale_api.common import locales
from funkwhale_api.common import preferences
from funkwhale_api.federation import models as federation_models
from funkwhale_api.federation import serializers as federation_serializers
from funkwhale_api.federation import utils as federation_utils
from funkwhale_api.music import models as music_models
@ -74,6 +75,14 @@ class ChannelCreateSerializer(serializers.Serializer):
validated_data["metadata"] = metadata
return validated_data
def validate_username(self, value):
matching = federation_models.Actor.objects.local().filter(
preferred_username__iexact=value
)
if matching.exists():
raise serializers.ValidationError("This username is already taken")
return value
@transaction.atomic
def create(self, validated_data):
from . import views

Wyświetl plik

@ -56,7 +56,6 @@ def test_channel_serializer_create_honor_max_channels_setting(factories, prefere
attributed_to = factories["federation.Actor"](local=True)
factories["audio.Channel"](attributed_to=attributed_to)
data = {
# TODO: cover
"name": "My channel",
"username": "mychannel",
"description": {"text": "This is my channel", "content_type": "text/markdown"},
@ -71,6 +70,23 @@ def test_channel_serializer_create_honor_max_channels_setting(factories, prefere
assert serializer.is_valid(raise_exception=True)
def test_channel_serializer_create_validates_username(factories):
attributed_to = factories["federation.Actor"](local=True)
data = {
"name": "My channel",
"username": attributed_to.preferred_username.upper(),
"description": {"text": "This is my channel", "content_type": "text/markdown"},
"tags": ["hello", "world"],
"content_category": "other",
}
serializer = serializers.ChannelCreateSerializer(
data=data, context={"actor": attributed_to}
)
with pytest.raises(serializers.serializers.ValidationError, match=r".*username.*"):
assert serializer.is_valid(raise_exception=True)
def test_channel_serializer_create_podcast(factories):
attributed_to = factories["federation.Actor"](local=True)