Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/functional/proxy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,19 @@ def test_we_do_not_have_a_csp_header(self, proxied_content):
policy = proxied_content.headers.get("Content-Security-Policy", "*MISSING*")

assert policy == "*MISSING*"

@pytest.mark.parametrize(
"link_mode,expected",
(
("new-tab", "new-tab"),
(None, "same-tab"),
),
)
def test_we_set_external_link_mode(self, app, link_mode, expected):
url = "/proxy/http://localhost:8080/"
if link_mode is not None:
url += f"?via.external_link_mode={link_mode}"

proxied_content = app.get(url)

assert f'setupExternalLinkHandler("{expected}");' in proxied_content
19 changes: 19 additions & 0 deletions tests/unit/viahtml/hooks/hooks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TestHooks:
def test_template_vars(self, hooks):
assert hooks.template_vars == {
"client_params": Any.function(),
"external_link_mode": Any.function(),
"ignore_prefixes": hooks.ignore_prefixes,
}

Expand All @@ -23,6 +24,24 @@ def test_client_params_in_template_vars(self, hooks):
assert params == "client"
get_config.assert_called_once_with(sentinel.http_env)

@pytest.mark.parametrize(
"link_mode,expected",
(
("new-tab", "new-tab"),
("same-tab", "same-tab"),
(None, "same-tab"),
("random", "random"),
),
)
def test_external_link_mode_in_template_vars(self, hooks, link_mode, expected):
http_env = {}
if link_mode is not None:
http_env["QUERY_STRING"] = f"via.external_link_mode={link_mode}"

external_link_mode = hooks.template_vars["external_link_mode"]

assert external_link_mode(http_env) == expected

def test_ignore_prefixes(self, hooks):
assert hooks.ignore_prefixes == sentinel.prefixes

Expand Down
11 changes: 10 additions & 1 deletion viahtml/hooks/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ def __init__(self, config):
def template_vars(self):
"""Get variables to make available in the global Jinja2 environment."""

template_vars = {"client_params": lambda http_env: self.get_config(http_env)[1]}
def external_link_mode(http_env):
via_config, _ = self.get_config(http_env)
return via_config.get("external_link_mode", "same-tab").lower()

template_vars = {
# It would be much nicer to calculate this once somehow
"client_params": lambda http_env: self.get_config(http_env)[1],
"external_link_mode": external_link_mode,
}

template_vars.update(self.config)

# This is already in the config, but run through the property just in
Expand Down
2 changes: 2 additions & 0 deletions viahtml/templates/banner.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,7 @@
var embed_script = document.createElement("script");
embed_script.src = "{{ h_embed_url }}";
document.head.appendChild(embed_script);

setupExternalLinkHandler({{external_link_mode(env) | tojson}});
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd copy pasted the code that actually does the work for this above from Via ages ago. We just need to turn it on.

The tojson here will convert our python string into a well formatted javascript string, and prevents any nonsense from leaking out.

})();
</script>