Skip to content

Commit

Permalink
When inviting users validate addresses wrt settings.EMAIL_VALIDATORS
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Apr 27, 2024
1 parent f480519 commit 32a1846
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
10 changes: 7 additions & 3 deletions tcms_tenants/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2021 Alexander Todorov <[email protected]>
# Copyright (c) 2019-2024 Alexander Todorov <[email protected]>

# Licensed under the GPL 3.0: https://www.gnu.org/licenses/gpl-3.0.txt
import re
Expand All @@ -10,6 +10,7 @@

from django_tenants.postgresql_backend import base as validators

from tcms.core.utils.mailto import custom_email_validators
from tcms_tenants import models
from tcms_tenants import utils

Expand Down Expand Up @@ -57,13 +58,16 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

for i in self.range:
self.fields[f"email_{i}"] = forms.CharField(required=False)
self.fields[f"email_{i}"] = forms.EmailField(
required=False,
validators=[custom_email_validators],
)

def clean(self):
emails = set()

for i in self.range:
email = self.cleaned_data[f"email_{i}"]
email = self.cleaned_data.get(f"email_{i}")
if email:
emails.add(email)

Expand Down
35 changes: 34 additions & 1 deletion tcms_tenants/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2022 Alexander Todorov <[email protected]>
# Copyright (c) 2019-2024 Alexander Todorov <[email protected]>

# Licensed under the GPL 3.0: https://www.gnu.org/licenses/gpl-3.0.txt
# pylint: disable=too-many-ancestors
Expand All @@ -16,6 +16,8 @@

from django_tenants.utils import tenant_context

from tcms.tests import deny_certain_email_addresses

from tcms_tenants.models import Tenant
from tcms_tenants.forms import VALIDATION_RE
from tcms_tenants.tests import LoggedInTestCase, TenantGroupsTestCase
Expand Down Expand Up @@ -243,6 +245,37 @@ def test_invited_users_are_granted_access(self, send_mail):
self.assertTrue(invited_user.tenant_groups.filter(name="InvitedUsers").exists())
self.assertFalse(invited_user.tenant_groups.filter(name="Tester").exists())

@override_settings(
EMAIL_VALIDATORS=(deny_certain_email_addresses,),
)
@patch("tcms.core.utils.mailto.send_mail")
def test_no_invites_sent_when_form_validation_fails(self, send_mail):
invitation_data = {
"email_0": "[email protected]",
"email_1": "[email protected]",
"email_2": "[email protected]",
}

for address in invitation_data.values():
self.assertFalse(UserModel.objects.filter(username=address).exists())

response = self.client.post(
reverse("tcms_tenants:invite-users"),
{
"email_0": "[email protected]",
"email_1": "[email protected]",
"email_2": "[email protected]",
},
)

send_mail.assert_not_called()
self.assertIsNotInstance(response, HttpResponseRedirect)
self.assertContains(response, "@yahoo email address has been denied")

for address in invitation_data.values():
self.assertContains(response, address)
self.assertFalse(UserModel.objects.filter(username=address).exists())


class UpdateTenantViewTestCase(LoggedInTestCase):
@classmethod
Expand Down

0 comments on commit 32a1846

Please sign in to comment.