Skip to content

Commit

Permalink
fix: add send activation email checks (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored Sep 14, 2023
1 parent d8fa608 commit eb3c286
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
12 changes: 10 additions & 2 deletions basedosdados_api/account/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down
5 changes: 5 additions & 0 deletions basedosdados_api/account/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions basedosdados_api/core/management/commands/loadfixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down
31 changes: 31 additions & 0 deletions basedosdados_api/utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit eb3c286

Please sign in to comment.