From eb3c286de1888b5db1c1850a1c82da28ff787a63 Mon Sep 17 00:00:00 2001 From: Vinicius Aguiar Date: Thu, 14 Sep 2023 13:05:09 -0300 Subject: [PATCH] fix: add send activation email checks (#386) --- basedosdados_api/account/signals.py | 12 +++++-- basedosdados_api/account/tests.py | 5 +++ .../core/management/commands/loadfixture.py | 5 +-- basedosdados_api/utils.py | 31 +++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 basedosdados_api/utils.py diff --git a/basedosdados_api/account/signals.py b/basedosdados_api/account/signals.py index b7a799cb..4dcdfe0c 100644 --- a/basedosdados_api/account/signals.py +++ b/basedosdados_api/account/signals.py @@ -9,12 +9,20 @@ from basedosdados_api.account.models import Account from basedosdados_api.account.token import token_generator from basedosdados_api.settings import EMAIL_HOST_USER +from basedosdados_api.utils import is_prod @receiver(post_save, sender=Account) def send_activation_email(sender, instance, created, raw, **kwargs): - """Send activation email to instance after registration, not fixtures""" - if created and not raw: + """Send activation email to instance after registration + + It only sends the email if: + - The account is new + - The account isn't active + - The account isn't a fixture + - The account is in production environment + """ + if created and not raw and not instance.is_active and is_prod(): to_email = instance.email from_email = EMAIL_HOST_USER subject = "Bem Vindo à Base dos Dados!" diff --git a/basedosdados_api/account/tests.py b/basedosdados_api/account/tests.py index ad18e581..9bccdd0b 100644 --- a/basedosdados_api/account/tests.py +++ b/basedosdados_api/account/tests.py @@ -11,6 +11,7 @@ @pytest.mark.django_db +@patch("basedosdados_api.account.signals.is_prod", new=lambda: True) def test_account_create(): account = Account.objects.create( username="john.doe", @@ -21,6 +22,7 @@ def test_account_create(): @pytest.mark.django_db @patch("basedosdados_api.account.signals.EmailMultiAlternatives") +@patch("basedosdados_api.account.signals.is_prod", new=lambda: True) def test_activate_account_signal(mock: MagicMock): Account.objects.create( username="john.doe", @@ -33,6 +35,7 @@ def test_activate_account_signal(mock: MagicMock): @pytest.mark.django_db @patch("basedosdados_api.account.signals.render_to_string") +@patch("basedosdados_api.account.signals.is_prod", new=lambda: True) def test_activate_account_confirmation(mock: MagicMock, client: Client): account = Account.objects.create( username="john.doe", @@ -52,6 +55,7 @@ def test_activate_account_confirmation(mock: MagicMock, client: Client): @pytest.mark.django_db @patch("basedosdados_api.account.views.EmailMultiAlternatives") +@patch("basedosdados_api.account.signals.is_prod", new=lambda: True) def test_password_reset_request(mock: MagicMock, client: Client): account = Account.objects.create( username="john.doe", @@ -72,6 +76,7 @@ def test_password_reset_request(mock: MagicMock, client: Client): @pytest.mark.django_db @patch("basedosdados_api.account.views.render_to_string") @patch("basedosdados_api.account.signals.render_to_string") +@patch("basedosdados_api.account.signals.is_prod", new=lambda: True) def test_password_reset_confirmation(mock_signal: MagicMock, mock_view: MagicMock, client: Client): # Create account account = Account.objects.create( diff --git a/basedosdados_api/core/management/commands/loadfixture.py b/basedosdados_api/core/management/commands/loadfixture.py index faa1259a..e907bb01 100644 --- a/basedosdados_api/core/management/commands/loadfixture.py +++ b/basedosdados_api/core/management/commands/loadfixture.py @@ -6,9 +6,10 @@ from django.db import connection from modeltranslation.management.commands.loaddata import Command as LoadDataCommand +from basedosdados_api.utils import is_prod + IS_SQLITE = "sqlite" in settings.DATABASES.get("default", {}).get("ENGINE") IS_POSTGRES = "postgres" in settings.DATABASES.get("default", {}).get("ENGINE") -IS_PRODUCTION = "prod" in settings.SETTINGS_MODULE DB_PATH = Path(settings.DATABASES.get("default", {}).get("NAME", ".")) DB_STATEMENT = "TRUNCATE" if IS_POSTGRES and not IS_SQLITE else "DELETE FROM" @@ -30,7 +31,7 @@ class Command(LoadDataCommand): """ def handle(self, *args, **options) -> str | None: - if IS_PRODUCTION: + if is_prod(): return None print("Purge previous database if exists") diff --git a/basedosdados_api/utils.py b/basedosdados_api/utils.py new file mode 100644 index 00000000..88903db4 --- /dev/null +++ b/basedosdados_api/utils.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +from django.conf import settings + +DB_NAME = settings.DATABASES.get("default", {}).get("NAME", "") +DB_ENGINE = settings.DATABASES.get("default", {}).get("ENGINE", "") + + +def is_remote(): + """Check if it is remote environment""" + return "prod" in settings.SETTINGS_MODULE + + +def is_dev(): + """Check if it is remote development environment""" + if is_remote() and "dev" in DB_NAME: + return True + return False + + +def is_stag(): + """Check if it is remote staging environment""" + if is_remote() and "stag" in DB_NAME: + return True + return False + + +def is_prod(): + """Check if it is remote production environment""" + if is_remote() and not is_dev() and not is_stag(): + return True + return False