From e4a5b7b3b6db4fa285d71a483937e519fc8f1301 Mon Sep 17 00:00:00 2001 From: Tiago Fonseca Date: Tue, 21 Jan 2025 13:44:48 -0300 Subject: [PATCH] fix imports and tests --- api/app/utils.py | 16 +++---- api/tests/unit/app/test_unit_app_utils.py | 51 +++++++++++------------ 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/api/app/utils.py b/api/app/utils.py index c414a1c79f8e..f0f29a6be234 100644 --- a/api/app/utils.py +++ b/api/app/utils.py @@ -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" @@ -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 diff --git a/api/tests/unit/app/test_unit_app_utils.py b/api/tests/unit/app/test_unit_app_utils.py index b12e6c489c6a..f8aee0c6f54e 100644 --- a/api/tests/unit/app/test_unit_app_utils.py +++ b/api/tests/unit/app/test_unit_app_utils.py @@ -5,6 +5,7 @@ import pytest from pytest_mock import MockerFixture +from pytest_django.fixtures import SettingsWrapper from app.utils import get_version_info @@ -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", @@ -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", @@ -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", @@ -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", @@ -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