Skip to content

Commit

Permalink
[#1672] update logging tests
Browse files Browse the repository at this point in the history
- the format of log messages had changed due to the change of the user account's __str__ method
  • Loading branch information
pi-sigma authored and Bart van der Schoor committed Sep 8, 2023
1 parent e7e8db8 commit e34b559
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 75 deletions.
12 changes: 1 addition & 11 deletions src/open_inwoner/accounts/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,10 @@ class UserModelEmailBackend(ModelBackend):
"""

def authenticate(
self,
request,
login_type=None,
username=None,
password=None,
user=None,
token=None,
**kwargs
self, request, username=None, password=None, user=None, token=None, **kwargs
):
config = SiteConfiguration.get_solo()

if login_type == "digid":
return None

if username and password and not config.login_2fa_sms:
try:
user = get_user_model().objects.get(email__iexact=username)
Expand Down
13 changes: 7 additions & 6 deletions src/open_inwoner/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,24 +333,25 @@ def find_user(
Q(first_name__iexact=first_name) & Q(last_name__iexact=last_name)
)

existing_users = existing_users.filter(is_active=True)

# no active users with the given specs
if not (existing_users := existing_users.filter(is_active=True)):
if not existing_users:
raise ValidationError(
_("The user cannot be added, their account has been deleted.")
)

# multiple users with the given specs
if existing_users.count() > 1:
# check if there are multiple users with the given specs
try:
existing_user = existing_users.get()
except User.MultipleObjectsReturned:
raise ValidationError(
_(
"We're having trouble finding an account with this information."
"Please contact us for help."
)
)

# exactly 1 user
existing_user = existing_users.get()

# contact already exists
if self.user.has_contact(existing_user):
raise ValidationError(
Expand Down
18 changes: 6 additions & 12 deletions src/open_inwoner/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ def __init__(self, *args, **kwargs):
self._old_bsn = self.bsn

def __str__(self):
return f"{self.first_name} {self.last_name} ({self.email})"
return (
f"{self.first_name} {self.last_name} ({self.get_contact_email()})".strip()
)

def clean(self, *args, **kwargs):
"""Reject non-unique emails, except for users with login_type DigiD"""
Expand All @@ -240,7 +242,7 @@ def clean(self, *args, **kwargs):
return

# account has been deactivated
if any(user.bsn == self.bsn and user.is_not_active for user in existing_users):
if any(user.bsn == self.bsn and not user.is_active for user in existing_users):
raise ValidationError(
{"email": ValidationError(_("This account has been deactivated"))}
)
Expand All @@ -253,11 +255,7 @@ def clean(self, *args, **kwargs):

# some account does not have login_type digid
raise ValidationError(
{
"email": ValidationError(
_("The user cannot be added. Please contact us for help.")
)
}
{"email": ValidationError(_("This email is already taken."))}
)

@property
Expand Down Expand Up @@ -356,10 +354,6 @@ def get_contact_type_display(self) -> str:
choice = ContactTypeChoices.get_choice(self.contact_type)
return choice.label

@property
def is_not_active(self):
return not self.is_active

def get_contact_email(self):
return self.email if "@example.org" not in self.email else ""

Expand All @@ -376,7 +370,7 @@ def has_contact(self, user):
"""
:returns: `True` if the subject has `user` as contact, `False` otherwise
"""
return user in self.user_contacts.all()
return self.user_contacts.filter(id=user.id).exists()

def get_plan_contact_new_count(self):
return (
Expand Down
2 changes: 1 addition & 1 deletion src/open_inwoner/accounts/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ class DuplicateEmailRegistrationTest(WebTest):

@classmethod
def setUpTestData(cls):
cls.msg_dupes = _("The user cannot be added. Please contact us for help.")
cls.msg_dupes = _("This email is already taken.")
cls.msg_inactive = _("This account has been deactivated")

#
Expand Down
2 changes: 1 addition & 1 deletion src/open_inwoner/accounts/tests/test_auth_2fa_sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_login_fails_with_sms_failure(self, mock_gateway_send):
"Het versturen van een SMS-bericht aan {phonenumber} is mislukt. Inloggen afgebroken."
).format(phonenumber=self.user.phonenumber),
"action_flag": list(LOG_ACTIONS[4]),
"content_object_repr": self.user.email,
"content_object_repr": f"{self.user.first_name} {self.user.last_name} ({self.user.email})",
},
)

Expand Down
15 changes: 0 additions & 15 deletions src/open_inwoner/accounts/tests/test_contact_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ def test_contact_list_login_required(self):

def test_contact_list_filled(self):
response = self.app.get(self.list_url, user=self.user)
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.contact.first_name)

def test_contact_list_only_show_personal_contacts(self):
other_user = UserFactory()
response = self.app.get(self.list_url, user=other_user)
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, self.contact.first_name)

def test_list_shows_pending_invitations(self):
Expand All @@ -62,14 +60,12 @@ def test_contact_filter(self):
self.user.user_contacts.add(begeleider)

response = self.app.get(self.list_url, user=self.user)
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.contact.first_name)
self.assertContains(response, begeleider.first_name)

form = response.forms["contact-filter"]
form["type"] = ContactTypeChoices.begeleider
response = form.submit()
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, self.contact.first_name)
self.assertContains(response, begeleider.first_name)

Expand All @@ -81,7 +77,6 @@ def test_contact_filter_without_any_contacts(self):
form = response.forms["contact-filter"]
form["type"] = ContactTypeChoices.contact
response = form.submit()
self.assertEqual(response.status_code, 200)
self.assertContains(
response,
_(
Expand Down Expand Up @@ -129,7 +124,6 @@ def test_contact_list_show_reversed(self):
other_contact.user_contacts.add(self.user)

response = self.app.get(self.list_url, user=self.user)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "reverse_contact_user_should_be_found")

def test_contact_create_login_required(self):
Expand All @@ -139,7 +133,6 @@ def test_contact_create_login_required(self):
def test_new_user_contact_not_created_and_invite_sent(self):
contacts_before = list(self.user.user_contacts.all())
response = self.app.get(self.create_url, user=self.user)
self.assertEqual(response.status_code, 200)

form = response.forms["contact-form"]
form["first_name"] = "John"
Expand Down Expand Up @@ -176,8 +169,6 @@ def test_existing_user_contact(self):
response = form.submit(user=self.user)
pending_invitation = self.user.contacts_for_approval.first()

self.assertEqual(response.status_code, 302)

response = response.follow()
self.assertContains(response, existing_user.email)
self.assertNotContains(response, existing_user.first_name)
Expand Down Expand Up @@ -214,7 +205,6 @@ def test_adding_same_contact_fails(self):
)
]
}
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["form"].errors, expected_errors)

def test_adding_inactive_contact_fails(self):
Expand All @@ -232,7 +222,6 @@ def test_adding_inactive_contact_fails(self):

response = form.submit()

self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["form"].errors, expected_errors)

def test_user_cannot_add_themselves(self):
Expand All @@ -247,7 +236,6 @@ def test_user_cannot_add_themselves(self):

response = form.submit()

self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["form"].errors, expected_errors)

def test_adding_contact_with_invalid_first_name_chars_fails(self):
Expand Down Expand Up @@ -321,7 +309,6 @@ def test_digid_contact_with_duplicate_email_success(self, m):
response = form.submit(user=self.digid_user)
pending_invitation = self.digid_user.contacts_for_approval.first()

self.assertEqual(response.status_code, 302)
self.assertContains(response.follow(), existing_user.email)
self.assertEqual(existing_user, pending_invitation)

Expand Down Expand Up @@ -349,7 +336,6 @@ def test_digid_contact_duplicate_email_case_insensitive_success(self, m):
response = form.submit(user=self.digid_user)
pending_invitation = self.digid_user.contacts_for_approval.first()

self.assertEqual(response.status_code, 302)
self.assertContains(response.follow(), existing_user.email)
self.assertEqual(existing_user, pending_invitation)

Expand All @@ -361,7 +347,6 @@ def test_email_required(self):
form["last_name"] = self.contact.last_name
response = form.submit(user=self.user)
expected_errors = {"email": ["Dit veld is vereist."]}
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["form"].errors, expected_errors)

def test_users_contact_is_removed(self):
Expand Down
Loading

0 comments on commit e34b559

Please sign in to comment.