aurweb/test/test_templates.py
Kevin Morris 0d734eb07d
feat(fastapi): add configurable commit hash display
Two new options have been added:

- [devel] commit_url
    - URL including an %s format specifier that can be used to link
      to a webpage for the commit.
- [devel] commit_hash
    - HEAD's commit hash (produced via `git rev-parse HEAD`)

If a `[devel] commit_hash` is configured, a link to the commit based on
`[devel] commit_url` will be displayed in the aurweb footer in
the form: `HEAD@<commit_hash>`. If no `[devel] commit_url` is
configured, a non-linked hash will be displayed.

Signed-off-by: Kevin Morris <kevr@0cost.org>
2021-10-24 18:12:19 -07:00

72 lines
2.4 KiB
Python

import pytest
from aurweb import config, templates
from aurweb.templates import register_filter, register_function
from aurweb.testing.requests import Request
@register_filter("func")
def func():
pass
@register_function("function")
def function():
pass
def test_register_filter_exists_key_error():
""" Most instances of register_filter are tested through module
imports or template renders, so we only test failures here. """
with pytest.raises(KeyError):
@register_filter("func")
def some_func():
pass
def test_register_function_exists_key_error():
""" Most instances of register_filter are tested through module
imports or template renders, so we only test failures here. """
with pytest.raises(KeyError):
@register_function("function")
def some_func():
pass
def test_commit_hash():
# Hashes we'll use for this test. long_commit_hash should be
# shortened to commit_hash for rendering.
commit_hash = "abcdefg"
long_commit_hash = commit_hash + "1234567"
def config_get_with_fallback(section: str, option: str,
fallback: str = None) -> str:
if section == "devel" and option == "commit_hash":
return long_commit_hash
return config.original_get_with_fallback(section, option, fallback)
# Fake config.get_with_fallback.
config.original_get_with_fallback = config.get_with_fallback
config.get_with_fallback = config_get_with_fallback
request = Request()
context = templates.make_context(request, "Test Context")
render = templates.render_raw_template(request, "index.html", context)
# We've faked config.get_with_fallback to return a "valid" commit_hash
# when queried. Test that the expected render occurs.
commit_url = config.get("devel", "commit_url")
expected = commit_url % commit_hash
assert expected in render
assert f"HEAD@{commit_hash}" in render
assert long_commit_hash not in render
# Restore config.get_with_fallback.
config.get_with_fallback = config.original_get_with_fallback
config.original_get_with_fallback = None
# Now, we no longer fake the commit_hash option: no commit
# is displayed in the footer. Assert this expectation.
context = templates.make_context(request, "Test Context")
render = templates.render_raw_template(request, "index.html", context)
assert commit_hash not in render