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

Fixed duplicate through email cleaning #1956 #1979

Merged
merged 4 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions evap/staff/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
TextAnswer,
UserProfile,
)
from evap.evaluation.tools import date_to_datetime
from evap.evaluation.tools import clean_email, date_to_datetime
from evap.results.tools import STATES_WITH_RESULT_TEMPLATE_CACHING, STATES_WITH_RESULTS_CACHING, cache_results
from evap.results.views import update_template_cache, update_template_cache_of_published_evaluations_in_course
from evap.staff.tools import remove_user_from_represented_and_ccing_users
Expand Down Expand Up @@ -987,7 +987,7 @@ def clean_evaluations_participating_in(self):
return evaluations_participating_in

def clean_email(self):
email = self.cleaned_data.get("email")
email = clean_email(self.cleaned_data.get("email"))
if email is None:
return None

Expand All @@ -999,7 +999,7 @@ def clean_email(self):

if user_with_same_email.exists():
raise forms.ValidationError(_("A user with the email '%s' already exists") % email)
return email.lower()
return email

def save(self, *args, **kw):
super().save(*args, **kw)
Expand Down
12 changes: 11 additions & 1 deletion evap/staff/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import patch

from django.forms.models import inlineformset_factory
from django.test import TestCase
from django.test import TestCase, override_settings
from model_bakery import baker

from evap.contributor.forms import EvaluationForm as ContributorEvaluationForm
Expand Down Expand Up @@ -121,6 +121,10 @@ def test_evaluation_email_form(self):


class UserFormTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.existing_user = baker.make(UserProfile, email="existing@example.com")

def test_user_form(self):
"""
Tests the UserForm with one valid and one invalid input dataset.
Expand All @@ -135,6 +139,7 @@ def test_user_form(self):
form = UserForm(instance=user, data=data)
self.assertFalse(form.is_valid())

@override_settings(INSTITUTION_EMAIL_REPLACEMENTS=[("institution.example.com", "example.com")])
def test_user_with_same_email(self):
"""
Tests whether the user form correctly handles email adresses
Expand All @@ -155,6 +160,11 @@ def test_user_with_same_email(self):
form = UserForm(instance=user, data=data)
self.assertTrue(form.is_valid())

data = {"email": "existing@institution.example.com"}
form = UserForm(instance=user, data=data)
self.assertFalse(form.is_valid())
self.assertIn("A user with the email 'existing@example.com' already exists", form.errors["email"])

def test_user_cannot_be_removed_from_evaluation_already_voted_for(self):
student = baker.make(UserProfile)
baker.make(Evaluation, participants=[student], voters=[student], course__semester__is_active=True)
Expand Down