-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_datasette_render_images.py
116 lines (104 loc) · 5.21 KB
/
test_datasette_render_images.py
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
from datasette_render_images import render_cell
from datasette.app import Datasette
from markupsafe import Markup
import pytest
GIF_1x1 = (
b"GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\xff\xff\xff!\xf9\x04\x01\x00"
b"\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x01D\x00;"
)
GIF_EXPECTED = (
'<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAAB'
'AAEAAAIBRAA7" alt="">'
)
PNG_1x1 = (
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01"
b"\x08\x00\x00\x00\x00:~\x9bU\x00\x00\x00\nIDATx\x9cc\xfa\x0f\x00\x01"
b"\x05\x01\x02\xcf\xa0.\xcd\x00\x00\x00\x00IEND\xaeB`\x82"
)
PNG_EXPECTED = (
'<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptV'
'AAAACklEQVR4nGP6DwABBQECz6AuzQAAAABJRU5ErkJggg==" alt="">'
)
# https://github.com/python/cpython/blob/master/Lib/test/imghdrdata/python.jpg
JPEG = (
b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00\x01\x00\x01\x00\x00"
b"\xff\xdb\x00C\x00\x03\x02\x02\x02\x02\x02\x03\x02\x02\x02\x03"
b"\x03\x03\x03\x04\x06\x04\x04\x04\x04\x04\x08\x06\x06\x05\x06\t\x08\n\n\t"
b"\x08\t\t\n\x0c\x0f\x0c\n\x0b\x0e\x0b\t\t\r\x11\r\x0e\x0f\x10\x10"
b"\x11\x10\n\x0c\x12\x13\x12\x10\x13\x0f\x10\x10\x10\xff\xdb\x00C\x01\x03\x03"
b"\x03\x04\x03\x04\x08\x04\x04\x08\x10\x0b\t\x0b\x10\x10\x10\x10"
b"\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10"
b"\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10"
b"\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\xff\xc0"
b'\x00\x11\x08\x00\x10\x00\x10\x03\x01"\x00\x02\x11\x01\x03\x11'
b"\x01\xff\xc4\x00\x16\x00\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x07\x04\x05\xff\xc4\x00$\x10\x00\x01"
b"\x04\x01\x04\x02\x02\x03\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02"
b'\x03\x04\x06\x05\x07\x08\x12\x13\x11"\x00\x14\t12\xff\xc4\x00\x15\x01'
b"\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x06\xff\xc4\x00#\x11\x00\x01\x02\x05\x03\x05\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x01\x02\x11\x03\x04\x05\x06!\x00\x121\x15\x16"
b"a\x81\xe1\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00?\x00\x14\xa6\xd2"
b"j\x1bs\xc1\xe6\x13\x12\xd4\x95\x1c\xf3\x11c\xe4%e\xbe\xbaZ\xeciE@\xb1"
b"\xe5 \xb2T\xa5\x1f\xd2\xca\xb8\xfa\xf2 \xab\x96=\x97l\x935\xe6\x9bw\xd7\xe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ks51+MiU/Iaad7UcUo4tILoS4kqcWkezS0hO/HvuRp0rO6hWnWO1UisZVuFi4GFeyEpmGepa5S"
"5SWVPuciFKRFLgSrwetnyPIB+Vb4N9mKhQMzo5po9XLdDs9d6ZVix2VEhiL9kuNPxw2gEKcDQ/rs"
'8AuA8VAe0vdl7VOYn+27flGAUgmITjbhSmCg3BYlyeWDkMolvw4KOp1KM6iCNvngZHwetf//Z" a'
'lt="">'
)
@pytest.mark.parametrize(
"input,expected",
[
("hello", None),
(1, None),
(True, None),
(GIF_1x1, GIF_EXPECTED),
# If it's a unicode string, not bytes, it is ignored:
(GIF_1x1.decode("latin1"), None),
# 1x1 transparent PNG:
(PNG_1x1, PNG_EXPECTED),
(PNG_1x1.decode("latin1"), None),
# Smallest possible JPEG, from https://github.com/mathiasbynens/small/
(JPEG, JPEG_EXPECTED),
(JPEG.decode("latin1"), None),
],
)
def test_render_cell(input, expected):
actual = render_cell(input, None)
assert expected == actual
assert actual is None or isinstance(actual, Markup)
def test_render_cell_maximum_image_size():
max_length = 100 * 1024
max_image = GIF_1x1 + (b"b" * (max_length - len(GIF_1x1)))
rendered = render_cell(max_image, None)
assert rendered is not None
assert rendered.startswith("<img src")
# Add one byte and it should no longer render
assert render_cell(max_image + b"b", None) is None
def test_render_cell_different_size_limit():
max_length = 100 * 1024
max_image_plus_one = GIF_1x1 + (b"b" * (max_length - len(GIF_1x1))) + b"b"
assert render_cell(max_image_plus_one, None) is None
ds = Datasette(
[],
metadata={"plugins": {"datasette-render-images": {"size_limit": 101 * 1024}}},
)
rendered = render_cell(max_image_plus_one, ds)
assert rendered is not None
assert rendered.startswith("<img src")