/usr/lib/python3/dist-packages/plainbox/impl/exporter/test_html.py is in python3-plainbox 0.5.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | # This file is part of Checkbox.
#
# Copyright 2013 Canonical Ltd.
# Written by:
# Sylvain Pineau <sylvain.pineau@canonical.com>
# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
# Daniel Manrique <daniel.manrique@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
"""
plainbox.impl.exporter.test_html
================================
Test definitions for plainbox.impl.exporter.html module
"""
from io import StringIO
from string import Template
from unittest import TestCase
import io
from lxml import etree as ET
from pkg_resources import resource_filename
from pkg_resources import resource_string
from plainbox.testing_utils import resource_json
from plainbox.impl.exporter.html import HTMLResourceInliner
from plainbox.impl.exporter.html import HTMLSessionStateExporter
class HTMLInlinerTests(TestCase):
def setUp(self):
template_substitutions = {
'PLAINBOX_ASSETS': resource_filename("plainbox", "data/")}
test_file_location = "test-data/html-exporter/html-inliner.html"
test_file = resource_filename("plainbox",
test_file_location)
with open(test_file) as html_file:
html_template = Template(html_file.read())
html_content = html_template.substitute(template_substitutions)
self.tree = ET.parse(StringIO(html_content), ET.HTMLParser())
# Now self.tree contains a tree with adequately-substituted
# paths and resources
inliner = HTMLResourceInliner()
self.inlined_tree = inliner.inline_resources(self.tree)
def test_script_inlining(self):
"""Test that a <script> resource gets inlined."""
for node in self.inlined_tree.xpath('//script'):
self.assertTrue(node.text)
def test_img_inlining(self):
"""
Test that a <img> gets inlined.
It should be replaced by a base64 representation of the
referenced image's data as per RFC2397.
"""
for node in self.inlined_tree.xpath('//img'):
# Skip image that purposefully points to a remote
# resource
if node.attrib.get('class') != "remote_resource":
self.assertTrue("base64" in node.attrib['src'])
def test_css_inlining(self):
"""Test that a <style> resource gets inlined."""
for node in self.inlined_tree.xpath('//style'):
# Skip a fake remote_resource node that's purposefully
# not inlined
if not 'nonexistent_resource' in node.attrib['type']:
self.assertTrue("body" in node.text)
def test_remote_resource_inlining(self):
"""
Test that a resource with a non-local (i.e. not file://
url) does NOT get inlined (rather it's replaced by an
empty string). We use <style> in this test.
"""
for node in self.inlined_tree.xpath('//style'):
# The not-inlined remote_resource
if 'nonexistent_resource' in node.attrib['type']:
self.assertTrue(node.text == "")
def test_unfindable_file_inlining(self):
"""
Test that a resource whose file does not exist does NOT
get inlined, and is instead replaced by empty string.
We use <img> in this test.
"""
for node in self.inlined_tree.xpath('//img'):
if node.attrib.get('class') == "remote_resource":
self.assertEqual("", node.attrib['src'])
class HTMLExporterTests(TestCase):
def setUp(self):
data = resource_json(
"plainbox", "test-data/xml-exporter/example-data.json",
exact=True)
exporter = HTMLSessionStateExporter(
system_id="",
timestamp="2012-12-21T12:00:00",
client_version="1.0")
stream = io.BytesIO()
exporter.dump(data, stream)
self.actual_result = stream.getvalue() # This is bytes
self.assertIsInstance(self.actual_result, bytes)
def test_html_output(self):
"""
Test that output from the exporter is HTML (or at least,
appears to be).
"""
# A pretty simplistic test since we just validate the output
# appears to be HTML. Looking at the exporter's code, it's mostly
# boilerplate use of lxml and etree, so let's not fall into testing
# an external library.
self.assertIn(b"<html>",
self.actual_result)
self.assertIn(b"<title>System Testing Report</title>",
self.actual_result)
def test_perfect_match(self):
"""
Test that output from the exporter exactly matches known
good HTML output, inlining and everything included.
"""
expected_result = resource_string(
"plainbox", "test-data/html-exporter/example-data.html"
) # unintuitively, resource_string returns bytes
self.assertEqual(self.actual_result, expected_result)
|