-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove @hsr.ch mail support and relicts
See #481
- Loading branch information
1 parent
1b04ee7
commit b364da8
Showing
8 changed files
with
57 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,7 @@ | |
from django.contrib.auth import get_user_model | ||
from registration.forms import RegistrationForm | ||
|
||
USERNAME_REGEXES = { | ||
"hsr.ch": re.compile(r"^[a-zA-Z0-9-_]+$"), | ||
"ost.ch": re.compile(r"^[a-zA-Z0-9-_.]+$"), | ||
} | ||
USERNAME_REGEX = re.compile(r"^[a-zA-Z0-9-_.]+$") | ||
|
||
|
||
class ProfileForm(forms.ModelForm): | ||
|
@@ -36,11 +33,11 @@ class HsrRegistrationForm(RegistrationForm): | |
def clean_email(self): | ||
email = self.cleaned_data["email"] | ||
|
||
# Only allow HSR/OST e-mails | ||
# Only allow OST e-mails | ||
email_domain = email.split("@")[1] | ||
if email_domain not in USERNAME_REGEXES: | ||
if email_domain != "ost.ch": | ||
raise forms.ValidationError( | ||
"Registrierung ist Studierenden mit einer @ost.ch oder @hsr.ch-Mailadresse vorbehalten." | ||
"Registrierung ist Studierenden mit einer @ost.ch-Mailadresse vorbehalten." | ||
) | ||
|
||
User = get_user_model() | ||
|
@@ -51,17 +48,13 @@ def clean_email(self): | |
|
||
# Extract username from e-mail | ||
email_user = email.split("@")[0] | ||
username_re = USERNAME_REGEXES[email_domain] | ||
|
||
# Ensure that the username part is valid | ||
if not username_re.match(email_user): | ||
msg = "Ungültige E-Mail, Benutzername darf nur a-z, A-Z, 0-9, - und _ enthalten." | ||
if "." in email_user and email_domain == "hsr.ch": | ||
msg += " Nutze bitte [email protected] statt [email protected]." | ||
|
||
if not USERNAME_REGEX.match(email_user): | ||
msg = "Ungültige E-Mail, Benutzername darf nur a-z, A-Z, 0-9, -, _ und . enthalten." | ||
raise forms.ValidationError(msg) | ||
|
||
if email_domain == "ost.ch" and "." not in email_user: | ||
if "." not in email_user: | ||
msg = "Ungültige E-Mail, Benutzername sollte . enthalten." | ||
raise forms.ValidationError(msg) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,70 +65,53 @@ def test_registration(client): | |
response = client.post( | ||
registration_url, | ||
{ | ||
"email": "testuser@hsr.ch", | ||
"email": "test.user@ost.ch", | ||
"password1": "testpass", | ||
"password2": "testpass", | ||
}, | ||
) | ||
assertRedirects(response, "/accounts/register/complete/") | ||
assert User.objects.filter(username="testuser").exists() | ||
assert User.objects.filter(username="test.user").exists() | ||
|
||
transaction.commit() | ||
assert len(mail.outbox) == 1 | ||
assert mail.outbox[0].subject == "[studentenportal.ch] Aktivierung" | ||
|
||
|
||
class RegistrationViewTest(TestCase): | ||
class RegistrationViewTest: | ||
|
||
registration_url = "/accounts/register/" | ||
|
||
def testRegistrationPage(self): | ||
response = self.client.get(self.registration_url) | ||
response = client.get(self.registration_url) | ||
self.assertContains(response, "<h1>Registrieren</h1>") | ||
self.assertContains( | ||
response, | ||
"Diese Registrierung ist Studenten mit einer HSR- oder OST-Email-Adresse", | ||
"Diese Registrierung ist Studenten mit einer OST-Email-Adresse", | ||
) | ||
self.assertContains(response, "<form") | ||
|
||
def testRegistrationBadUsername(self): | ||
def testRegistrationBadUsername(self, client): | ||
""" | ||
Test that a registration with a bad username returns an error. | ||
""" | ||
response = self.client.post( | ||
self.registration_url, | ||
{ | ||
"email": "[email protected]", | ||
"password1": "testpass", | ||
"password2": "testpass", | ||
}, | ||
) | ||
assert response.status_code == 200 | ||
content = response.content.decode("utf8") | ||
assert "Ungültige E-Mail" in content | ||
assert "Nutze bitte" not in content | ||
|
||
def testRegistrationLongUsernameHsr(self): | ||
""" | ||
Test that a registration with a long username returns an error. | ||
""" | ||
response = self.client.post( | ||
response = client.post( | ||
self.registration_url, | ||
{ | ||
"email": "foo.bar@hsr.ch", | ||
"email": "a.+++@ost.ch", | ||
"password1": "testpass", | ||
"password2": "testpass", | ||
}, | ||
) | ||
assert response.status_code == 200 | ||
content = response.content.decode("utf8") | ||
assert "Ungültige E-Mail" in content | ||
assert "Nutze bitte" in content | ||
|
||
def testRegistrationLongUsernameOst(self): | ||
def testRegistrationLongUsernameOst(self, client): | ||
""" | ||
Test that a registration with a long OST username works. | ||
""" | ||
response = self.client.post( | ||
response = client.post( | ||
self.registration_url, | ||
{ | ||
"email": "[email protected]", | ||
|
@@ -138,11 +121,11 @@ def testRegistrationLongUsernameOst(self): | |
) | ||
assert response.status_code == 302 | ||
|
||
def testRegistrationShortUsernameOst(self): | ||
def testRegistrationShortUsernameOst(self, client): | ||
""" | ||
Test that a registration with a short OST username fails. | ||
""" | ||
response = self.client.post( | ||
response = client.post( | ||
self.registration_url, | ||
{ | ||
"email": "[email protected]", | ||
|
@@ -154,51 +137,52 @@ def testRegistrationShortUsernameOst(self): | |
content = response.content.decode("utf8") | ||
assert "Ungültige E-Mail" in content | ||
|
||
def testRegistrationBadDomain(self): | ||
@pytest.mark.parametrize("domain", ["zhaw.ch", "hsr.ch"]) | ||
def testRegistrationBadDomain(self, client, domain): | ||
""" | ||
Test that a registration with a non-hsr.ch Domain return an error. | ||
Test that a registration with a non-ost.ch Domain return an error. | ||
""" | ||
response = self.client.post( | ||
response = client.post( | ||
self.registration_url, | ||
{ | ||
"email": "[email protected]", | ||
"email": f"a.meier@{domain}", | ||
"password1": "testpass", | ||
"password2": "testpass", | ||
}, | ||
) | ||
assert response.status_code == 200 | ||
assert ( | ||
"Registrierung ist Studierenden mit einer @ost.ch oder @hsr.ch-Mailadresse vorbehalten" | ||
"Registrierung ist Studierenden mit einer @ost.ch-Mailadresse vorbehalten" | ||
in response.content.decode("utf8") | ||
) | ||
|
||
def testRegistrationDoubleUsername(self): | ||
def testRegistrationDoubleUsername(self, client): | ||
""" | ||
Test that a registration with a bad username returns an error. | ||
Test that a registration with a duplicate username returns an error. | ||
""" | ||
baker.make(User, username="a", email="abc@hsr.ch") | ||
response = self.client.post( | ||
baker.make(User, username="a.b", email="a.b@ost.ch") | ||
response = client.post( | ||
self.registration_url, | ||
{ | ||
"email": "a@hsr.ch", | ||
"email": "a.b@ost.ch", | ||
"password1": "testpass", | ||
"password2": "testpass", | ||
}, | ||
) | ||
assert response.status_code == 200 | ||
assert "Benutzer "a" existiert bereits" in response.content.decode( | ||
assert "Benutzer "a.b" existiert bereits" in response.content.decode( | ||
"utf8" | ||
) | ||
|
||
def testRegistrationDoubleEmail(self): | ||
def testRegistrationDoubleEmail(self, client): | ||
""" | ||
Test that a registration with a bad username returns an error. | ||
Test that a registration with a duplicate email returns an error. | ||
""" | ||
baker.make(User, username="abc", email="a@hsr.ch") | ||
response = self.client.post( | ||
baker.make(User, username="abc", email="a.b.c@ost.ch") | ||
response = client.post( | ||
self.registration_url, | ||
{ | ||
"email": "a@hsr.ch", | ||
"email": "a.b.c@ost.ch", | ||
"password1": "testpass", | ||
"password2": "testpass", | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters