-
Notifications
You must be signed in to change notification settings - Fork 0
Add tests for PDF generation #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
54d4d03
47e7bfb
395b50c
a8e5dfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ | |
] | ||
|
||
STATIC_URL = "/static/" | ||
STATIC_ROOT = BASE_DIR / "static_root" | ||
|
||
ROOT_URLCONF = "testapp.urls" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:root { | ||
--color-primary: purple; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>External URL</title> | ||
<link href="https://example.com/index.css" rel="stylesheet"> | ||
</head> | ||
<body> | ||
External URL. | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>Hello world</title> | ||
</head> | ||
<body> | ||
Hello {{ world }}. | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{% load static %} | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>Local URL</title> | ||
<link href="http://testserver/static/testapp/some.css" rel="stylesheet"> | ||
<link href="{% static 'testapp/some.css' %}" rel="stylesheet"> | ||
</head> | ||
<body> | ||
Local URL. | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% load static %} | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>Missing asset</title> | ||
<link href="/static/non_existent.css" rel="stylesheet"> | ||
</head> | ||
<body> | ||
Missing asset. | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from unittest.mock import patch | ||
|
||
from django.core.management import call_command | ||
|
||
import pytest | ||
|
||
from maykin_common.pdf import render_to_pdf | ||
|
||
|
||
def get_base_url(): | ||
return "http://testserver" | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def _collectstatic(settings, tmp_path): | ||
static_root = tmp_path / "static_root" | ||
settings.STATIC_ROOT = str(static_root) | ||
call_command("collectstatic", interactive=False, link=True, verbosity=0) | ||
yield | ||
Comment on lines
+14
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will the contents of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tried it but the |
||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def _settings(settings): | ||
settings.PDF_BASE_URL_FUNCTION = f"{__name__}.get_base_url" | ||
|
||
|
||
def test_raises_if_setting_not_configured_properly(settings): | ||
settings.PDF_BASE_URL_FUNCTION = None | ||
|
||
with pytest.raises(NotImplementedError): | ||
render_to_pdf("testapp/pdf/hello_world.html", {}) | ||
|
||
|
||
def test_render_template_returns_html(): | ||
html, pdf = render_to_pdf("testapp/pdf/hello_world.html", {"world": "pytest"}) | ||
|
||
assert isinstance(html, str) | ||
assert "Hello pytest" in html | ||
assert isinstance(pdf, bytes) | ||
|
||
|
||
def test_external_url_uses_default_resolver(): | ||
with patch("maykin_common.pdf.weasyprint.default_url_fetcher") as mock_fetcher: | ||
render_to_pdf("testapp/pdf/external_url.html", {}) | ||
|
||
mock_fetcher.assert_called_once_with("https://example.com/index.css") | ||
|
||
|
||
def test_local_asset_does_not_use_default_resolver(): | ||
with patch("maykin_common.pdf.weasyprint.default_url_fetcher") as mock_fetcher: | ||
render_to_pdf("testapp/pdf/local_url.html", {}) | ||
|
||
mock_fetcher.assert_not_called() | ||
|
||
|
||
def test_render_with_missing_asset(): | ||
with patch("maykin_common.pdf.weasyprint.default_url_fetcher") as mock_fetcher: | ||
render_to_pdf("testapp/pdf/missing_asset.html", {}) | ||
|
||
mock_fetcher.assert_called_once_with("http://testserver/static/non_existent.css") | ||
|
||
|
||
def test_resolves_assets_in_debug_mode(settings): | ||
settings.STATIC_ROOT = "/bad/path" | ||
settings.DEBUG = True | ||
|
||
with patch("maykin_common.pdf.weasyprint.default_url_fetcher") as mock_fetcher: | ||
render_to_pdf("testapp/pdf/local_url.html", {}) | ||
|
||
mock_fetcher.assert_not_called() | ||
|
||
|
||
def test_fully_qualified_static_url(settings): | ||
settings.STATIC_URL = "http://testserver/static/" | ||
|
||
with patch("maykin_common.pdf.weasyprint.default_url_fetcher") as mock_fetcher: | ||
render_to_pdf("testapp/pdf/local_url.html", {}) | ||
|
||
mock_fetcher.assert_not_called() | ||
|
||
|
||
def test_other_storages_than_file_system_storage(settings): | ||
settings.STORAGES = { | ||
"default": { | ||
"BACKEND": "django.core.files.storage.InMemoryStorage", | ||
}, | ||
"staticfiles": { | ||
# this causes the /static/ prefix to be absent (!) | ||
"BACKEND": "django.core.files.storage.InMemoryStorage", | ||
}, | ||
} | ||
|
||
with patch("maykin_common.pdf.weasyprint.default_url_fetcher") as mock_fetcher: | ||
render_to_pdf("testapp/pdf/local_url.html", {}) | ||
|
||
mock_fetcher.assert_called_with("http://testserver/testapp/some.css") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some normalisation going on? Should we consider:
I don't think so. Unsure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had the same thoughts. Given a URL with trailing
?
, it gets normalized to a string without the?
because there are no query params at all, but otherwise I think it's not relevant because in all realistic cases you configure static url to be[{scheme}{netloc}]{path}
which always acts as a prefix. So whatever may be trailing after the base url is irrelevant anyway. The check before was checking that scheme and netloc match, and then prefix match on the path.I picked the less complex option because of this context.