Skip to content
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

Update username regex pattern in routes #2566

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions onadata/apps/main/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
"""API Django admin amendments."""
# pylint: disable=imported-auth-user
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError


class BaseCustomUserForm:
"""
Base form class for custom user forms.
Contains common logic for validating the username.
"""

def clean_username(self):
"""
Clean the username field to ensure it does not contain hyphens.
Raises:
ValidationError: If the username contains hyphens.
Returns:
str: The cleaned username.
"""
username = self.cleaned_data["username"]
if "-" in username:
raise ValidationError("Usernames cannot contain hyphens.")
return username


class CustomUserCreationForm(BaseCustomUserForm, UserCreationForm):
"""
Custom form for user creation.
Inherits from BaseCustomUserForm and UserCreationForm.
"""

class Meta(UserCreationForm.Meta):
model = User


class CustomUserChangeForm(BaseCustomUserForm, UserChangeForm):
"""
Custom form for user change.
Inherits from BaseCustomUserForm and UserChangeForm.
"""

class Meta(UserChangeForm.Meta):
model = User


class CustomUserAdmin(UserAdmin):
"""
Custom User admin panel configuration.
"""

add_form = CustomUserCreationForm
form = CustomUserChangeForm


admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
27 changes: 27 additions & 0 deletions onadata/apps/main/tests/test_user_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from onadata.apps.main.admin import CustomUserChangeForm, CustomUserCreationForm
from onadata.apps.main.tests.test_base import TestBase


class TestUserValidation(TestBase):
def test_custom_user_creation_form_invalid_username(self):
# Try to create a user with a hyphenated username
form_data = {
"username": "john-doe",
"password1": "testpassword",
"password2": "testpassword",
}
form = CustomUserCreationForm(data=form_data)
self.assertFalse(form.is_valid())
self.assertIn("username", form.errors)
errors = form.errors.get("username")[0]
self.assertEqual(str(errors), "Usernames cannot contain hyphens.")

def test_custom_user_change_form_invalid_username(self):
# Try to change a user's username to one with a hyphen
user = self._create_user("bob-user", "bob")
form_data = {"username": "bob-user-1"}
form = CustomUserChangeForm(data=form_data, instance=user)
self.assertFalse(form.is_valid())
self.assertIn("username", form.errors)
errors = form.errors.get("username")[0]
self.assertEqual(str(errors), "Usernames cannot contain hyphens.")
Loading