Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
funkwhale
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Creak
funkwhale
Commits
dfaff270
Verified
Commit
dfaff270
authored
5 years ago
by
Eliot Berriot
Browse files
Options
Downloads
Patches
Plain Diff
See
#170
: apply proper special chars and username blacklist to channel names
parent
581c531f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/funkwhale_api/audio/serializers.py
+9
-1
9 additions, 1 deletion
api/funkwhale_api/audio/serializers.py
api/tests/audio/test_serializers.py
+43
-2
43 additions, 2 deletions
api/tests/audio/test_serializers.py
with
52 additions
and
3 deletions
api/funkwhale_api/audio/serializers.py
+
9
−
1
View file @
dfaff270
from
django.conf
import
settings
from
django.db
import
transaction
from
django.db
import
transaction
from
rest_framework
import
serializers
from
rest_framework
import
serializers
...
@@ -13,6 +14,7 @@ from funkwhale_api.music import models as music_models
...
@@ -13,6 +14,7 @@ from funkwhale_api.music import models as music_models
from
funkwhale_api.music
import
serializers
as
music_serializers
from
funkwhale_api.music
import
serializers
as
music_serializers
from
funkwhale_api.tags
import
models
as
tags_models
from
funkwhale_api.tags
import
models
as
tags_models
from
funkwhale_api.tags
import
serializers
as
tags_serializers
from
funkwhale_api.tags
import
serializers
as
tags_serializers
from
funkwhale_api.users
import
serializers
as
users_serializers
from
.
import
categories
from
.
import
categories
from
.
import
models
from
.
import
models
...
@@ -52,7 +54,10 @@ class ChannelMetadataSerializer(serializers.Serializer):
...
@@ -52,7 +54,10 @@ class ChannelMetadataSerializer(serializers.Serializer):
class
ChannelCreateSerializer
(
serializers
.
Serializer
):
class
ChannelCreateSerializer
(
serializers
.
Serializer
):
name
=
serializers
.
CharField
(
max_length
=
music_models
.
MAX_LENGTHS
[
"
ARTIST_NAME
"
])
name
=
serializers
.
CharField
(
max_length
=
music_models
.
MAX_LENGTHS
[
"
ARTIST_NAME
"
])
username
=
serializers
.
CharField
(
max_length
=
music_models
.
MAX_LENGTHS
[
"
ARTIST_NAME
"
])
username
=
serializers
.
CharField
(
max_length
=
music_models
.
MAX_LENGTHS
[
"
ARTIST_NAME
"
],
validators
=
[
users_serializers
.
ASCIIUsernameValidator
()],
)
description
=
common_serializers
.
ContentSerializer
(
allow_null
=
True
)
description
=
common_serializers
.
ContentSerializer
(
allow_null
=
True
)
tags
=
tags_serializers
.
TagsListField
()
tags
=
tags_serializers
.
TagsListField
()
content_category
=
serializers
.
ChoiceField
(
content_category
=
serializers
.
ChoiceField
(
...
@@ -76,6 +81,9 @@ class ChannelCreateSerializer(serializers.Serializer):
...
@@ -76,6 +81,9 @@ class ChannelCreateSerializer(serializers.Serializer):
return
validated_data
return
validated_data
def
validate_username
(
self
,
value
):
def
validate_username
(
self
,
value
):
if
value
.
lower
()
in
[
n
.
lower
()
for
n
in
settings
.
ACCOUNT_USERNAME_BLACKLIST
]:
raise
serializers
.
ValidationError
(
"
This username is already taken
"
)
matching
=
federation_models
.
Actor
.
objects
.
local
().
filter
(
matching
=
federation_models
.
Actor
.
objects
.
local
().
filter
(
preferred_username__iexact
=
value
preferred_username__iexact
=
value
)
)
...
...
This diff is collapsed.
Click to expand it.
api/tests/audio/test_serializers.py
+
43
−
2
View file @
dfaff270
...
@@ -70,7 +70,7 @@ def test_channel_serializer_create_honor_max_channels_setting(factories, prefere
...
@@ -70,7 +70,7 @@ def test_channel_serializer_create_honor_max_channels_setting(factories, prefere
assert
serializer
.
is_valid
(
raise_exception
=
True
)
assert
serializer
.
is_valid
(
raise_exception
=
True
)
def
test_channel_serializer_create_validates_username
(
factories
):
def
test_channel_serializer_create_validates_username
_uniqueness
(
factories
):
attributed_to
=
factories
[
"
federation.Actor
"
](
local
=
True
)
attributed_to
=
factories
[
"
federation.Actor
"
](
local
=
True
)
data
=
{
data
=
{
"
name
"
:
"
My channel
"
,
"
name
"
:
"
My channel
"
,
...
@@ -83,7 +83,48 @@ def test_channel_serializer_create_validates_username(factories):
...
@@ -83,7 +83,48 @@ def test_channel_serializer_create_validates_username(factories):
serializer
=
serializers
.
ChannelCreateSerializer
(
serializer
=
serializers
.
ChannelCreateSerializer
(
data
=
data
,
context
=
{
"
actor
"
:
attributed_to
}
data
=
data
,
context
=
{
"
actor
"
:
attributed_to
}
)
)
with
pytest
.
raises
(
serializers
.
serializers
.
ValidationError
,
match
=
r
"
.*username.*
"
):
with
pytest
.
raises
(
serializers
.
serializers
.
ValidationError
,
match
=
r
"
.*username is already taken.*
"
):
assert
serializer
.
is_valid
(
raise_exception
=
True
)
def
test_channel_serializer_create_validates_username_chars
(
factories
):
attributed_to
=
factories
[
"
federation.Actor
"
](
local
=
True
)
data
=
{
"
name
"
:
"
My channel
"
,
"
username
"
:
"
hello world
"
,
"
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
"
.*Enter a valid username.*
"
):
assert
serializer
.
is_valid
(
raise_exception
=
True
)
def
test_channel_serializer_create_validates_blacklisted_username
(
factories
,
settings
):
settings
.
ACCOUNT_USERNAME_BLACKLIST
=
[
"
forBidden
"
]
attributed_to
=
factories
[
"
federation.Actor
"
](
local
=
True
)
data
=
{
"
name
"
:
"
My channel
"
,
"
username
"
:
"
FORBIDDEN
"
,
"
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 is already taken.*
"
):
assert
serializer
.
is_valid
(
raise_exception
=
True
)
assert
serializer
.
is_valid
(
raise_exception
=
True
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment