bito-code-review[bot] commented on code in PR #35014:
URL: https://github.com/apache/superset/pull/35014#discussion_r2321513044
##########
tests/unit_tests/reports/notifications/email_tests.py:
##########
@@ -98,3 +103,173 @@ def test_email_subject_with_datetime() -> None:
)._get_subject()
assert datetime_pattern not in subject
assert now.strftime(datetime_pattern) in subject
+
+
+@patch('superset.reports.notifications.email.is_feature_enabled')
+@patch('superset.reports.notifications.email.CSS')
+@patch('superset.reports.notifications.email.HTML')
+@patch('superset.reports.notifications.email.app')
+def test_pdf_generation_triggered_and_content(mock_app, mock_html, mock_css,
mock_is_feature_enabled):
+ mock_is_feature_enabled.return_value = False # For self._name simplicity
+ mock_app.config.get.side_effect = get_app_config_mock(DEFAULT_PDF_CONFIG)
+
+ mock_pdf_bytes = b"mock_pdf_content"
+ mock_html_instance = MagicMock()
+ mock_html_instance.write_pdf.return_value = mock_pdf_bytes
+ mock_html.return_value = mock_html_instance
+
+ mock_content = MagicMock(spec=NotificationContent)
+ mock_content.name = "Test Report Name"
+ mock_content.description = "Test PDF Description"
+ mock_content.embedded_data = pd.DataFrame({'col1': [10, 20], 'col2': ['X',
'Y']})
+ mock_content.screenshots = None
+ mock_content.csv = None
+ mock_content.pdf = None
+ mock_content.text = None
+ mock_content.report_format = "PDF"
+ mock_content.url = "http://superset.example.com"
+ mock_content.header_data = {}
+
+ notification = EmailNotification(recipient=MagicMock(),
content=mock_content)
+ email_content_result = notification._get_content()
+
+ mock_html.assert_called_once()
+ args_html, kwargs_html = mock_html.call_args
+ html_passed_to_weasyprint = kwargs_html.get('string', args_html[0] if
args_html else '')
+ assert '<div class="report-description">Test PDF Description</div>' in
html_passed_to_weasyprint
+ assert "<td>X</td>" in html_passed_to_weasyprint # Check for
df.to_html(..., escape=False)
+ assert ":root" in html_passed_to_weasyprint # Check for CSS variables block
+ assert f'--report-name-var: "{mock_content.name}";' in
html_passed_to_weasyprint
+
+ mock_css.assert_called_once()
+ args_css, kwargs_css = mock_css.call_args
+ css_string_passed = kwargs_css.get('string', args_css[0] if args_css else
'')
+ assert "@page { size: A4 portrait; margin: 2.5cm 1.5cm 2cm 1.5cm; }" in
css_string_passed
+ assert 'body { font-family: sans-serif; }' in css_string_passed
+
+ assert email_content_result.pdf == {f"{mock_content.name}.pdf":
mock_pdf_bytes}
+ # Check that html_table is not included in the email body
+ assert "<table" not in email_content_result.body
+ assert mock_content.description not in email_content_result.body #
Description should be in PDF
+
+@patch('superset.reports.notifications.email.is_feature_enabled')
+@patch('superset.reports.notifications.email.CSS')
+@patch('superset.reports.notifications.email.HTML')
+@patch('superset.reports.notifications.email.app')
+def test_pdf_headers_footers_enabled(mock_app, mock_html, mock_css,
mock_is_feature_enabled):
+ mock_is_feature_enabled.return_value = False
+ config = {**DEFAULT_PDF_CONFIG, "PDF_EXPORT_HEADERS_FOOTERS_ENABLED": True}
+ mock_app.config.get.side_effect = get_app_config_mock(config)
+
+ mock_html_instance = MagicMock()
+ mock_html_instance.write_pdf.return_value = b"pdf"
+ mock_html.return_value = mock_html_instance
+
+ mock_content = MagicMock(spec=NotificationContent, name="My Report",
description="Desc", report_format="PDF", embedded_data=pd.DataFrame({'A': [1]}))
+ mock_content.url = "http://superset.example.com"
+ mock_content.header_data = {}
+
+
+ notification = EmailNotification(recipient=MagicMock(),
content=mock_content)
+ notification._get_content()
+
+ mock_css.assert_called_once()
+ args_css, _ = mock_css.call_args
+ css_string = args_css[0]
+
+ expected_header_content =
config["PDF_EXPORT_HEADER_TEMPLATE"].replace("{report_name}", "My Report")
+ expected_header_content = expected_header_content.replace("{page_number}",
"counter(page)")
+ expected_header_content = expected_header_content.replace("{total_pages}",
"counter(pages)")
+
+ expected_footer_content =
config["PDF_EXPORT_FOOTER_TEMPLATE"].replace("{generation_date}",
notification.now.strftime('%Y-%m-%d %H:%M:%S UTC'))
+ expected_footer_content = expected_footer_content.replace("{report_name}",
"My Report")
+
+ assert f'@page @top-center {{ content: "{expected_header_content}";
font-size: 9pt; color: #333; }}' in css_string
+ assert f'@page @bottom-center {{ content: "{expected_footer_content}";
font-size: 9pt; color: #333; }}' in css_string
+
+@patch('superset.reports.notifications.email.is_feature_enabled')
+@patch('superset.reports.notifications.email.CSS')
+@patch('superset.reports.notifications.email.HTML')
+@patch('superset.reports.notifications.email.app')
+def test_pdf_headers_footers_disabled(mock_app, mock_html, mock_css,
mock_is_feature_enabled):
+ mock_is_feature_enabled.return_value = False
+ config = {**DEFAULT_PDF_CONFIG, "PDF_EXPORT_HEADERS_FOOTERS_ENABLED":
False}
+ mock_app.config.get.side_effect = get_app_config_mock(config)
+
+ mock_html_instance = MagicMock()
+ mock_html_instance.write_pdf.return_value = b"pdf"
+ mock_html.return_value = mock_html_instance
+
+ mock_content = MagicMock(spec=NotificationContent, name="My Report",
description="Desc", report_format="PDF", embedded_data=pd.DataFrame({'A': [1]}))
+ mock_content.url = "http://superset.example.com"
+ mock_content.header_data = {}
+
+ notification = EmailNotification(recipient=MagicMock(),
content=mock_content)
+ notification._get_content()
+
+ mock_css.assert_called_once()
+ args_css, _ = mock_css.call_args
+ css_string = args_css[0]
+ assert '@page @top-center { content: ""; }' in css_string # Check one,
assume others are similar
+ assert '@page @bottom-center { content: ""; }' in css_string
+
+
+@patch('superset.reports.notifications.email.is_feature_enabled')
+@patch('superset.reports.notifications.email.CSS')
+@patch('superset.reports.notifications.email.HTML')
+@patch('superset.reports.notifications.email.app')
+def test_pdf_page_size_orientation(mock_app, mock_html, mock_css,
mock_is_feature_enabled):
+ mock_is_feature_enabled.return_value = False
+ config = {**DEFAULT_PDF_CONFIG, "PDF_EXPORT_PAGE_SIZE": "Letter",
"PDF_EXPORT_ORIENTATION": "landscape"}
+ mock_app.config.get.side_effect = get_app_config_mock(config)
+
+ mock_html_instance = MagicMock()
+ mock_html_instance.write_pdf.return_value = b"pdf"
+ mock_html.return_value = mock_html_instance
+
+ mock_content = MagicMock(spec=NotificationContent, name="My Report",
description="Desc", report_format="PDF", embedded_data=pd.DataFrame({'A': [1]}))
+ mock_content.url = "http://superset.example.com"
+ mock_content.header_data = {}
+
+ notification = EmailNotification(recipient=MagicMock(),
content=mock_content)
+ notification._get_content()
+
+ mock_css.assert_called_once()
+ args_css, _ = mock_css.call_args
+ css_string = args_css[0]
+ assert "@page { size: Letter landscape; margin: 2.5cm 1.5cm 2cm 1.5cm; }"
in css_string
+
+@patch('superset.reports.notifications.email.is_feature_enabled')
+@patch('superset.reports.notifications.email.CSS')
+@patch('superset.reports.notifications.email.HTML')
+@patch('superset.reports.notifications.email.app')
+def test_fallback_to_html_when_not_pdf_format(mock_app, mock_html, mock_css,
mock_is_feature_enabled):
+ mock_is_feature_enabled.return_value = False
+ mock_app.config.get.side_effect = get_app_config_mock(DEFAULT_PDF_CONFIG)
+
+ mock_content = MagicMock(spec=NotificationContent)
+ mock_content.name = "HTML Report"
+ mock_content.description = "HTML Description"
+ mock_content.embedded_data = pd.DataFrame({'col1': [1], 'col2': ['A']})
+ mock_content.screenshots = None
+ mock_content.csv = None
+ mock_content.pdf = None
+ mock_content.text = None
+ mock_content.report_format = "PNG" # Not PDF
+ mock_content.url = "http://superset.example.com"
+ mock_content.header_data = {}
+
+
+ notification = EmailNotification(recipient=MagicMock(),
content=mock_content)
+ email_content_result = notification._get_content()
+
+ mock_html.assert_not_called()
+ mock_css.assert_not_called()
+
+ assert email_content_result.pdf is None
+ assert "HTML Description" in email_content_result.body
+ assert "<table" in email_content_result.body # HTML table should be in body
+ # Check that pandas escapes HTML by default
+ mock_content.embedded_data = pd.DataFrame({'col1':
['<script>alert(1)</script>']})
+ email_content_result_escaped = notification._get_content()
Review Comment:
<div>
<div id="suggestion">
<div id="issue"><b>Test logic error in HTML escaping validation</b></div>
<div id="fix">
Test logic error: The test modifies `mock_content.embedded_data` after
creating the `EmailNotification` instance but expects the second
`_get_content()` call to use the new data. Create a new `EmailNotification`
instance with the modified content to properly test HTML escaping.
</div>
<details>
<summary>
<b>Code suggestion</b>
</summary>
<blockquote>Check the AI-generated fix before applying</blockquote>
<div id="code">
```suggestion
# Check that pandas escapes HTML by default
mock_content.embedded_data = pd.DataFrame({'col1':
['<script>alert(1)</script>']})
notification_escaped = EmailNotification(recipient=MagicMock(),
content=mock_content)
email_content_result_escaped = notification_escaped._get_content()
```
</div>
</details>
</div>
<small><i>Code Review Run <a
href=https://github.com/apache/superset/pull/35014#issuecomment-3252891874>#28dabd</a></i></small>
</div>
---
Should Bito avoid suggestions like this for future reviews? (<a
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
- [ ] Yes, avoid them
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]