Skip to content

Commit

Permalink
Add test to check email subject templates do NOT contain newlines (#1…
Browse files Browse the repository at this point in the history
…5651)

* Add test to check that email subject blocks don't have newlines

* Remove newline in subject block caught by new test

---------

Co-authored-by: Dustin Ingram <di@users.noreply.github.com>
  • Loading branch information
facutuesca and di committed Mar 22, 2024
1 parent 244cad5 commit 081dffd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
25 changes: 25 additions & 0 deletions tests/functional/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import re

from pathlib import Path

import pytest
Expand Down Expand Up @@ -38,6 +40,11 @@
"canonicalize_name": "packaging.utils:canonicalize_name",
}

# A compiled regex that matches a subject block, possibly with newlines inside
SUBJECT_BLOCK_EXPRESSION = re.compile(
r"\{% block subject %}.*\{% endblock %}", re.DOTALL
)


@pytest.mark.parametrize(
"template",
Expand Down Expand Up @@ -103,3 +110,21 @@ def test_render_templates(template):
env.filters.update(FILTERS)

assert env.get_template(str(template))


@pytest.mark.parametrize(
"template",
[f for f in Path(warehouse.__path__[0]).glob("templates/email/**/subject.txt")],
)
def test_email_subjects_for_multiple_lines(template: Path):
"""
Test if all email subject templates don't contain new lines. See
https://github.com/pypi/warehouse/issues/13216
"""

with template.open("r") as f:
match = SUBJECT_BLOCK_EXPRESSION.search(f.read())
# There should be a subject block inside a subject template file
assert match is not None
# There should NOT be a newline inside the subject block
assert "\n" not in match.group(0)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@

{% extends "email/_base/subject.txt" %}

{% block subject %}
Pending OpenID Connect publisher invalidated for {{ project_name }}
{% endblock %}
{% block subject %}Pending OpenID Connect publisher invalidated for {{ project_name }}{% endblock %}

0 comments on commit 081dffd

Please sign in to comment.