2016-05-24 14:06:34 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-08-09 23:01:44 +02:00
|
|
|
import os
|
2017-03-31 07:40:52 +02:00
|
|
|
import re
|
2016-06-04 20:28:02 +02:00
|
|
|
from typing import Any, Dict, Iterable
|
2016-05-24 14:06:34 +02:00
|
|
|
import logging
|
|
|
|
|
2016-05-11 19:29:29 +02:00
|
|
|
from django.test import override_settings
|
2016-05-24 14:06:34 +02:00
|
|
|
from django.template.loader import get_template
|
2018-08-25 14:06:17 +02:00
|
|
|
from django.test.client import RequestFactory
|
2016-05-24 14:06:34 +02:00
|
|
|
|
2019-08-10 18:24:59 +02:00
|
|
|
from jinja2.exceptions import UndefinedError
|
|
|
|
|
2018-12-23 00:13:57 +01:00
|
|
|
from zerver.lib.exceptions import InvalidMarkdownIncludeStatement
|
2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_helpers import get_all_templates
|
|
|
|
from zerver.lib.test_classes import (
|
|
|
|
ZulipTestCase,
|
|
|
|
)
|
2017-10-07 00:29:18 +02:00
|
|
|
from zerver.lib.test_runner import slow
|
2019-07-23 02:33:45 +02:00
|
|
|
from zerver.models import Realm
|
2016-11-15 13:41:12 +01:00
|
|
|
|
2016-05-24 14:06:34 +02:00
|
|
|
|
2017-11-05 11:49:43 +01:00
|
|
|
class get_form_value:
|
2017-11-05 10:51:25 +01:00
|
|
|
def __init__(self, value: Any) -> None:
|
2016-05-24 14:06:34 +02:00
|
|
|
self._value = value
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def value(self) -> Any:
|
2016-05-24 14:06:34 +02:00
|
|
|
return self._value
|
|
|
|
|
|
|
|
|
2017-11-02 18:36:25 +01:00
|
|
|
class DummyForm(Dict[str, Any]):
|
2016-05-24 14:06:34 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class TemplateTestCase(ZulipTestCase):
|
2016-05-24 14:06:34 +02:00
|
|
|
"""
|
|
|
|
Tests that backend template rendering doesn't crash.
|
|
|
|
|
|
|
|
This renders all the Zulip backend templates, passing dummy data
|
|
|
|
as the context, which allows us to verify whether any of the
|
|
|
|
templates are broken enough to not render at all (no verification
|
|
|
|
is done that the output looks right). Please see `get_context`
|
|
|
|
function documentation for more information.
|
|
|
|
"""
|
2017-10-07 00:29:18 +02:00
|
|
|
@slow("Tests a large number of different templates")
|
2016-05-11 19:29:29 +02:00
|
|
|
@override_settings(TERMS_OF_SERVICE=None)
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_templates(self) -> None:
|
2016-06-04 20:28:02 +02:00
|
|
|
|
2016-05-24 14:06:34 +02:00
|
|
|
# Just add the templates whose context has a conflict with other
|
2016-05-11 19:01:53 +02:00
|
|
|
# templates' context in `defer`.
|
|
|
|
defer = ['analytics/activity.html']
|
2016-12-01 08:54:21 +01:00
|
|
|
|
|
|
|
# Django doesn't send template_rendered signal for parent templates
|
|
|
|
# https://code.djangoproject.com/ticket/24622
|
|
|
|
covered = [
|
2016-11-15 13:41:12 +01:00
|
|
|
'zerver/portico.html',
|
|
|
|
'zerver/portico_signup.html',
|
|
|
|
]
|
2016-12-01 08:54:21 +01:00
|
|
|
|
|
|
|
logged_out = [
|
2017-05-13 23:57:06 +02:00
|
|
|
'zerver/compare.html',
|
2017-05-10 03:42:24 +02:00
|
|
|
'zerver/footer.html',
|
2016-12-01 08:54:21 +01:00
|
|
|
]
|
|
|
|
|
2016-11-15 13:41:12 +01:00
|
|
|
logged_in = [
|
2016-12-20 02:30:08 +01:00
|
|
|
'analytics/stats.html',
|
2017-03-09 02:30:44 +01:00
|
|
|
'zerver/landing_nav.html',
|
2016-11-15 13:41:12 +01:00
|
|
|
]
|
|
|
|
unusual = [
|
2018-12-17 18:58:43 +01:00
|
|
|
'zerver/emails/confirm_new_email.subject.txt',
|
2017-09-26 01:41:15 +02:00
|
|
|
'zerver/emails/compiled/confirm_new_email.html',
|
2017-05-01 20:28:42 +02:00
|
|
|
'zerver/emails/confirm_new_email.txt',
|
2018-12-17 18:58:43 +01:00
|
|
|
'zerver/emails/notify_change_in_email.subject.txt',
|
2017-09-26 01:41:15 +02:00
|
|
|
'zerver/emails/compiled/notify_change_in_email.html',
|
2018-12-17 18:58:43 +01:00
|
|
|
'zerver/emails/digest.subject.txt',
|
2017-05-04 06:42:23 +02:00
|
|
|
'zerver/emails/digest.txt',
|
2018-12-17 18:58:43 +01:00
|
|
|
'zerver/emails/followup_day1.subject.txt',
|
2017-09-26 01:41:15 +02:00
|
|
|
'zerver/emails/compiled/followup_day1.html',
|
2017-05-04 06:42:23 +02:00
|
|
|
'zerver/emails/followup_day1.txt',
|
2018-12-17 18:58:43 +01:00
|
|
|
'zerver/emails/followup_day2.subject.txt',
|
2017-05-04 06:42:23 +02:00
|
|
|
'zerver/emails/followup_day2.txt',
|
2017-09-26 01:41:15 +02:00
|
|
|
'zerver/emails/compiled/followup_day2.html',
|
|
|
|
'zerver/emails/compiled/password_reset.html',
|
2016-11-15 13:41:12 +01:00
|
|
|
'corporate/zephyr.html',
|
|
|
|
'corporate/zephyr-mirror.html',
|
|
|
|
'zilencer/enterprise_tos_accept_body.txt',
|
|
|
|
'zerver/zulipchat_migration_tos.html',
|
|
|
|
'zilencer/enterprise_tos_accept_body.txt',
|
2018-03-05 20:19:07 +01:00
|
|
|
'zerver/invalid_email.html',
|
2016-11-15 13:41:12 +01:00
|
|
|
'zerver/invalid_realm.html',
|
|
|
|
'zerver/debug.html',
|
|
|
|
'zerver/base.html',
|
2017-08-04 02:48:07 +02:00
|
|
|
'zerver/portico-header.html',
|
2017-07-12 09:44:50 +02:00
|
|
|
'two_factor/_wizard_forms.html',
|
2016-11-15 13:41:12 +01:00
|
|
|
]
|
2017-03-31 07:40:52 +02:00
|
|
|
|
2019-07-02 23:47:37 +02:00
|
|
|
integrations_regexp = re.compile(r'^zerver/integrations/.*\.html$')
|
2017-03-31 07:40:52 +02:00
|
|
|
|
2017-09-17 02:14:56 +02:00
|
|
|
# Since static/generated/bots/ is searched by Jinja2 for templates,
|
|
|
|
# it mistakes logo files under that directory for templates.
|
2018-07-02 00:05:24 +02:00
|
|
|
bot_logos_regexp = re.compile(r'\w+\/logo\.(svg|png)$')
|
2017-09-17 02:14:56 +02:00
|
|
|
|
2017-04-10 12:55:43 +02:00
|
|
|
skip = covered + defer + logged_out + logged_in + unusual + ['tests/test_markdown.html',
|
|
|
|
'zerver/terms.html',
|
|
|
|
'zerver/privacy.html']
|
2017-09-17 02:14:56 +02:00
|
|
|
|
2019-07-02 23:47:37 +02:00
|
|
|
all_templates = get_all_templates()
|
|
|
|
self.assertEqual(set(skip) - set(all_templates), set())
|
|
|
|
templates = [t for t in all_templates if not (
|
2017-09-17 02:14:56 +02:00
|
|
|
t in skip or integrations_regexp.match(t) or bot_logos_regexp.match(t))]
|
2016-05-24 14:06:34 +02:00
|
|
|
self.render_templates(templates, self.get_context())
|
|
|
|
|
2016-05-11 19:01:53 +02:00
|
|
|
# Test the deferred templates with updated context.
|
2016-05-24 14:06:34 +02:00
|
|
|
update = {'data': [('one', 'two')]}
|
2016-05-11 19:01:53 +02:00
|
|
|
self.render_templates(defer, self.get_context(**update))
|
2016-05-24 14:06:34 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def render_templates(self, templates: Iterable[str], context: Dict[str, Any]) -> None:
|
2016-11-15 13:41:12 +01:00
|
|
|
for template_name in templates:
|
|
|
|
template = get_template(template_name)
|
2016-05-24 14:06:34 +02:00
|
|
|
try:
|
|
|
|
template.render(context)
|
2019-08-10 18:24:59 +02:00
|
|
|
except UndefinedError as e: # nocoverage # ideally, this block shouldn't have to execute
|
|
|
|
raise UndefinedError(e.message + """\n
|
|
|
|
This test is designed to confirm that every Jinja2 template is free
|
|
|
|
of syntax errors. There are two common causes for this test failing:
|
|
|
|
|
|
|
|
* One of Zulip's HTML templates doesn't render.
|
|
|
|
* A new context variable was added to a template, without a sample
|
|
|
|
value being added to `get_context` in zerver/tests/test_templates.py.
|
|
|
|
|
|
|
|
""")
|
2017-03-05 08:23:45 +01:00
|
|
|
except Exception: # nocoverage # nicer error handler
|
2016-08-09 18:53:41 +02:00
|
|
|
logging.error("Exception while rendering '{}'".format(template.template.name))
|
|
|
|
raise
|
2016-05-24 14:06:34 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def get_context(self, **kwargs: Any) -> Dict[str, Any]:
|
2016-05-24 14:06:34 +02:00
|
|
|
"""Get the dummy context for shallow testing.
|
|
|
|
|
|
|
|
The context returned will always contain a parameter called
|
|
|
|
`shallow_tested`, which tells the signal receiver that the
|
|
|
|
test was not rendered in an actual logical test (so we can
|
|
|
|
still do coverage reporting on which templates have a logical
|
|
|
|
test).
|
|
|
|
|
|
|
|
Note: `context` just holds dummy values used to make the test
|
|
|
|
pass. This context only ensures that the templates do not
|
|
|
|
throw a 500 error when rendered using dummy data. If new
|
|
|
|
required parameters are added to a template, this test will
|
|
|
|
fail; the usual fix is to just update the context below to add
|
|
|
|
the new parameter to the dummy data.
|
|
|
|
|
|
|
|
:param kwargs: Keyword arguments can be used to update the base
|
|
|
|
context.
|
|
|
|
|
|
|
|
"""
|
2017-05-07 19:39:30 +02:00
|
|
|
user_profile = self.example_user('hamlet')
|
|
|
|
email = user_profile.email
|
2016-05-24 14:06:34 +02:00
|
|
|
|
|
|
|
context = dict(
|
2018-08-18 15:42:01 +02:00
|
|
|
sidebar_index="zerver/help/include/sidebar_index.md",
|
|
|
|
doc_root="/help/",
|
2017-04-03 11:49:13 +02:00
|
|
|
article="zerver/help/index.md",
|
2016-05-24 14:06:34 +02:00
|
|
|
shallow_tested=True,
|
|
|
|
user_profile=user_profile,
|
2016-08-21 08:19:25 +02:00
|
|
|
user=user_profile,
|
2016-05-24 14:06:34 +02:00
|
|
|
form=DummyForm(
|
|
|
|
full_name=get_form_value('John Doe'),
|
|
|
|
terms=get_form_value(True),
|
|
|
|
email=get_form_value(email),
|
2016-12-20 10:41:46 +01:00
|
|
|
emails=get_form_value(email),
|
2018-08-25 14:06:17 +02:00
|
|
|
subdomain=get_form_value("zulip"),
|
|
|
|
next_param=get_form_value("billing")
|
2016-05-24 14:06:34 +02:00
|
|
|
),
|
|
|
|
current_url=lambda: 'www.zulip.com',
|
2016-08-09 18:53:57 +02:00
|
|
|
integrations_dict={},
|
2016-05-24 14:06:34 +02:00
|
|
|
referrer=dict(
|
|
|
|
full_name='John Doe',
|
|
|
|
realm=dict(name='zulip.com'),
|
|
|
|
),
|
|
|
|
message_count=0,
|
|
|
|
messages=[dict(header='Header')],
|
|
|
|
new_streams=dict(html=''),
|
|
|
|
data=dict(title='Title'),
|
2017-01-30 23:19:38 +01:00
|
|
|
device_info={"device_browser": "Chrome",
|
|
|
|
"device_os": "Windows",
|
|
|
|
"device_ip": "127.0.0.1",
|
|
|
|
"login_time": "9:33am NewYork, NewYork",
|
|
|
|
},
|
2017-11-08 05:39:42 +01:00
|
|
|
api_uri_context={},
|
2019-07-23 02:33:45 +02:00
|
|
|
realm_plan_type=Realm.LIMITED,
|
2018-08-07 15:37:22 +02:00
|
|
|
cloud_annual_price=80,
|
|
|
|
seat_count=8,
|
2019-01-17 13:07:40 +01:00
|
|
|
request=RequestFactory().get("/"),
|
|
|
|
invite_as={"MEMBER": 1},
|
2019-05-04 20:24:36 +02:00
|
|
|
max_file_upload_size = 25,
|
2019-08-03 14:15:32 +02:00
|
|
|
avatar_urls={"john@gmail.com": "www.zulip.com"},
|
2016-05-24 14:06:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
context.update(kwargs)
|
|
|
|
return context
|
2016-05-11 19:01:53 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_markdown_in_template(self) -> None:
|
2016-05-11 19:01:53 +02:00
|
|
|
template = get_template("tests/test_markdown.html")
|
|
|
|
context = {
|
|
|
|
'markdown_test_file': "zerver/tests/markdown/test_markdown.md"
|
|
|
|
}
|
|
|
|
content = template.render(context)
|
|
|
|
|
|
|
|
content_sans_whitespace = content.replace(" ", "").replace('\n', '')
|
|
|
|
self.assertEqual(content_sans_whitespace,
|
2016-11-09 21:52:42 +01:00
|
|
|
'header<h1id="hello">Hello!</h1><p>Thisissome<em>boldtext</em>.</p>footer')
|
2016-05-11 19:29:29 +02:00
|
|
|
|
2018-09-07 00:09:51 +02:00
|
|
|
def test_markdown_tabbed_sections_extension(self) -> None:
|
|
|
|
template = get_template("tests/test_markdown.html")
|
|
|
|
context = {
|
|
|
|
'markdown_test_file': "zerver/tests/markdown/test_tabbed_sections.md"
|
|
|
|
}
|
|
|
|
content = template.render(context)
|
|
|
|
content_sans_whitespace = content.replace(" ", "").replace('\n', '')
|
|
|
|
|
|
|
|
# Note that the expected HTML has a lot of stray <p> tags. This is a
|
|
|
|
# consequence of how the Markdown renderer converts newlines to HTML
|
|
|
|
# and how elements are delimited by newlines and so forth. However,
|
|
|
|
# stray <p> tags are usually matched with closing tags by HTML renderers
|
|
|
|
# so this doesn't affect the final rendered UI in any visible way.
|
|
|
|
expected_html = """
|
|
|
|
header
|
|
|
|
|
|
|
|
<h1 id="heading">Heading</h1>
|
|
|
|
<p>
|
2019-02-08 04:55:50 +01:00
|
|
|
<div class="code-section has-tabs" markdown="1">
|
2018-09-07 00:09:51 +02:00
|
|
|
<ul class="nav">
|
|
|
|
<li data-language="ios">iOS</li>
|
|
|
|
<li data-language="desktop-web">Desktop/Web</li>
|
|
|
|
</ul>
|
|
|
|
<div class="blocks">
|
|
|
|
<div data-language="ios" markdown="1"></p>
|
|
|
|
<p>iOS instructions</p>
|
|
|
|
<p></div>
|
|
|
|
<div data-language="desktop-web" markdown="1"></p>
|
|
|
|
<p>Desktop/browser instructions</p>
|
|
|
|
<p></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<h2 id="heading-2">Heading 2</h2>
|
|
|
|
<p>
|
2019-02-08 04:55:50 +01:00
|
|
|
<div class="code-section has-tabs" markdown="1">
|
2018-09-07 00:09:51 +02:00
|
|
|
<ul class="nav">
|
|
|
|
<li data-language="desktop-web">Desktop/Web</li>
|
|
|
|
<li data-language="android">Android</li>
|
|
|
|
</ul>
|
|
|
|
<div class="blocks">
|
|
|
|
<div data-language="desktop-web" markdown="1"></p>
|
|
|
|
<p>Desktop/browser instructions</p>
|
|
|
|
<p></div>
|
|
|
|
<div data-language="android" markdown="1"></p>
|
|
|
|
<p>Android instructions</p>
|
|
|
|
<p></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</p>
|
|
|
|
|
2019-02-08 04:55:50 +01:00
|
|
|
<h2 id="heading-3">Heading 3</h2>
|
|
|
|
<p>
|
|
|
|
<div class="code-section no-tabs" markdown="1">
|
|
|
|
<ul class="nav">
|
|
|
|
<li data-language="null_tab">None</li>
|
|
|
|
</ul>
|
|
|
|
<div class="blocks">
|
|
|
|
<div data-language="null_tab" markdown="1"></p>
|
|
|
|
<p>Instructions for all platforms</p>
|
|
|
|
<p></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</p>
|
|
|
|
|
2018-09-07 00:09:51 +02:00
|
|
|
footer
|
|
|
|
"""
|
|
|
|
|
|
|
|
expected_html_sans_whitespace = expected_html.replace(" ", "").replace('\n', '')
|
|
|
|
self.assertEqual(content_sans_whitespace,
|
|
|
|
expected_html_sans_whitespace)
|
|
|
|
|
2018-07-07 22:14:30 +02:00
|
|
|
def test_markdown_nested_code_blocks(self) -> None:
|
|
|
|
template = get_template("tests/test_markdown.html")
|
|
|
|
context = {
|
|
|
|
'markdown_test_file': "zerver/tests/markdown/test_nested_code_blocks.md"
|
|
|
|
}
|
|
|
|
content = template.render(context)
|
|
|
|
|
|
|
|
content_sans_whitespace = content.replace(" ", "").replace('\n', '')
|
|
|
|
expected = ('header<h1id="this-is-a-heading">Thisisaheading.</h1><ol>'
|
|
|
|
'<li><p>Alistitemwithanindentedcodeblock:</p><divclass="codehilite">'
|
|
|
|
'<pre>indentedcodeblockwithmultiplelines</pre></div></li></ol>'
|
|
|
|
'<divclass="codehilite"><pre><span></span>'
|
|
|
|
'non-indentedcodeblockwithmultiplelines</pre></div>footer')
|
|
|
|
self.assertEqual(content_sans_whitespace, expected)
|
|
|
|
|
2018-12-23 00:13:57 +01:00
|
|
|
def test_custom_markdown_include_extension(self) -> None:
|
|
|
|
template = get_template("tests/test_markdown.html")
|
|
|
|
context = {
|
|
|
|
'markdown_test_file': "zerver/tests/markdown/test_custom_include_extension.md"
|
|
|
|
}
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(InvalidMarkdownIncludeStatement, "Invalid markdown include statement"):
|
|
|
|
template.render(context)
|
|
|
|
|
|
|
|
def test_custom_markdown_include_extension_empty_macro(self) -> None:
|
|
|
|
template = get_template("tests/test_markdown.html")
|
|
|
|
context = {
|
|
|
|
'markdown_test_file': "zerver/tests/markdown/test_custom_include_extension_empty.md"
|
|
|
|
}
|
|
|
|
content = template.render(context)
|
|
|
|
content_sans_whitespace = content.replace(" ", "").replace('\n', '')
|
|
|
|
expected = 'headerfooter'
|
|
|
|
self.assertEqual(content_sans_whitespace, expected)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_custom_tos_template(self) -> None:
|
2016-05-11 19:29:29 +02:00
|
|
|
response = self.client_get("/terms/")
|
2016-11-19 21:54:00 +01:00
|
|
|
|
|
|
|
self.assert_in_success_response([u"Thanks for using our products and services (\"Services\"). ",
|
|
|
|
u"By using our Services, you are agreeing to these terms"],
|
|
|
|
response)
|
2017-04-10 12:55:43 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_custom_terms_of_service_template(self) -> None:
|
2017-04-12 23:40:38 +02:00
|
|
|
not_configured_message = 'This installation of Zulip does not have a configured ' \
|
|
|
|
'terms of service'
|
2017-04-10 12:55:43 +02:00
|
|
|
with self.settings(TERMS_OF_SERVICE=None):
|
|
|
|
response = self.client_get('/terms/')
|
|
|
|
self.assert_in_success_response([not_configured_message], response)
|
|
|
|
with self.settings(TERMS_OF_SERVICE='zerver/tests/markdown/test_markdown.md'):
|
|
|
|
response = self.client_get('/terms/')
|
|
|
|
self.assert_in_success_response(['This is some <em>bold text</em>.'], response)
|
|
|
|
self.assert_not_in_success_response([not_configured_message], response)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_custom_privacy_policy_template(self) -> None:
|
2017-04-12 23:40:38 +02:00
|
|
|
not_configured_message = 'This installation of Zulip does not have a configured ' \
|
|
|
|
'privacy policy'
|
2017-04-10 12:55:43 +02:00
|
|
|
with self.settings(PRIVACY_POLICY=None):
|
|
|
|
response = self.client_get('/privacy/')
|
|
|
|
self.assert_in_success_response([not_configured_message], response)
|
|
|
|
with self.settings(PRIVACY_POLICY='zerver/tests/markdown/test_markdown.md'):
|
|
|
|
response = self.client_get('/privacy/')
|
|
|
|
self.assert_in_success_response(['This is some <em>bold text</em>.'], response)
|
|
|
|
self.assert_not_in_success_response([not_configured_message], response)
|
2018-08-09 23:01:44 +02:00
|
|
|
|
|
|
|
def test_custom_privacy_policy_template_with_absolute_url(self) -> None:
|
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
abs_path = os.path.join(current_dir, '..', '..',
|
|
|
|
'templates/zerver/tests/markdown/test_markdown.md')
|
|
|
|
with self.settings(PRIVACY_POLICY=abs_path):
|
|
|
|
response = self.client_get('/privacy/')
|
|
|
|
self.assert_in_success_response(['This is some <em>bold text</em>.'], response)
|