From 4bf46f1c2a202b4daf31b2b829b238d550912da6 Mon Sep 17 00:00:00 2001 From: Se Yeon Kim Date: Thu, 11 Jan 2024 13:01:44 -0600 Subject: [PATCH 1/2] Check flag for permanent domain address delete Permanent delete only happens when custom_domain_management_redesign is set to everyone --- emails/models.py | 10 ++++--- emails/tests/models_tests.py | 51 ++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/emails/models.py b/emails/models.py index 0e3d81401f..5e6392d46e 100644 --- a/emails/models.py +++ b/emails/models.py @@ -714,9 +714,13 @@ def valid_address_pattern(address): def valid_address(address: str, domain: str, subdomain: str | None = None) -> bool: address_pattern_valid = valid_address_pattern(address) address_contains_badword = has_bad_words(address) - address_already_deleted = DeletedAddress.objects.filter( - address_hash=address_hash(address, domain=domain, subdomain=subdomain) - ).count() + address_already_deleted = 0 + if not subdomain or flag_is_active_in_task( + "custom_domain_management_redesign", None + ): + address_already_deleted = DeletedAddress.objects.filter( + address_hash=address_hash(address, domain=domain, subdomain=subdomain) + ).count() if ( address_already_deleted > 0 or address_contains_badword diff --git a/emails/tests/models_tests.py b/emails/tests/models_tests.py index 75b00a3cb4..2257648d66 100644 --- a/emails/tests/models_tests.py +++ b/emails/tests/models_tests.py @@ -10,6 +10,7 @@ from django.test import override_settings, TestCase from allauth.socialaccount.models import SocialAccount +from waffle.testutils import override_flag import pytest from model_bakery import baker @@ -175,6 +176,21 @@ def test_valid_address_pattern_is_not_valid(self): assert not valid_address_pattern("foo bar") assert not valid_address_pattern("Foo") + @override_flag("custom_domain_management_redesign", active=True) + def test_valid_address_dupe_domain_address_of_deleted_is_not_valid(self): + user = make_premium_test_user() + user_profile = user.profile + user_profile.subdomain = "test" + user_profile.save() + address = "same-address" + domain_address = DomainAddress.make_domain_address( + user_profile, address=address + ) + domain_address.delete() + assert not valid_address( + address, domain_address.domain_value, user_profile.subdomain + ) + class RelayAddressTest(TestCase): def setUp(self): @@ -348,20 +364,6 @@ def test_valid_address_dupe_of_deleted_invalid(self): relay_address.delete() assert not valid_address(relay_address.address, relay_address.domain_value) - def test_dupe_domain_address_of_deleted_invalid(self): - user = make_premium_test_user() - user_profile = user.profile - user_profile.subdomain = "test" - user_profile.save() - address = "same-address" - domain_address = DomainAddress.make_domain_address( - user_profile, address=address - ) - domain_address.delete() - assert not valid_address( - address, domain_address.domain_value, user_profile.subdomain - ) - @patch.object(emails_config, "blocklist", ["blocked-word"]) def test_address_contains_blocklist_invalid(self) -> None: blocked_word = "blocked-word" @@ -1122,6 +1124,27 @@ def test_make_domain_address_valid_premium_user_with_no_subdomain(self) -> None: DomainAddress.make_domain_address(user_profile, "test-nosubdomain") assert exc_info.value.get_codes() == "need_subdomain" + @override_flag("custom_domain_management_redesign", active=False) + def test_make_domain_address_can_make_dupe_of_deleted(self): + address = "same-address" + domain_address = DomainAddress.make_domain_address( + self.user_profile, address=address + ) + domain_address_hash = address_hash( + domain_address.address, + domain_address.user_profile.subdomain, + domain_address.domain_value, + ) + domain_address.delete() + dupe_domain_address = DomainAddress.make_domain_address( + self.user_profile, address=address + ) + assert ( + DeletedAddress.objects.filter(address_hash=domain_address_hash).count() == 1 + ) + assert dupe_domain_address.full_address == domain_address.full_address + + @override_flag("custom_domain_management_redesign", active=True) def test_make_domain_address_cannot_make_dupe_of_deleted(self): address = "same-address" domain_address = DomainAddress.make_domain_address( From f0de41084ff5f722e7667f4ec0e43e0b0f88cce0 Mon Sep 17 00:00:00 2001 From: Se Yeon Kim Date: Fri, 12 Jan 2024 11:51:54 -0600 Subject: [PATCH 2/2] Move test_valid_address_dupe_domain_address_of_deleted_is_not_valid --- emails/tests/models_tests.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/emails/tests/models_tests.py b/emails/tests/models_tests.py index 2257648d66..402f1dd5c0 100644 --- a/emails/tests/models_tests.py +++ b/emails/tests/models_tests.py @@ -176,21 +176,6 @@ def test_valid_address_pattern_is_not_valid(self): assert not valid_address_pattern("foo bar") assert not valid_address_pattern("Foo") - @override_flag("custom_domain_management_redesign", active=True) - def test_valid_address_dupe_domain_address_of_deleted_is_not_valid(self): - user = make_premium_test_user() - user_profile = user.profile - user_profile.subdomain = "test" - user_profile.save() - address = "same-address" - domain_address = DomainAddress.make_domain_address( - user_profile, address=address - ) - domain_address.delete() - assert not valid_address( - address, domain_address.domain_value, user_profile.subdomain - ) - class RelayAddressTest(TestCase): def setUp(self): @@ -1144,6 +1129,17 @@ def test_make_domain_address_can_make_dupe_of_deleted(self): ) assert dupe_domain_address.full_address == domain_address.full_address + @override_flag("custom_domain_management_redesign", active=True) + def test_valid_address_dupe_domain_address_of_deleted_is_not_valid(self): + address = "same-address" + domain_address = DomainAddress.make_domain_address( + self.user_profile, address=address + ) + domain_address.delete() + assert not valid_address( + address, domain_address.domain_value, self.user_profile.subdomain + ) + @override_flag("custom_domain_management_redesign", active=True) def test_make_domain_address_cannot_make_dupe_of_deleted(self): address = "same-address"