-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b6ffaa
commit b0605e0
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,37 @@ | ||
from __future__ import annotations | ||
|
||
from django.apps import AppConfig | ||
from django.conf import global_settings | ||
from django.conf import settings | ||
|
||
try: | ||
from environs import Env | ||
except ImportError: | ||
Env = None | ||
|
||
|
||
class EmailRelayConfig(AppConfig): | ||
name = "email_relay" | ||
default_auto_field = "django.db.models.BigAutoField" | ||
|
||
def ready(self) -> None: | ||
from email_relay.conf import app_settings | ||
|
||
if not app_settings.ENABLE_AUTO_SETUP: | ||
return | ||
|
||
if ( | ||
settings.EMAIL_BACKEND is None | ||
or settings.EMAIL_BACKEND == global_settings.EMAIL_BACKEND | ||
): | ||
settings.EMAIL_BACKEND = "email_relay.backend.EmailRelayBackend" | ||
|
||
if app_settings.DATABASE_ALIAS not in settings.DATABASES and Env is not None: | ||
env = Env() | ||
|
||
settings.DATABASES[app_settings.DATABASE_ALIAS] = env.dj_db_url( | ||
"EMAIL_RELAY_DATABASE_URL", default="sqlite://:memory:" | ||
) | ||
|
||
if "email_relay.db.EmailDatabaseRouter" not in settings.DATABASE_ROUTERS: | ||
settings.DATABASE_ROUTERS.append("email_relay.db.EmailDatabaseRouter") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
from django.apps import apps | ||
from django.conf import settings | ||
from django.test.utils import override_settings | ||
|
||
from email_relay.conf import EMAIL_RELAY_DATABASE_ALIAS | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="module") | ||
def auto_setup_setting(): | ||
with override_settings( | ||
DJANGO_EMAIL_RELAY={ | ||
"ENABLE_AUTO_SETUP": True, | ||
}, | ||
): | ||
yield | ||
|
||
|
||
@pytest.fixture | ||
def app_config(): | ||
return apps.get_app_config("email_relay") | ||
|
||
|
||
@override_settings(EMAIL_BACKEND=None) | ||
def test_auto_setup_email_backend(app_config): | ||
app_config.ready() | ||
|
||
assert settings.EMAIL_BACKEND == "email_relay.backend.EmailRelayBackend" | ||
|
||
|
||
@override_settings( | ||
DATABASES={ | ||
"default": { | ||
"ENGINE": "django.db.backends.sqlite3", | ||
"NAME": ":memory:", | ||
}, | ||
}, | ||
) | ||
def test_auto_setup_database(app_config): | ||
app_config.ready() | ||
|
||
assert EMAIL_RELAY_DATABASE_ALIAS in settings.DATABASES | ||
|
||
|
||
def test_auto_setup_database_router(app_config): | ||
app_config.ready() | ||
|
||
assert "email_relay.db.EmailDatabaseRouter" in settings.DATABASE_ROUTERS |