Skip to content

Commit

Permalink
fix imports and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagoapolo committed Jan 21, 2025
1 parent 4d4a569 commit e4a5b7b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
16 changes: 6 additions & 10 deletions api/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
from typing import TypedDict

import shortuuid
from app.settings.common import (
EMAIL_BACKEND,
EMAIL_HOST_USER,
SENDGRID_API_KEY,
AWS_SES_REGION_ENDPOINT,
)

from django.conf import settings

UNKNOWN = "unknown"
VERSIONS_INFO_FILE_LOCATION = ".versions.json"
Expand Down Expand Up @@ -37,13 +33,13 @@ def is_saas() -> bool:


def has_email_provider() -> bool:
match EMAIL_BACKEND:
match settings.EMAIL_BACKEND:
case "django.core.mail.backends.smtp.EmailBackend":
return EMAIL_HOST_USER is not None
return settings.EMAIL_HOST_USER is not None
case "sgbackend.SendGridBackend":
return SENDGRID_API_KEY is not None
return settings.SENDGRID_API_KEY is not None
case "django_ses.SESBackend":
return AWS_SES_REGION_ENDPOINT is not None
return settings.AWS_SES_REGION_ENDPOINT is not None
case _:
return False

Expand Down
51 changes: 25 additions & 26 deletions api/tests/unit/app/test_unit_app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest
from pytest_mock import MockerFixture
from pytest_django.fixtures import SettingsWrapper

from app.utils import get_version_info

Expand Down Expand Up @@ -83,16 +84,13 @@ def path_side_effect(file_path: str) -> mocker.MagicMock:
}


def test_get_version_info_with_email_config_smtp(mocker: MockerFixture) -> None:
def test_get_version_info_with_email_config_smtp(settings: SettingsWrapper) -> None:

settings.EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
settings.EMAIL_HOST_USER = "user"

mocker.patch(
"app.utils.EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend"
)
mocker.patch("app.utils.EMAIL_HOST_USER", "user")
# When
result = get_version_info()

# Then
assert result == {
"ci_commit_sha": "unknown",
"image_tag": "unknown",
Expand All @@ -102,14 +100,13 @@ def test_get_version_info_with_email_config_smtp(mocker: MockerFixture) -> None:
}


def test_get_version_info_with_email_config_sendgrid(mocker: MockerFixture) -> None:
def test_get_version_info_with_email_config_sendgrid(settings: SettingsWrapper) -> None:

settings.EMAIL_BACKEND = "sgbackend.SendGridBackend"
settings.SENDGRID_API_KEY = "key"

mocker.patch("app.utils.EMAIL_BACKEND", "sgbackend.SendGridBackend")
mocker.patch("app.utils.SENDGRID_API_KEY", "key")
# When
result = get_version_info()

# Then
assert result == {
"ci_commit_sha": "unknown",
"image_tag": "unknown",
Expand All @@ -119,14 +116,13 @@ def test_get_version_info_with_email_config_sendgrid(mocker: MockerFixture) -> N
}


def test_get_version_info_with_email_config_ses(mocker: MockerFixture) -> None:
def test_get_version_info_with_email_config_ses(settings: SettingsWrapper) -> None:

settings.EMAIL_BACKEND = "django_ses.SESBackend"
settings.AWS_SES_REGION_ENDPOINT = "endpoint"

mocker.patch("app.utils.EMAIL_BACKEND", "django_ses.SESBackend")
mocker.patch("app.utils.AWS_SES_REGION_ENDPOINT", "endpoint")
# When
result = get_version_info()

# Then
assert result == {
"ci_commit_sha": "unknown",
"image_tag": "unknown",
Expand All @@ -136,7 +132,7 @@ def test_get_version_info_with_email_config_ses(mocker: MockerFixture) -> None:
}


def test_get_version_info_without_email_config(mocker: MockerFixture) -> None:
def test_get_version_info_without_email_config(settings: SettingsWrapper) -> None:
expected = {
"ci_commit_sha": "unknown",
"image_tag": "unknown",
Expand All @@ -145,22 +141,25 @@ def test_get_version_info_without_email_config(mocker: MockerFixture) -> None:
"is_saas": False,
}

mocker.patch(
"app.utils.EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend"
)
mocker.patch("app.utils.EMAIL_HOST_USER", None)
settings.EMAIL_BACKEND = None

result = get_version_info()
assert result == expected

settings.EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
settings.EMAIL_HOST_USER = None

result = get_version_info()
assert result == expected

mocker.patch("app.utils.EMAIL_BACKEND", "django_ses.SESBackend")
mocker.patch("app.utils.AWS_SES_REGION_ENDPOINT", None)
settings.EMAIL_BACKEND = "django_ses.SESBackend"
settings.AWS_SES_REGION_ENDPOINT = None

result = get_version_info()
assert result == expected

mocker.patch("app.utils.EMAIL_BACKEND", "sgbackend.SendGridBackend")
mocker.patch("app.utils.SENDGRID_API_KEY", None)
settings.EMAIL_BACKEND = "sgbackend.SendGridBackend"
settings.SENDGRID_API_KEY = None

result = get_version_info()
assert result == expected

0 comments on commit e4a5b7b

Please sign in to comment.