import jingrow
from jingrow.core.pagetype.pagetype.test_pagetype import new_pagetype
from jingrow.tests.utils import JingrowTestCase
from jingrow.www.printview import get_html_and_style


class PrintViewTest(JingrowTestCase):
	def test_print_view_without_errors(self):
		user = jingrow.get_last_pg("User")

		messages_before = jingrow.get_message_log()
		ret = get_html_and_style(pg=user.as_json(), print_format="Standard", no_letterhead=1)
		messages_after = jingrow.get_message_log()

		if len(messages_after) > len(messages_before):
			new_messages = messages_after[len(messages_before) :]
			self.fail("Print view showing error/warnings: \n" + "\n".join(str(msg) for msg in new_messages))

		# html should exist
		self.assertTrue(bool(ret["html"]))

	def test_print_error(self):
		"""Print failures shouldn't generate PDF with failure message but instead escalate the error"""
		pagetype = new_pagetype(is_submittable=1).insert()

		pg = jingrow.new_pg(pagetype.name)
		pg.insert()
		pg.submit()
		pg.cancel()

		# cancelled pg can't be printed by default
		self.assertRaises(jingrow.PermissionError, jingrow.attach_print, pg.pagetype, pg.name)
