-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[WEB-4434]fix: user name validation #7617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
33d3e03
fde6de3
43cdaec
4e4e3ef
9ef0de4
03bbe79
56795bb
04e14f3
00e38a7
b4acaa2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import pytest | ||
| from rest_framework import serializers | ||
|
|
||
| from plane.app.serializers.user import UserSerializer | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| class TestUserSerializer: | ||
| """Test the UserSerializer""" | ||
|
|
||
| def test_validate_first_name_valid(self): | ||
| """Test that valid first names are accepted""" | ||
|
|
||
| serializer = UserSerializer() | ||
| valid_names = [ | ||
| "John", | ||
| "John Doe", | ||
| "John-Doe", | ||
| "John_Doe", | ||
| "John123", | ||
| ] | ||
|
|
||
| for name in valid_names: | ||
| result = serializer.validate_first_name(name) | ||
|
|
||
| assert result == name | ||
|
|
||
| def test_validate_first_name_with_url(self): | ||
| """Test that first names containing URLs are rejected""" | ||
|
|
||
| serializer = UserSerializer() | ||
| invalid_names = [ | ||
| "http://example.com", | ||
| "John https://test.com", | ||
| "www.test.com", | ||
| ] | ||
|
|
||
| for name in invalid_names: | ||
| with pytest.raises(serializers.ValidationError) as exc_info: | ||
| serializer.validate_first_name(name) | ||
|
|
||
| assert str(exc_info.value.detail[0]) == "First name cannot contain a URL." | ||
|
|
||
| def test_validate_first_name_with_special_chars(self): | ||
| """Test that first names with special characters are rejected""" | ||
|
|
||
| serializer = UserSerializer() | ||
| invalid_names = [ | ||
| "John@Doe", | ||
| "John#Doe", | ||
| "John$Doe", | ||
| "John!Doe", | ||
| "John&Doe", | ||
| ] | ||
|
|
||
| for name in invalid_names: | ||
| with pytest.raises(serializers.ValidationError) as exc_info: | ||
| serializer.validate_first_name(name) | ||
|
|
||
| assert str(exc_info.value.detail[0]) == ( | ||
| "first name can only contain letters, numbers, " | ||
| "hyphens (-), and underscores (_)" | ||
| ) | ||
|
|
||
| def test_validate_last_name_valid(self): | ||
| """Test that valid last names are accepted""" | ||
|
|
||
| serializer = UserSerializer() | ||
| valid_names = [ | ||
| "Smith", | ||
| "Smith Jr", | ||
| "Smith-Jr", | ||
| "Smith_Jr", | ||
| "Smith123", | ||
| "", | ||
| ] | ||
|
|
||
| for name in valid_names: | ||
| result = serializer.validate_last_name(name) | ||
|
|
||
| assert result == name | ||
|
|
||
sangeethailango marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def test_validate_last_name_with_url(self): | ||
| """Test that last names containing URLs are rejected""" | ||
|
|
||
| serializer = UserSerializer() | ||
| invalid_names = [ | ||
| "http://example.com", | ||
| "Smith https://test.com", | ||
| "www.test.com", | ||
| ] | ||
|
|
||
| for name in invalid_names: | ||
| with pytest.raises(serializers.ValidationError) as exc_info: | ||
| serializer.validate_last_name(name) | ||
|
|
||
| assert str(exc_info.value.detail[0]) == "Last name cannot contain a URL." | ||
|
|
||
| def test_validate_last_name_with_special_chars(self): | ||
| """Test that last names with special characters are rejected""" | ||
|
|
||
| serializer = UserSerializer() | ||
| invalid_names = [ | ||
| "Smith@Jr", | ||
| "Smith#Jr", | ||
| "Smith$Jr", | ||
| "Smith!Jr", | ||
| "Smith&Jr", | ||
| ] | ||
|
|
||
| for name in invalid_names: | ||
| with pytest.raises(serializers.ValidationError) as exc_info: | ||
| serializer.validate_last_name(name) | ||
|
|
||
| assert str(exc_info.value.detail[0]) == ( | ||
| "last name can only contain letters, numbers, " | ||
| "hyphens (-), and underscores (_)" | ||
| ) | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -213,6 +213,11 @@ export const ProfileSetupStep: FC<Props> = observer(({ handleStepChange }) => { | |||||||||||||||||||||||||||
| value: 24, | ||||||||||||||||||||||||||||
| message: "Name must be within 24 characters.", | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| validate: (value) => { | ||||||||||||||||||||||||||||
| if (!/^[a-zA-Z0-9 _-]+$/.test(value)) | ||||||||||||||||||||||||||||
| return "First name can only contain letters, numbers, hyphens, and underscores."; | ||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
|
Comment on lines
+216
to
+220
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainAlign rule with copy and block whitespace-only names Message says “First name” though label is “Name”, and you allow spaces but don’t mention them; also accepts only-spaces. Trim and update message. - validate: (value) => {
- if (!/^[a-zA-Z0-9 _-]+$/.test(value))
- return "First name can only contain letters, numbers, hyphens, and underscores.";
- return true;
- },
+ validate: (value) => {
+ const v = value.trim();
+ if (v.length === 0) return "Name can't be empty or only spaces.";
+ if (!/^[A-Za-z0-9 _-]+$/.test(v))
+ return "Name can only contain letters, numbers, spaces, hyphens (-), and underscores (_).";
+ return true;
+ },If the PR removes last name in onboarding, also delete it from:
🏁 Script executed: #!/bin/bash
# Find remaining onboarding references to last_name
rg -nP -C2 '\blast_name\b' apps/web/core/components/onboardingLength of output: 8338 Update name validation to trim whitespace and align message copy apps/web/core/components/onboarding/steps/profile/root.tsx (lines 216–220): - validate: (value) => {
- if (!/^[a-zA-Z0-9 _-]+$/.test(value))
- return "First name can only contain letters, numbers, hyphens, and underscores.";
- return true;
- },
+ validate: (value) => {
+ const trimmed = value.trim();
+ if (trimmed.length === 0) return "Name can't be empty or only spaces.";
+ if (!/^[A-Za-z0-9 _-]+$/.test(trimmed))
+ return "Name can only contain letters, numbers, spaces, hyphens (-), and underscores (_).";
+ return true;
+ },📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||
| render={({ field: { value, onChange, ref } }) => ( | ||||||||||||||||||||||||||||
| <input | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.