Files
galaxy/test/unit/app/tools/test_error_reporting.py
paperbenni bec3d3ad9c Drop support for Python 3.8
Drop Python 3.8 support in 5 Pulsar-compatible packages
(job_metrics, tool_util, tool_util_models, util, objectstore)
and run pyupgrade --py310-plus on their source code.

- Bump requires-python from >=3.8 to >=3.10 in all 5 packages
- Remove Python 3.8 and 3.9 classifiers
- Remove ruff per-file-ignores for UP rules on these paths
- Remove backports.zoneinfo conditional dependency
- Remove pydyf<0.11 pin from conditional-requirements.txt
- Update Makefile pyupgrade target (remove PY38_PYUPGRADE_PATHS)
- Update CI workflow to test with Python 3.10 instead of 3.8

Clean up unused deprecated typing imports after pyupgrade

Remove now-unused typing imports (Dict, List, Optional, Set, Tuple,
Type, Union) that became dead after pyupgrade --py310-plus converted
annotations to use built-in types and | syntax.

Also run ruff check --fix --select=UP007,UP045 across the entire
codebase to convert remaining Optional[X] -> X | None and
Union[X, Y] -> X | Y patterns.

Enable ruff UP007/UP045 for Python 3.10 union syntax

Remove UP007 (Union[X,Y] -> X | Y) and UP045 (Optional[X] -> X | None)
from the ruff ignore list and convert all type aliases across the
codebase. These rules were deferred while Python 3.9 was supported;
requires-python is now >=3.10.

A custom script was used because neither ruff --fix nor
pyupgrade --py310-plus converts Optional[X]/Union[X,Y] in type alias
positions (e.g. X = Union[A, B]) — they only handle annotation
positions (e.g. def f(x: Optional[int])). All 167 violations were
module-level type aliases. A few edge cases were fixed manually:
single-element Union[X,], typing.Union qualified refs, runtime
Optional[type] calls, and Annotated[Optional[...]] pydantic fields.

Fix UP007 autofix regression with string forward reference type aliases

Commit fa6bd955a0 enabled ruff UP007/UP045 and auto-fixed
module-level type aliases using Union with string forward
references, producing invalid 'str | str' expressions.

