-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Updating testing to check this - Allow custom env files to be passed to `create_app`
- Loading branch information
Showing
5 changed files
with
84 additions
and
17 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
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
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
from flask_mail import Mail, Message | ||
|
||
from pydatalab.config import CONFIG | ||
from pydatalab.logger import LOGGER | ||
|
||
MAIL = Mail() | ||
|
||
|
@@ -20,14 +20,14 @@ def send_mail(recipient: str, subject: str, body: str): | |
body (str): The body of the email. | ||
""" | ||
|
||
if CONFIG.EMAIL_AUTH_SMTP_SETTINGS is None: | ||
raise RuntimeError("No SMTP settings configured.") | ||
LOGGER.debug("Sending email to %s", recipient) | ||
|
||
message = Message( | ||
sender=CONFIG.EMAIL_AUTH_SMTP_SETTINGS.MAIL_DEFAULT_SENDER, | ||
sender="[email protected]", | ||
recipients=[recipient], | ||
body=body, | ||
subject=subject, | ||
) | ||
MAIL.connect() | ||
MAIL.send(message) | ||
LOGGER.debug("Email sent to %s", recipient) |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
import pytest | ||
|
||
from pydatalab.config import ServerConfig | ||
from pydatalab.config import ServerConfig, SMTPSettings | ||
from pydatalab.main import create_app | ||
|
||
|
||
|
@@ -42,5 +42,41 @@ def test_config_override(): | |
|
||
def test_validators(): | ||
# check bad prefix | ||
with pytest.raises(RuntimeError): | ||
_ = ServerConfig(IDENTIFIER_PREFIX="this prefix is way way too long") | ||
with pytest.raises(RuntimeError, match="Identifier prefix must be less than 12 characters long,"): | ||
_ = ServerConfig(IDENTIFIER_PREFIX="this prefix is way way too long", TESTING=False) | ||
|
||
|
||
def test_mail_settings_combinations(tmpdir): | ||
"""Tests that the config file mail settings get passed | ||
correctly to the flask settings, and that additional | ||
overrides can be provided as environment variables. | ||
""" | ||
|
||
from pydatalab.config import CONFIG | ||
|
||
CONFIG.update( | ||
{ | ||
"EMAIL_AUTH_SMTP_SETTINGS": SMTPSettings( | ||
MAIL_SERVER="example.com", | ||
MAIL_DEFAULT_SENDER="[email protected]", | ||
MAIL_PORT=587, | ||
MAIL_USE_TLS=True, | ||
MAIL_USERNAME="user", | ||
) | ||
} | ||
) | ||
|
||
app = create_app() | ||
assert app.config["MAIL_SERVER"] == "example.com" | ||
assert app.config["MAIL_DEFAULT_SENDER"] == "[email protected]" | ||
assert app.config["MAIL_PORT"] == 587 | ||
assert app.config["MAIL_USE_TLS"] is True | ||
assert app.config["MAIL_USERNAME"] == "user" | ||
|
||
# write temporary .env file and check that it overrides the config | ||
env_file = Path(tmpdir.join(".env")) | ||
env_file.write_text("MAIL_PASSWORD=password\n[email protected]") | ||
|
||
app = create_app(env_file=env_file) | ||
assert app.config["MAIL_PASSWORD"] == "password" | ||
assert app.config["MAIL_DEFAULT_SENDER"] == "[email protected]" |