# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import io
import unittest
from PyPDF2 import PdfFileReader
import frappe
import frappe.utils.pdf as pdfgen
class TestPdf(unittest.TestCase):
@property
def html(self):
return """
This is a test html snippet
"""
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'] == '10')
self.assertTrue(html_options['margin-right'] == '0')
def test_pdf_encryption(self):
password = "qwe"
pdf = pdfgen.get_pdf(self.html, options={"password": password})
reader = PdfFileReader(io.BytesIO(pdf))
self.assertTrue(reader.isEncrypted)
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)