Skip to content

Commit

Permalink
Remove @hsr.ch mail support and relicts
Browse files Browse the repository at this point in the history
See #481
  • Loading branch information
The-Compiler committed Aug 25, 2022
1 parent 1b04ee7 commit b364da8
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 94 deletions.
4 changes: 2 additions & 2 deletions apps/events/fixtures/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"pk": 2,
"model": "events.event",
"fields": {
"description": "Der erste Tag der neuen.HSR Studenten.\r\n\r\nSiehe http://hsr.ch/",
"description": "Der erste Tag der neuen OST Studenten.\r\n\r\nSiehe https://ost.ch/",
"end_date": null,
"author": 2,
"start_time": "10:00:00",
Expand All @@ -38,4 +38,4 @@
"start_date": "2012-09-25"
}
}
]
]
21 changes: 7 additions & 14 deletions apps/front/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions apps/front/templates/registration/registration_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<h1>Registrieren</h1>
</header>

<p>Diese Registrierung ist Studenten mit einer HSR- oder OST-Email-Adresse
(<code>username@hsr.ch</code>) vorbehalten.</p>
<p>Diese Registrierung ist Studenten mit einer OST-Email-Adresse
(<code>vorname.nachname@ost.ch</code>) vorbehalten.</p>

{% if form.non_field_errors %}
<div class="alert-error">
Expand Down
2 changes: 1 addition & 1 deletion apps/lecturers/baker_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
first_name="David",
last_name="Krakaduku",
abbreviation="KRA",
email="krakaduku@hsr.ch",
email="kraka.duku@ost.ch",
office="1.337",
subjects="Quantenphysik, Mathematik für Mathematiker",
)
4 changes: 2 additions & 2 deletions tests/events/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def setUp(self):
start_time=datetime.time(hour=19, minute=30),
end_time=datetime.time(hour=23, minute=59),
location="Gebäude 13",
url="http://hsr.ch/",
url="https://ost.ch/",
)

def tearDown(self):
Expand All @@ -112,7 +112,7 @@ def testContent(self):
self.assertContains(response, "<strong>Ort:</strong>")
self.assertContains(response, "Gebäude 13")
self.assertContains(response, "<strong>Website:</strong>")
self.assertContains(response, "http://hsr.ch/")
self.assertContains(response, "https://ost.ch/")


@pytest.mark.django_db(transaction=True)
Expand Down
38 changes: 12 additions & 26 deletions tests/front/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,15 @@
from apps.front import forms


class TestUsernameRegex:
@pytest.mark.parametrize(
"username, valid",
[
("mmueller", True),
("m_mueller", True),
("m-mueller", True),
("m2mueller", True),
("a+++", False),
],
)
@pytest.mark.parametrize("domain", ["hsr.ch", "ost.ch"])
def test_common(self, domain, username, valid):
username_re = forms.USERNAME_REGEXES[domain]
assert bool(username_re.match(username)) == valid

@pytest.mark.parametrize(
"domain, dots_allowed",
[
("hsr.ch", False),
("ost.ch", True),
],
)
def test_dots(self, domain, dots_allowed):
username_re = forms.USERNAME_REGEXES[domain]
assert bool(username_re.match("melanie.mueller")) == dots_allowed
@pytest.mark.parametrize(
"username, valid",
[
("m.mueller", True),
("m.muel_ler", True),
("m.muel-ler", True),
("m.2mueller", True),
("a.+++", False),
],
)
def test_username_regex(username, valid):
assert bool(forms.USERNAME_REGEX.match(username)) == valid
76 changes: 30 additions & 46 deletions tests/front/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand All @@ -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]",
Expand All @@ -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 &quot;a&quot; existiert bereits" in response.content.decode(
assert "Benutzer &quot;a.b&quot; 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",
},
Expand Down
2 changes: 1 addition & 1 deletion tests/lecturers/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def testContact(self):
login(self)
response = self.client.get(self.url)
self.assertContains(response, "1.337")
self.assertContains(response, "krakaduku@hsr.ch")
self.assertContains(response, "kraka.duku@ost.ch")


@pytest.mark.django_db
Expand Down

0 comments on commit b364da8

Please sign in to comment.