diff --git a/lib/galaxy/app_unittest_utils/galaxy_mock.py b/lib/galaxy/app_unittest_utils/galaxy_mock.py index 6351b73fb9f8..15cd8a9912e2 100644 --- a/lib/galaxy/app_unittest_utils/galaxy_mock.py +++ b/lib/galaxy/app_unittest_utils/galaxy_mock.py @@ -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 = "admin@email.to" + 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 diff --git a/test/unit/app/managers/test_UserManager.py b/test/unit/app/managers/test_UserManager.py index d78a75b27099..00152b51ca97 100644 --- a/test/unit/app/managers/test_UserManager.py +++ b/test/unit/app/managers/test_UserManager.py @@ -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 @@ -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="user@nopassword.com", 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 == "user@nopassword.com" + 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('user@nopassword.com'), '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, "user@nopassword.com", "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="user@nopassword.com", 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 == "user@nopassword.com" + 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="user@nopassword.com")) + 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