Skip to content

Commit

Permalink
Complete renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
tcompa committed Feb 4, 2025
1 parent 87097e2 commit e19e5f4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
9 changes: 6 additions & 3 deletions fractal_server/app/security/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,18 @@ async def on_after_register(
# Send mail section
settings = Inject(get_settings)

if this_user.oauth_accounts and settings.mail_settings is not None:
if (
this_user.oauth_accounts
and settings.email_settings is not None
):
try:
logger.info(
"START sending email about new signup to "
f"{settings.mail_settings.recipients}."
f"{settings.email_settings.recipients}."
)
mail_new_oauth_signup(
msg=f"New user registered: '{this_user.email}'.",
mail_settings=settings.mail_settings,
email_settings=settings.email_settings,
)
logger.info("END sending email about new signup.")
except Exception as e:
Expand Down
28 changes: 16 additions & 12 deletions fractal_server/app/security/signup_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,42 @@
from fractal_server.config import MailSettings


def mail_new_oauth_signup(msg: str, mail_settings: MailSettings):
def mail_new_oauth_signup(msg: str, email_settings: MailSettings):
"""
Send an email using the specified settings to notify a new OAuth signup.
"""

mail_msg = EmailMessage()
mail_msg.set_content(msg)
mail_msg["From"] = formataddr((mail_settings.sender, mail_settings.sender))
mail_msg["From"] = formataddr(
(email_settings.sender, email_settings.sender)
)
mail_msg["To"] = ", ".join(
[
formataddr((recipient, recipient))
for recipient in mail_settings.recipients
for recipient in email_settings.recipients
]
)
mail_msg[
"Subject"
] = f"[Fractal, {mail_settings.instance_name}] New OAuth signup"
] = f"[Fractal, {email_settings.instance_name}] New OAuth signup"

with smtplib.SMTP(mail_settings.smtp_server, mail_settings.port) as server:
with smtplib.SMTP(
email_settings.smtp_server, email_settings.port
) as server:
server.ehlo()
if mail_settings.use_starttls:
if email_settings.use_starttls:
server.starttls()
server.ehlo()
if mail_settings.use_login:
if email_settings.use_login:
password = (
Fernet(mail_settings.encryption_key)
.decrypt(mail_settings.encrypted_password)
Fernet(email_settings.encryption_key)
.decrypt(email_settings.encrypted_password)
.decode("utf-8")
)
server.login(user=mail_settings.sender, password=password)
server.login(user=email_settings.sender, password=password)

Check notice on line 43 in fractal_server/app/security/signup_email.py

View workflow job for this annotation

GitHub Actions / Coverage

Missing coverage

Missing coverage on lines 34-43
server.sendmail(
from_addr=mail_settings.sender,
to_addrs=mail_settings.recipients,
from_addr=email_settings.sender,
to_addrs=email_settings.recipients,
msg=mail_msg.as_string(),
)
4 changes: 2 additions & 2 deletions fractal_server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ def PIP_CACHE_DIR_ARG(self) -> str:
"""
TBD
"""
mail_settings: Optional[MailSettings] = None
email_settings: Optional[MailSettings] = None

@root_validator(pre=True)
def validate_email_settings(cls, values):
Expand Down Expand Up @@ -657,7 +657,7 @@ def assert_key(key: str):
f"Original error: {str(e)}."
)

values["FRACTAL_EMAIL_SETTINGS"] = MailSettings(
values["email_settings"] = MailSettings(
sender=email_values["FRACTAL_EMAIL_SENDER"],
recipients=email_values["FRACTAL_EMAIL_RECIPIENTS"].split(","),
smtp_server=email_values["FRACTAL_EMAIL_SMTP_SERVER"],
Expand Down
2 changes: 1 addition & 1 deletion tests/no_version/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

async def test_server_not_available(override_settings_factory, db, caplog):
override_settings_factory(
FRACTAL_EMAIL_SETTINGS=MailSettings(
email_settings=MailSettings(
sender="fractal@fractal.fractal",
recipients=["test@example.org"],
smtp_server="localhost",
Expand Down
15 changes: 6 additions & 9 deletions tests/no_version/test_unit_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def test_fractal_email():
)
# 1: no mail settings
settings = Settings(**common_attributes)
assert settings.mail_settings is None
assert settings.email_settings is None
# 2: FRACTAL_EMAIL_USE_LOGIN is true, but no password settings
with pytest.raises(ValidationError):
Settings(
Expand Down Expand Up @@ -437,15 +437,15 @@ def test_fractal_email():
FRACTAL_EMAIL_PASSWORD=FRACTAL_EMAIL_PASSWORD,
FRACTAL_EMAIL_PASSWORD_KEY=FRACTAL_EMAIL_PASSWORD_KEY,
)
assert settings.mail_settings is not None
assert len(settings.mail_settings.recipients) == 2
assert settings.email_settings is not None
assert len(settings.email_settings.recipients) == 2
# 5: FRACTAL_EMAIL_USE_LOGIN is false and no password needed
settings = Settings(
**common_attributes,
**required_mail_args,
FRACTAL_EMAIL_USE_LOGIN=False,
)
assert settings.mail_settings is not None
assert settings.email_settings is not None
# 6: missing required arguments
for arg in required_mail_args:
with pytest.raises(ValidationError):
Expand All @@ -455,23 +455,20 @@ def test_fractal_email():
FRACTAL_EMAIL_USE_LOGIN=False,
)
# 7a: fail with Fernet encryption
with pytest.raises(ValidationError) as e:
with pytest.raises(ValidationError, match="FRACTAL_EMAIL_PASSWORD"):
Settings(
**common_attributes,
**required_mail_args,
FRACTAL_EMAIL_PASSWORD="invalid",
FRACTAL_EMAIL_PASSWORD_KEY=FRACTAL_EMAIL_PASSWORD_KEY,
)
assert "FRACTAL_EMAIL_PASSWORD" in e.value.errors()[0]["msg"]
debug(e.value.errors()[0]["msg"])
with pytest.raises(ValidationError) as e:
with pytest.raises(ValidationError, match="FRACTAL_EMAIL_PASSWORD"):
Settings(
**common_attributes,
**required_mail_args,
FRACTAL_EMAIL_PASSWORD=FRACTAL_EMAIL_PASSWORD,
FRACTAL_EMAIL_PASSWORD_KEY="invalid",
)
assert "FRACTAL_EMAIL_PASSWORD_KEY" in e.value.errors()[0]["msg"]

# 8: fail with sender emails
with pytest.raises(ValidationError):
Expand Down

0 comments on commit e19e5f4

Please sign in to comment.