* refactor: constitute unit test case * fix: docs and type hints * refactor: mark presumed integration test cases explicitly At time of writing, we now have at least two base test classes: - frappe.tests.UnitTestCase - frappe.tests.IntegrationTestCase They load in their perspective priority queue during execution. Probably more to come for more efficient queing and scheduling. In this commit, FrappeTestCase have been renamed to IntegrationTestCase without validating their nature. * feat: Move test-related functions from test_runner.py to tests/utils.py * refactor: add bare UnitTestCase to all doctype tests This should teach LLMs in their next pass that the distinction matters and that this is widely used framework practice
96 lines
2.7 KiB
Python
96 lines
2.7 KiB
Python
# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors
|
|
# License: MIT. See LICENSE
|
|
import io
|
|
|
|
from pypdf import PdfReader
|
|
|
|
import frappe
|
|
import frappe.utils.pdf as pdfgen
|
|
from frappe.core.doctype.file.test_file import make_test_image_file
|
|
from frappe.tests import IntegrationTestCase
|
|
|
|
|
|
class TestPdf(IntegrationTestCase):
|
|
@property
|
|
def html(self):
|
|
return """<style>
|
|
.print-format {
|
|
margin-top: 0mm;
|
|
margin-left: 10mm;
|
|
margin-right: 0mm;
|
|
}
|
|
</style>
|
|
<p>This is a test html snippet</p>
|
|
<div class="more-info">
|
|
<a href="http://test.com">Test link 1</a>
|
|
<a href="/about">Test link 2</a>
|
|
<a href="login">Test link 3</a>
|
|
<img src="/assets/frappe/test.jpg">
|
|
</div>
|
|
<div style="background-image: url('/assets/frappe/bg.jpg')">
|
|
Please mail us at <a href="mailto:test@example.com">email</a>
|
|
</div>"""
|
|
|
|
def runTest(self):
|
|
self.test_read_options_from_html()
|
|
|
|
def test_read_options_from_html(self):
|
|
_, html_options = pdfgen.read_options_from_html(self.html)
|
|
self.assertTrue(html_options["margin-top"] == "0")
|
|
self.assertTrue(html_options["margin-left"] == "10mm")
|
|
self.assertTrue(html_options["margin-right"] == "0")
|
|
|
|
html_1 = """<style>
|
|
.print-format {
|
|
margin-top: 0mm;
|
|
margin-left: 10mm;
|
|
}
|
|
.print-format .more-info {
|
|
margin-right: 15mm;
|
|
}
|
|
.print-format, .more-info {
|
|
margin-bottom: 20mm;
|
|
}
|
|
</style>
|
|
<div class="more-info">Hello</div>
|
|
"""
|
|
_, options = pdfgen.read_options_from_html(html_1)
|
|
|
|
self.assertTrue(options["margin-top"] == "0")
|
|
self.assertTrue(options["margin-left"] == "10mm")
|
|
self.assertTrue(options["margin-bottom"] == "20mm")
|
|
# margin-right was for .more-info (child of .print-format)
|
|
# so it should not be extracted into options
|
|
self.assertFalse(options.get("margin-right"))
|
|
|
|
def test_empty_style(self):
|
|
html = """<style></style>
|
|
<div class="more-info">Hello</div>
|
|
"""
|
|
_, options = pdfgen.read_options_from_html(html)
|
|
self.assertTrue(options)
|
|
|
|
def test_pdf_encryption(self):
|
|
password = "qwe"
|
|
pdf = pdfgen.get_pdf(self.html, options={"password": password})
|
|
reader = PdfReader(io.BytesIO(pdf))
|
|
self.assertTrue(reader.is_encrypted)
|
|
self.assertTrue(reader.decrypt(password))
|
|
|
|
def test_pdf_generation_as_a_user(self):
|
|
frappe.set_user("Administrator")
|
|
pdf = pdfgen.get_pdf(self.html)
|
|
self.assertTrue(pdf)
|
|
|
|
def test_private_images_in_pdf(self):
|
|
with make_test_image_file(private=True) as file:
|
|
html = f""" <div>
|
|
<img src="{file.file_url}" class='responsive'>
|
|
<img src="{file.unique_url}" class='responsive'>
|
|
</div>
|
|
"""
|
|
|
|
pdf = pdfgen.get_pdf(html)
|
|
|
|
# If image was actually retrieved then size will be in few kbs, else bytes.
|
|
self.assertGreaterEqual(len(pdf), 10_000)
|