From ba8ba6f0699905c0ef5e27d8d8eb67268afe68cf Mon Sep 17 00:00:00 2001 From: Patrick Reiffenstein Date: Tue, 11 Jun 2024 19:08:04 +0200 Subject: [PATCH] Changed username check to be case insensitive (#468) * Changed username check to be case insensitive Fixes #466. * Username case sensitivity test added --- stregsystem/admin.py | 4 ++-- stregsystem/tests.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/stregsystem/admin.py b/stregsystem/admin.py index 74a5ab7d..80f4f4dd 100644 --- a/stregsystem/admin.py +++ b/stregsystem/admin.py @@ -202,7 +202,7 @@ class Meta: def clean_username(self): username = self.cleaned_data['username'] if self.instance is None or self.instance.pk is None: - if Member.objects.filter(username=username).exists(): + if Member.objects.filter(username__iexact=username).exists(): raise forms.ValidationError("Brugernavnet er allerede taget") return username @@ -234,7 +234,7 @@ class MemberAdmin(admin.ModelAdmin): def save_model(self, request, obj, form, change): if 'username' in form.changed_data and change: - if Member.objects.filter(username=obj.username).exclude(pk=obj.pk).exists(): + if Member.objects.filter(username__iexact=obj.username).exclude(pk=obj.pk).exists(): messages.add_message(request, messages.WARNING, 'Det brugernavn var allerede optaget') super().save_model(request, obj, form, change) diff --git a/stregsystem/tests.py b/stregsystem/tests.py index b2db8a45..fd64bcb5 100644 --- a/stregsystem/tests.py +++ b/stregsystem/tests.py @@ -964,6 +964,11 @@ def test_cant_create_duplicate_username(self): form = MemberForm(model_to_dict(jeff)) self.assertFalse(form.is_valid()) + def test_cant_create_duplicate_username_in_other_case(self): + jeff = Member(username="JeFf", firstname="jeffrey", lastname="jefferson", gender="M") + form = MemberForm(model_to_dict(jeff)) + self.assertFalse(form.is_valid()) + def test_can_create_non_duplicate_username(self): not_jeff = Member(username="not_jeff", firstname="jeff", lastname="jefferson", gender="M") form = MemberForm(model_to_dict(not_jeff))