Skip to content

Commit

Permalink
Prevent submitting registration request of already existing email/nic…
Browse files Browse the repository at this point in the history
…k (refs #134)
  • Loading branch information
Almad committed Aug 18, 2024
1 parent ce0cdf5 commit 44440ff
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
25 changes: 24 additions & 1 deletion ddcz/forms/signup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.forms import ModelForm
from django.utils.safestring import mark_safe

from ..models import AwaitingRegistration
from ..models import AwaitingRegistration, UserProfile


class SignUpForm(ModelForm):
Expand Down Expand Up @@ -187,8 +187,31 @@ def clean_nick(self, *args, **kwargs):
+ ", ".join(bad_characters)
+ ")."
)

if len(nick) < 3:
raise forms.ValidationError("Nick musí mít alespoň 3 znaky.")

if UserProfile.objects.filter(nick=nick).exists():
raise forms.ValidationError("Tento nick je již obsazený.")
elif AwaitingRegistration.objects.filter(nick=nick).exists():
raise forms.ValidationError(
"Žádost o registraci již byla odeslána, buď prosím trpělivý. Máš-li pocit že to je příliš dlouho, připomeň se prosím na Discordu."
)

return nick

def clean_email(self, *args, **kwargs):
email = self.cleaned_data.get("email")
if UserProfile.objects.filter(email=email).exists():
raise forms.ValidationError(
"S tímto emailem jsi se již registroval, zkusi si prosím restovat heslo."
)
elif AwaitingRegistration.objects.filter(email=email).exists():
raise forms.ValidationError(
"Žádost o registraci již byla odeslána, buď prosím trpělivý. Máš-li pocit že to je příliš dlouho, připomeň se prosím na Discordu."
)
return email

def clean_age(self, *args, **kwargs):
age = self.cleaned_data.get("age")
if age < self.MIN_AGE:
Expand Down
46 changes: 46 additions & 0 deletions ddcz/tests/test_integration/test_signup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from ddcz.forms.signup import SignUpForm
from ddcz.models import UserProfile

from django.test import TestCase


class TestSignupForm(TestCase):
def test_duplicate_nick_registrations(self):
test_nick = "testnick"
UserProfile.objects.create(nick=test_nick)

form = SignUpForm(
{
"nick": test_nick,
"email": "[email protected]",
"name_given": "John",
"name_family": "Doe",
"salutation": "Dr(ak).",
"password": "password",
"password_confirm": "password",
"gdpr": "T",
"gender": "F",
"age": 20,
}
)
self.assertFalse(form.is_valid())

def test_duplicate_email_registrations(self):
test_email = "[email protected]"
UserProfile.objects.create(nick="testnick", email=test_email)

form = SignUpForm(
{
"nick": "whatever",
"email": test_email,
"name_given": "John",
"name_family": "Doe",
"salutation": "Dr(ak).",
"password": "password",
"password_confirm": "password",
"gdpr": "T",
"gender": "F",
"age": 20,
}
)
self.assertFalse(form.is_valid())

0 comments on commit 44440ff

Please sign in to comment.