This was a known ruff bug (charliermarsh/ruff#826) that has since
been fixed in later ruff versions, but this codebase was
converted before the fix was in place.

Revert to Union syntax and restore TYPE_CHECKING imports that
ruff's TCH rule cleaned up as a side effect when it thought the
forward references were unused.
2026-06-25 15:12:05 +01:00

195 lines
8.9 KiB
Python

import json
import shutil
import tempfile
from pathlib import Path
from unittest import mock
from galaxy import model
from galaxy.app_unittest_utils import galaxy_mock
from galaxy.app_unittest_utils.tools_support import UsesApp
from galaxy.tools.errors import EmailErrorReporter
from galaxy.util.unittest import TestCase
from galaxy.workflow.errors import WorkflowEmailErrorReporter
# The email the user created their account with.
TEST_USER_EMAIL = "mockgalaxyuser@galaxyproject.org"
# The email the user supplied when submitting the error
TEST_USER_SUPPLIED_EMAIL = "fake@example.org"
TEST_SERVER_EMAIL_FROM = "email_from@galaxyproject.org"
TEST_SERVER_ERROR_EMAIL_TO = "admin@email.to" # setup in mock config
class TestErrorReporter(TestCase, UsesApp):
def setUp(self):
self.setup_app()
self.app.config.email_from = TEST_SERVER_EMAIL_FROM
self.tmp_path = Path(tempfile.mkdtemp())
self.email_path = self.tmp_path / "email.json"
smtp_server = f"mock_emails_to_path://{self.email_path}"
self.app.config.smtp_server = smtp_server
self.app.workflow_manager = mock.MagicMock()
def tearDown(self):
shutil.rmtree(self.tmp_path)
def test_basic(self):
user, hda = self._setup_model_objects()
email_path = self.email_path
assert not email_path.exists()
error_report = EmailErrorReporter(hda, self.app)
error_report.send_report(user, email=TEST_USER_SUPPLIED_EMAIL, message="My custom message")
assert email_path.exists()
text = email_path.read_text()
email_json = json.loads(text)
assert email_json["from"] == TEST_SERVER_EMAIL_FROM
assert email_json["to"] == f"{TEST_SERVER_ERROR_EMAIL_TO}, {TEST_USER_SUPPLIED_EMAIL}"
assert f"Galaxy tool error report from {TEST_USER_SUPPLIED_EMAIL}" == email_json["subject"]
assert "cat1" in email_json["body"]
assert "cat1" in email_json["html"]
assert TEST_USER_EMAIL == email_json["reply_to"]
def test_workflow_error_reporting(self):
user, invocation, trans = self._setup_invocation_model_objects()
email_path = self.email_path
assert not email_path.exists()
error_report = WorkflowEmailErrorReporter(invocation, self.app)
error_report.send_report(user, email=TEST_USER_SUPPLIED_EMAIL, message="My custom message", trans=trans)
assert email_path.exists()
text = email_path.read_text()
email_json = json.loads(text)
assert email_json["from"] == TEST_SERVER_EMAIL_FROM
assert email_json["to"] == f"{TEST_SERVER_ERROR_EMAIL_TO}, {TEST_USER_SUPPLIED_EMAIL}"
assert f"Galaxy workflow run error report from {TEST_USER_SUPPLIED_EMAIL}" == email_json["subject"]
assert "Test Workflow" in email_json["body"]
assert "Test Workflow" in email_json["html"]
assert TEST_USER_EMAIL == email_json["reply_to"]
def test_workflow_error_reporting_inaccessible_history(self):
_, invocation, trans = self._setup_invocation_model_objects()
other_user = model.User(email="otheruser@galaxyproject.org", password="mockpass2")
self._commit_objects([other_user])
email_path = self.email_path
assert not email_path.exists()
error_report = WorkflowEmailErrorReporter(invocation, self.app)
self.app.workflow_manager.check_security.side_effect = Exception("Invocation is not accessible") # type: ignore[attr-defined]
error_report.send_report(other_user, email=TEST_USER_SUPPLIED_EMAIL, message="My custom message", trans=trans)
# Same comment as in `test_hda_security`, but for accessibility of the invocation:
# Without accessibility, the email still gets sent but the supplied email is ignored
assert email_path.exists()
text = email_path.read_text()
email_json = json.loads(text)
assert email_json["from"] == TEST_SERVER_EMAIL_FROM
assert email_json["to"] == f"{TEST_SERVER_ERROR_EMAIL_TO}"
def test_hda_security(self, tmp_path):
user, hda = self._setup_model_objects()
error_report = EmailErrorReporter(hda, self.app)
security_agent = self.app.security_agent
private_role = security_agent.create_private_user_role(user)
access_action = security_agent.permitted_actions.DATASET_ACCESS.action
manage_action = security_agent.permitted_actions.DATASET_MANAGE_PERMISSIONS.action
permissions = {access_action: [private_role], manage_action: [private_role]}
security_agent.set_all_dataset_permissions(hda.dataset, permissions)
other_user = model.User(email="otheruser@galaxyproject.org", password="mockpass2")
self._commit_objects([other_user])
security_agent = self.app.security_agent
email_path = self.email_path
assert not email_path.exists()
error_report.send_report(other_user, email=TEST_USER_SUPPLIED_EMAIL, message="My custom message")
# Without permissions, the email still gets sent but the supplied email is ignored
# I'm not saying this is the right behavior but it is what the code does at the time of test
# writing -John
assert email_path.exists()
text = email_path.read_text()
email_json = json.loads(text)
assert "otheruser@galaxyproject.org" not in email_json["to"]
def test_html_sanitization(self, tmp_path):
user, hda = self._setup_model_objects()
email_path = self.email_path
assert not email_path.exists()
error_report = EmailErrorReporter(hda, self.app)
error_report.send_report(
user, email=TEST_USER_SUPPLIED_EMAIL, message='My custom <a href="http://sneaky.com/">message</a>'
)
assert email_path.exists()
text = email_path.read_text()
email_json = json.loads(text)
html = email_json["html"]
assert "&lt;a href=&#34;http://sneaky.com/&#34;&gt;message&lt;/a&gt;" in html
def test_redact_user_details_in_bugreport(self, tmp_path):
user, hda = self._setup_model_objects()
email_path = self.email_path
assert not email_path.exists()
error_report = EmailErrorReporter(hda, self.app)
error_report.send_report(
user, email=TEST_USER_SUPPLIED_EMAIL, message="My custom message", redact_user_details_in_bugreport=True
)
assert email_path.exists()
text = email_path.read_text()
email_json = json.loads(text)
assert "The user redacted (user: 1) provided the following information:" in email_json["body"]
assert (
"""The user <span style="font-family: monospace;">redacted (user: 1)</span> provided the following information:"""
in email_json["html"]
)
def test_no_redact_user_details_in_bugreport(self, tmp_path):
user, hda = self._setup_model_objects()
email_path = self.email_path
assert not email_path.exists()
error_report = EmailErrorReporter(hda, self.app)
error_report.send_report(
user, email=TEST_USER_SUPPLIED_EMAIL, message="My custom message", redact_user_details_in_bugreport=False
)
assert email_path.exists()
text = email_path.read_text()
email_json = json.loads(text)
assert (
f"The user '{TEST_USER_EMAIL}' (providing preferred contact email '{TEST_USER_SUPPLIED_EMAIL}') provided the following information:"
in email_json["body"]
)
assert (
f"""The user <span style="font-family: monospace;">&#39;{TEST_USER_EMAIL}&#39; (providing preferred contact email &#39;{TEST_USER_SUPPLIED_EMAIL}&#39;)</span> provided the following information:"""
in email_json["html"]
)
def _setup_model_objects(self):
user = model.User(email=TEST_USER_EMAIL, password="mockpass")
job = model.Job()
job.tool_id = "cat1"
job.history = model.History()
job.user = user
job.history.user = user
hda = model.HistoryDatasetAssociation(history=job.history)
hda.dataset = model.Dataset()
hda.dataset.state = "ok"
job.add_output_dataset("out1", hda)
self._commit_objects([job, hda, user])
return user, hda
def _setup_invocation_model_objects(self):
user = model.User(email=TEST_USER_EMAIL, password="mockpass")
invocation = model.WorkflowInvocation()
invocation.workflow = model.Workflow()
invocation.workflow.name = "Test Workflow"
invocation.workflow.stored_workflow = model.StoredWorkflow()
invocation.workflow.stored_workflow.user = user
invocation.history = model.History()
invocation.history.user = user
trans = galaxy_mock.MockTrans(app=self.app, history=invocation.history, user=user)
self._commit_objects([user, invocation])
return user, invocation, trans
def _commit_objects(self, objects):
session = self.app.model.context
session.add_all(objects)
session.commit()