Skip to content

Commit

Permalink
Merge pull request #15681 from guerler/add_mail_test
Browse files Browse the repository at this point in the history
Verify that activation and reset emails are properly generated
  • Loading branch information
guerler authored Mar 3, 2023
2 parents 44e8a31 + f8836f6 commit 67cfa2a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/galaxy/app_unittest_utils/galaxy_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,25 @@ def __init__(self, **kwargs):
self.user_activation_on = False
self.new_user_dataset_access_role_default_private = False

self.expose_dataset_path = True
self.activation_grace_period = 0
self.allow_user_dataset_purge = True
self.allow_user_creation = True
self.auth_config_file = "config/auth_conf.xml.sample"
self.custom_activation_email_message = "custom_activation_email_message"
self.email_domain_allowlist_content = None
self.email_domain_blocklist_content = None
self.email_from = "email_from"
self.enable_old_display_applications = True
self.redact_username_in_logs = False
self.auth_config_file = "config/auth_conf.xml.sample"
self.error_email_to = "[email protected]"
self.expose_dataset_path = True
self.hostname = "hostname"
self.instance_resource_url = "instance_resource_url"
self.password_expiration_period = 0
self.pretty_datetime_format = "pretty_datetime_format"
self.redact_username_in_logs = False
self.smtp_server = True
self.terms_url = "terms_url"
self.templates_dir = "templates"

self.umask = 0o77
self.flush_per_n_datasets = 0
Expand Down
48 changes: 48 additions & 0 deletions test/unit/app/managers/test_UserManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Executable directly using: python -m test.unit.managers.test_UserManager
"""
from datetime import datetime
from unittest.mock import patch

from sqlalchemy import desc

Expand Down Expand Up @@ -187,6 +188,53 @@ def test_empty_password(self):
assert not check_password("", user.password)
assert not check_password(None, user.password)

def testable_url_for(*a, **k):
return f"(url_for): {k}"

@patch("routes.url_for", testable_url_for)
def test_activation_email(self):
self.log("should produce the activation email")
self.user_manager.create(email="[email protected]", username="nopassword")
self.trans.request.host = "request.host"

def validate_send_email(frm, to, subject, body, config, html=None):
assert frm == "email_from"
assert to == "[email protected]"
assert subject == "Galaxy Account Activation"
assert "custom_activation_email_message" in body
assert "Hello nopassword" in body
assert (
"{'controller': 'user', 'action': 'activate', 'activation_token': 'activation_token', 'email': Markup('[email protected]'), 'qualified': True}"
in body
)

with patch("galaxy.util.send_mail", side_effect=validate_send_email) as mock_send_mail:
with patch("galaxy.util.hash_util.new_secure_hash_v2", return_value="activation_token") as mock_hash_util:
result = self.user_manager.send_activation_email(self.trans, "[email protected]", "nopassword")
mock_send_mail.assert_called_once()
mock_hash_util.assert_called_once()
assert result is True

@patch("routes.url_for", testable_url_for)
def test_reset_email(self):
self.log("should produce the password reset email")
self.user_manager.create(email="[email protected]", username="nopassword")
self.trans.request.host = "request.host"

def validate_send_email(frm, to, subject, body, config, html=None):
assert frm == "email_from"
assert to == "[email protected]"
assert subject == "Galaxy Password Reset"
assert "reset your Galaxy password" in body
assert "{'controller': 'login', 'action': 'start', 'token': 'reset_token'}" in body

with patch("galaxy.util.send_mail", side_effect=validate_send_email) as mock_send_mail:
with patch("galaxy.model.unique_id", return_value="reset_token") as mock_unique_id:
result = self.user_manager.send_reset_email(self.trans, dict(email="[email protected]"))
mock_send_mail.assert_called_once()
mock_unique_id.assert_called_once()
assert result is None

def test_get_user_by_identity(self):
# return None if username/email not found
assert self.user_manager.get_user_by_identity("xyz") is None
Expand Down

0 comments on commit 67cfa2a

Please sign in to comment.