/usr/lib/python2.7/dist-packages/klein/test/test_plating.py is in python-klein 17.2.0-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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | """
Tests for L{klein.plating}.
"""
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
import json
from klein import Plating
from twisted.web.template import tags, slot
from twisted.web.error import FlattenerError, MissingRenderMethod
from klein.test.test_resource import requestMock, _render
from klein.test.util import TestCase
from klein import Klein
page = Plating(
defaults={
"title": "default title unchanged",
Plating.CONTENT: "NEVER MIND THE CONTENT",
},
tags=tags.html(
tags.head(tags.title(slot("title"))),
tags.body(
tags.h1(slot("title")),
tags.div(slot(Plating.CONTENT),
Class="content")
)
),
)
element = Plating(
defaults={
"a": "NO VALUE FOR A",
"b": "NO VALUE FOR B",
},
tags=tags.div(tags.span("a: ", slot("a")),
tags.span("b: ", slot("b"))),
)
@element.widgeted
def enwidget(a, b):
"""
Provide some values for the L{widget} template.
"""
return {"a": a, "b": b}
class PlatingTests(TestCase):
"""
Tests for L{Plating}.
"""
def setUp(self):
"""
Create an app and a resource wrapping that app for this test.
"""
self.app = Klein()
self.kr = self.app.resource()
def get(self, uri):
"""
Issue a virtual GET request to the given path that is expected to
succeed synchronously, and return the generated request object and
written bytes.
"""
request = requestMock(uri)
d = _render(self.kr, request)
self.successResultOf(d)
return request, request.getWrittenData()
def test_template_html(self):
"""
Rendering a L{Plating.routed} decorated route results in templated
HTML.
"""
@page.routed(self.app.route("/"),
tags.span(slot("ok")))
def plateMe(request):
return {"ok": "test-data-present"}
request, written = self.get(b"/")
self.assertIn(b'<span>test-data-present</span>', written)
self.assertIn(b'<title>default title unchanged</title>', written)
def test_template_json(self):
"""
Rendering a L{Plating.routed} decorated route with a query parameter
asking for JSON will yield JSON instead.
"""
@page.routed(self.app.route("/"),
tags.span(slot("ok")))
def plateMe(request):
return {"ok": "an-plating-test"}
request, written = self.get(b"/?json=true")
self.assertEqual(
request.responseHeaders.getRawHeaders(b'content-type')[0],
b'text/json; charset=utf-8'
)
self.assertEquals({"ok": "an-plating-test",
"title": "default title unchanged"},
json.loads(written.decode('utf-8')))
def test_template_numbers(self):
"""
Data returned from a plated method may include numeric types (integers,
floats, and possibly longs), which although they are not normally
serializable by twisted.web.template, will be converted by plating into
their decimal representation.
"""
@page.routed(self.app.route("/"),
tags.div(tags.span(slot("anInteger")),
tags.i(slot("anFloat")),
tags.b(slot("anLong")),
))
def plateMe(result):
return {"anInteger": 7,
"anFloat": 3.2,
"anLong": 0x10000000000000001}
request, written = self.get(b"/")
self.assertIn(b"<span>7</span>", written)
self.assertIn(b"<i>3.2</i>", written)
self.assertIn(b"<b>18446744073709551617</b>", written)
def test_render_list(self):
"""
The C{:list} renderer suffix will render the slot named by the renderer
as a list, filling each slot.
"""
@page.routed(self.app.route("/"),
tags.ul(tags.li(slot("item"),
render="subplating:list")))
def rsrc(request):
return {"subplating": [1, 2, 3]}
request, written = self.get(b"/")
self.assertIn(b'<ul><li>1</li><li>2</li><li>3</li></ul>', written)
self.assertIn(b'<title>default title unchanged</title>', written)
def test_widget_html(self):
"""
When L{Plating.widgeted} is applied as a decorator, it gives the
decorated function a C{widget} attribute which is a version of the
function with a modified return type that turns it into a renderable
HTML sub-element that may fill a slot.
"""
@page.routed(self.app.route("/"),
tags.div(slot("widget")))
def rsrc(request):
return {"widget": enwidget.widget(3, 4)}
request, written = self.get(b"/")
self.assertIn(b"<span>a: 3</span>", written)
self.assertIn(b"<span>b: 4</span>", written)
def test_widget_json(self):
"""
When L{Plating.widgeted} is applied as a decorator, and the result is
serialized to JSON, it appears the same as the returned value despite
the HTML-friendly wrapping described above.
"""
@page.routed(self.app.route("/"),
tags.div(slot("widget")))
def rsrc(request):
return {"widget": enwidget.widget(3, 4)}
request, written = self.get(b"/?json=1")
self.assertEqual(json.loads(written.decode('utf-8')),
{"widget": {"a": 3, "b": 4},
"title": "default title unchanged"})
def test_prime_directive_return(self):
"""
Nothing within these Articles Of Federation shall authorize the United
Federation of Planets to alter the return value of a callable by
applying a decorator to it...
"""
exact_result = {"ok": "some nonsense value"}
@page.routed(self.app.route("/"),
tags.span(slot("ok")))
def plateMe(request):
return exact_result
self.assertIdentical(plateMe(None), exact_result)
def test_prime_directive_arguments(self):
"""
... or shall require the function to modify its signature under these
Articles Of Federation.
"""
@page.routed(self.app.route("/"),
tags.span(slot("ok")))
def plateMe(request, one, two, three):
return (one, two, three)
exact_one = {"one": "and"}
exact_two = {"two": "and"}
exact_three = {"three": "and"}
result_one, result_two, result_three = plateMe(
None, exact_one, exact_two, three=exact_three
)
self.assertIdentical(result_one, exact_one)
self.assertIdentical(result_two, exact_two)
self.assertIdentical(result_three, exact_three)
def test_presentation_only_json(self):
"""
Slots marked as "presentation only" will not be reflected in the
output.
"""
plating = Plating(tags=tags.span(slot("title")),
presentation_slots={"title"})
@plating.routed(self.app.route("/"),
tags.span(slot("data")))
def justJson(request):
return {"title": "uninteresting", "data": "interesting"}
request, written = self.get(b"/?json=1")
self.assertEqual(json.loads(written.decode("utf-8")),
{"data": "interesting"})
def test_missing_renderer(self):
"""
Missing renderers will result in an exception during rendering.
"""
def test(missing):
plating = Plating(tags=tags.span(slot(Plating.CONTENT)))
@plating.routed(self.app.route("/"),
tags.span(tags.span(render=missing)))
def no(request):
return {}
self.get(b"/")
[fe] = self.flushLoggedErrors(FlattenerError)
self.assertIsInstance(fe.value.args[0], MissingRenderMethod)
test("garbage")
test("garbage:missing")
def test_json_serialize_unknown_type(self):
"""
The JSON serializer will raise a L{TypeError} when it can't find an
appropriate type.
"""
from klein._plating import json_serialize
class reprish(object):
def __repr__(self):
return '<blub>'
te = self.assertRaises(TypeError, json_serialize, {"an": reprish()})
self.assertIn("<blub>", str(te))
|