-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When inviting users validate addresses wrt settings.EMAIL_VALIDATORS
- Loading branch information
Showing
2 changed files
with
41 additions
and
4 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
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 | ||
|
@@ -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 | ||
|
||
|
@@ -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) | ||
|
||
|
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 |
---|---|---|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|