1 | # pylint: disable=unused-variable,unused-argument,misplaced-comparison-constant,expression-not-assigned,singleton-comparison |
||
2 | |||
3 | import os |
||
4 | |||
5 | import pytest |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
6 | from expecter import expect |
||
0 ignored issues
–
show
|
|||
7 | |||
8 | from memegen.routes.image import cache_filtered, cache_unfiltered |
||
9 | |||
10 | from .utils import load |
||
11 | |||
12 | TESTS = os.path.dirname(__file__) |
||
13 | ROOT = os.path.dirname(TESTS) |
||
14 | IMAGES = os.path.join(ROOT, 'data', 'images') |
||
15 | |||
16 | |||
17 | def describe_get(): |
||
18 | |||
19 | def describe_visible(): |
||
20 | |||
21 | def with_nominal_text(client): |
||
22 | path = os.path.join(IMAGES, 'iw', 'hello', 'world' + '.img') |
||
23 | if os.path.exists(path): |
||
24 | os.remove(path) |
||
25 | |||
26 | response = client.get("/iw/hello/world.jpg?watermark=none") |
||
27 | |||
28 | expect(response.status_code) == 200 |
||
29 | expect(response.mimetype) == 'image/jpeg' |
||
30 | expect(os.path.isfile(path)) == True |
||
31 | |||
32 | def with_only_1_line(client): |
||
33 | response = client.get("/iw/hello.jpg") |
||
34 | |||
35 | expect(response.status_code) == 200 |
||
36 | expect(response.mimetype) == 'image/jpeg' |
||
37 | |||
38 | @pytest.mark.xfail(os.name == 'nt', reason="Windows has a path limit") |
||
39 | def with_lots_of_text(client): |
||
40 | top = "_".join(["hello"] * 20) |
||
41 | bottom = "_".join(["world"] * 20) |
||
42 | response = client.get("/iw/" + top + "/" + bottom + ".jpg") |
||
43 | |||
44 | expect(response.status_code) == 200 |
||
45 | expect(response.mimetype) == 'image/jpeg' |
||
46 | |||
47 | def describe_hidden(): |
||
48 | |||
49 | def when_jpg(client): |
||
50 | response = client.get("/_aXcJaGVsbG8vd29ybGQJ.jpg") |
||
51 | |||
52 | expect(response.status_code) == 200 |
||
53 | expect(response.mimetype) == 'image/jpeg' |
||
54 | |||
55 | def describe_custom_style(): |
||
56 | |||
57 | def when_provided(client): |
||
58 | response = client.get("/sad-biden/hello.jpg?alt=scowl") |
||
59 | |||
60 | expect(response.status_code) == 200 |
||
61 | expect(response.mimetype) == 'image/jpeg' |
||
62 | |||
63 | def it_redirects_to_lose_alt_when_default_style(client): |
||
64 | status, data = load(client.get("/sad-biden/hello.jpg?alt=default")) |
||
65 | |||
66 | expect(status) == 302 |
||
67 | expect(data).contains('<a href="/sad-biden/hello.jpg">') |
||
68 | |||
69 | def it_redirects_to_lose_alt_when_unknown_style(client): |
||
70 | status, data = load(client.get( |
||
71 | "/sad-biden/hello.jpg?alt=__unknown__")) |
||
72 | |||
73 | expect(status) == 302 |
||
74 | expect(data).contains('<a href="/sad-biden/hello.jpg">') |
||
75 | |||
76 | def it_keeps_alt_after_template_redirect(client): |
||
77 | status, data = load(client.get("/sad-joe/hello.jpg?alt=scowl")) |
||
78 | |||
79 | expect(status) == 302 |
||
80 | expect(data).contains('<a href="/sad-biden/hello.jpg?alt=scowl">') |
||
81 | |||
82 | def it_keeps_alt_after_text_redirect(client): |
||
83 | status, data = load(client.get("/sad-biden.jpg?alt=scowl")) |
||
84 | |||
85 | expect(status) == 302 |
||
86 | expect(data).contains("_vote.jpg") |
||
87 | expect(data).contains("alt=scowl") |
||
88 | |||
89 | def it_redirects_to_custom_when_alt_is_url(client): |
||
90 | url = "http://www.gstatic.com/webp/gallery/1.jpg" |
||
91 | status, data = load(client.get(f"/sad-biden/hello.jpg?alt={url}")) |
||
92 | |||
93 | expect(status) == 302 |
||
94 | expect(data).contains(f'<a href="/custom/hello.jpg?alt={url}">') |
||
95 | |||
96 | def it_redirects_to_standard_when_alt_is_builtin(client): |
||
97 | url = "https://memegen.link/cbg/_.jpg" |
||
98 | status, data = load(client.get(f"/custom/hello.jpg?alt={url}")) |
||
99 | |||
100 | expect(status) == 302 |
||
101 | expect(data).contains(f'<a href="/cbg/hello.jpg">') |
||
0 ignored issues
–
show
|
|||
102 | |||
103 | def it_returns_an_error_with_non_image_urls(client): |
||
104 | url = "http://example.com" |
||
105 | response = client.get(f"/custom/hello.jpg?alt={url}") |
||
106 | |||
107 | expect(response.status_code) == 415 |
||
108 | |||
109 | def it_returns_an_error_with_non_urls(client): |
||
110 | url = "undefined" |
||
111 | response = client.get(f"/custom/hello.jpg?alt={url}") |
||
112 | |||
113 | print(response.data) |
||
114 | expect(response.status_code) == 415 |
||
115 | |||
116 | def it_handles_png_int32_pixels(client): |
||
117 | url = "https://raw.githubusercontent.com/jacebrowning/memegen/master/tests/files/201692816359570.jpeg" |
||
118 | response = client.get(f"/custom/hello.jpg?alt={url}") |
||
119 | |||
120 | expect(response.status_code) == 200 |
||
121 | expect(response.mimetype) == 'image/jpeg' |
||
122 | |||
123 | def it_handles_jpg_cmyk_pixels(client): |
||
124 | url = "https://raw.githubusercontent.com/jacebrowning/memegen/master/tests/files/Channel_digital_image_CMYK_color.jpg" |
||
125 | response = client.get(f"/custom/hello.jpg?alt={url}") |
||
126 | |||
127 | expect(response.status_code) == 200 |
||
128 | expect(response.mimetype) == 'image/jpeg' |
||
129 | |||
130 | def it_redirects_to_lose_alt_when_invalid_url(client): |
||
131 | url = "http:invalid" |
||
132 | status, data = load(client.get(f"/sad-biden/hello.jpg?alt={url}")) |
||
133 | |||
134 | expect(status) == 302 |
||
135 | expect(data).contains('<a href="/sad-biden/hello.jpg">') |
||
136 | |||
137 | def it_redirects_to_lose_alt_when_missing_schema(client): |
||
138 | url = "http:/www.gstatic.com/webp/gallery/1.jpg" |
||
139 | status, data = load(client.get(f"/sad-biden/hello.jpg?alt={url}")) |
||
140 | |||
141 | expect(status) == 302 |
||
142 | expect(data).contains('<a href="/sad-biden/hello.jpg">') |
||
143 | |||
144 | def describe_custom_font(): |
||
145 | |||
146 | def when_provided(client): |
||
147 | response = client.get("/iw/hello.jpg?font=impact") |
||
148 | |||
149 | expect(response.status_code) == 200 |
||
150 | expect(response.mimetype) == 'image/jpeg' |
||
151 | |||
152 | def it_redirects_on_unknown_fonts(client): |
||
153 | status, data = load(client.get("/iw/hello.jpg?font=__unknown__")) |
||
154 | |||
155 | expect(status) == 302 |
||
156 | expect(data).contains('<a href="/iw/hello.jpg">') |
||
157 | |||
158 | def it_keeps_font_after_redirect(client): |
||
159 | status, data = load(client.get("/iw/what%3F.jpg?font=impact")) |
||
160 | |||
161 | expect(status) == 302 |
||
162 | expect(data).contains('<a href="/iw/what~q.jpg?font=impact">') |
||
163 | |||
164 | def describe_custom_size(): |
||
165 | |||
166 | def it_keeps_size_after_redirect(client): |
||
167 | status, data = load(client.get( |
||
168 | "/iw/what%3F.jpg?width=67&height=89")) |
||
169 | |||
170 | expect(status) == 302 |
||
171 | expect(data).contains('<a href="/iw/what~q.jpg?') |
||
172 | expect(data).contains('width=67') |
||
173 | expect(data).contains('height=89') |
||
174 | |||
175 | def describe_watermark(): |
||
176 | |||
177 | def it_is_required_with_text(public_client): |
||
178 | url = "/iw/test.jpg?watermark=none" |
||
179 | status, data = load(public_client.get(url)) |
||
180 | |||
181 | expect(status) == 302 |
||
182 | expect(data).contains('<a href="/iw/test.jpg"') |
||
183 | |||
184 | def it_accepts_supported_custom_watermarks(client): |
||
185 | response = client.get("/iw/test.jpg?watermark=test") |
||
186 | |||
187 | expect(response.status_code) == 200 |
||
188 | expect(response.mimetype) == 'image/jpeg' |
||
189 | |||
190 | def it_redirects_with_unsupported_watermarks(client): |
||
191 | status, data = load(client.get( |
||
192 | "/iw/test.jpg?watermark=unsupported")) |
||
193 | |||
194 | expect(status) == 302 |
||
195 | expect(data).contains('<a href="/iw/test.jpg"') |
||
196 | |||
197 | def it_keeps_watermark_after_redirect(client): |
||
198 | status, data = load(client.get("/iw/test 2.jpg?watermark=test")) |
||
199 | |||
200 | expect(status) == 302 |
||
201 | expect(data).contains('<a href="/iw/test_2.jpg?watermark=test"') |
||
202 | |||
203 | def it_is_disable_when_no_text(public_client): |
||
204 | response = public_client.get("/iw/_.jpg") |
||
205 | |||
206 | expect(response.status_code) == 200 |
||
207 | expect(response.mimetype) == 'image/jpeg' |
||
208 | |||
209 | def it_redirect_to_be_disabled_when_no_text(client): |
||
210 | status, data = load(client.get("/iw/_.jpg?watermark=test")) |
||
211 | |||
212 | expect(status) == 302 |
||
213 | expect(data).contains('<a href="/iw/_.jpg"') |
||
214 | |||
215 | def describe_preview(): |
||
216 | |||
217 | def it_keeps_flag_after_redirect(client): |
||
218 | status, data = load(client.get( |
||
219 | "/iw/i am still typi.jpg?preview=true")) |
||
220 | |||
221 | expect(status) == 302 |
||
222 | expect(data).contains( |
||
223 | '<a href="/iw/i_am_still_typi.jpg?preview=true">') |
||
224 | |||
225 | def describe_latest(): |
||
226 | |||
227 | @pytest.fixture() |
||
228 | def enable_cache(): |
||
229 | cache_filtered.disabled = False |
||
230 | cache_unfiltered.disabled = False |
||
231 | cache_filtered.items = [] |
||
232 | cache_unfiltered.items = [] |
||
233 | |||
234 | @pytest.fixture() |
||
235 | def disable_cache(): |
||
236 | cache_filtered.disabled = True |
||
237 | cache_unfiltered.disabled = True |
||
238 | cache_filtered.items = [] |
||
239 | cache_unfiltered.items = [] |
||
240 | |||
241 | def it_returns_the_last_image(client, enable_cache): |
||
242 | client.get("/iw/my_first_meme.jpg") |
||
243 | |||
244 | status, data = load(client.get("/latest.jpg")) |
||
245 | |||
246 | expect(status) == 302 |
||
247 | expect(data).contains( |
||
248 | '<a href="http://localhost/iw/my_first_meme.jpg?preview=true">') |
||
249 | |||
250 | def it_returns_a_placeholder_with_an_empty_cache( |
||
251 | client, disable_cache): |
||
252 | status, data = load(client.get("/latest.jpg")) |
||
253 | |||
254 | expect(status) == 302 |
||
255 | expect(data).contains( |
||
256 | '<a href="http://localhost/custom/your_meme/goes_here.jpg' |
||
257 | '?alt=https://raw.githubusercontent.com/jacebrowning/memegen/' |
||
258 | 'master/memegen/static/images/missing.png">') |
||
259 | |||
260 | def it_filters_blocked_words(client, enable_cache): |
||
261 | client.get("/iw/nazis.jpg") |
||
262 | |||
263 | status, data = load(client.get("/latest.jpg")) |
||
264 | |||
265 | expect(status) == 302 |
||
266 | expect(data).excludes( |
||
267 | '<a href="http://localhost/iw/nazis.jpg') |
||
268 | |||
269 | status, data = load(client.get("/latest.jpg?filtered=false")) |
||
270 | |||
271 | expect(status) == 302 |
||
272 | expect(data).contains( |
||
273 | '<a href="http://localhost/iw/nazis.jpg?preview=true">') |
||
274 | |||
275 | def it_filters_custom_images(client, enable_cache): |
||
276 | client.get("/custom/test.jpg") |
||
277 | |||
278 | status, data = load(client.get("/latest.jpg")) |
||
279 | |||
280 | expect(status) == 302 |
||
281 | expect(data).excludes( |
||
282 | '<a href="http://localhost/custom/test.jpg') |
||
283 | |||
284 | status, data = load(client.get("/latest.jpg?filtered=false")) |
||
285 | |||
286 | expect(status) == 302 |
||
287 | expect(data).contains( |
||
288 | '<a href="http://localhost/custom/test.jpg?preview=true">') |
||
289 | |||
290 | def describe_redirects(): |
||
291 | |||
292 | def when_missing_dashes(client): |
||
293 | status, data = load(client.get( |
||
294 | "/iw/HelloThere_World/How-areYOU.jpg")) |
||
295 | |||
296 | expect(status) == 302 |
||
297 | expect(data).contains( |
||
298 | '<a href="/iw/hello_there_world/how_are_you.jpg">') |
||
299 | |||
300 | def when_no_text(client): |
||
301 | status, data = load(client.get("/live.jpg")) |
||
302 | |||
303 | expect(status) == 302 |
||
304 | expect(data).contains('<a href="/live/_/do_it_live!.jpg">') |
||
305 | |||
306 | def when_aliased_template(client): |
||
307 | status, data = load(client.get("/insanity-wolf/hello/world.jpg")) |
||
308 | |||
309 | expect(status) == 302 |
||
310 | expect(data).contains('<a href="/iw/hello/world.jpg">') |
||
311 | |||
312 | def when_jpeg_extension_without_text(client): |
||
313 | status, data = load(client.get("/iw.jpeg")) |
||
314 | |||
315 | expect(status) == 302 |
||
316 | expect(data).contains('<a href="/iw.jpg">') |
||
317 | |||
318 | def when_jpeg_extension_with_text(client): |
||
319 | status, data = load(client.get("/iw/hello/world.jpeg")) |
||
320 | |||
321 | expect(status) == 302 |
||
322 | expect(data).contains('<a href="/iw/hello/world.jpg">') |
||
323 | |||
324 | def describe_errors(): |
||
325 | |||
326 | def when_unknown_template(client): |
||
327 | response = client.get("/make/sudo/give.me.jpg") |
||
328 | |||
329 | expect(response.status_code) == 200 |
||
330 | expect(response.mimetype) == 'image/jpeg' |
||
331 | # unit tests ensure this is a placeholder image |
||
332 | |||
333 | @pytest.mark.xfail(os.name == 'nt', reason="Windows has a path limit") |
||
334 | def when_too_much_text_for_a_filename(client): |
||
335 | top = "hello" |
||
336 | bottom = "_".join(["world"] * 50) |
||
337 | url = "/iw/" + top + "/" + bottom + ".jpg" |
||
338 | status, data = load(client.get(url)) |
||
339 | |||
340 | expect(status) == 414 |
||
341 | expect(data) == { |
||
342 | 'message': "Filename too long." |
||
343 | } |
||
344 |