Passed
Push — master ( 985ebe...85a50e )
by Jace
01:21
created

describe_watermark()   A

Complexity

Conditions 4

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 20
loc 20
rs 9.2
1
# pylint: disable=unused-variable,unused-argument,misplaced-comparison-constant,expression-not-assigned,singleton-comparison
2
3
import os
4
5
import pytest
6
from expecter import expect
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}"))
0 ignored issues
show
introduced by
invalid syntax (<string>, line 91)
Loading history...
92
93
            expect(status) == 302
94
            expect(data).contains(f'<a href="/custom/hello.jpg?alt={url}">')
95
96
        def it_returns_an_error_with_non_image_urls(client):
97
            url = "http://example.com"
98
            response = client.get(f"/custom/hello.jpg?alt={url}")
99
100
            expect(response.status_code) == 415
101
102
        def it_returns_an_error_with_non_urls(client):
103
            url = "undefined"
104
            response = client.get(f"/custom/hello.jpg?alt={url}")
105
106
            print(response.data)
107
            expect(response.status_code) == 415
108
109
        def it_handles_png_int32_pixels(client):
110
            url = "https://raw.githubusercontent.com/jacebrowning/memegen/master/tests/files/201692816359570.jpeg"
111
            response = client.get(f"/custom/hello.jpg?alt={url}")
112
113
            expect(response.status_code) == 200
114
            expect(response.mimetype) == 'image/jpeg'
115
116
        def it_handles_jpg_cmyk_pixels(client):
117
            url = "https://raw.githubusercontent.com/jacebrowning/memegen/master/tests/files/Channel_digital_image_CMYK_color.jpg"
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_redirects_to_lose_alt_when_invalid_url(client):
124
            url = "http:invalid"
125
            status, data = load(client.get(f"/sad-biden/hello.jpg?alt={url}"))
126
127
            expect(status) == 302
128
            expect(data).contains('<a href="/sad-biden/hello.jpg">')
129
130
        def it_redirects_to_lose_alt_when_missing_schema(client):
131
            url = "http:/www.gstatic.com/webp/gallery/1.jpg"
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 View Code Duplication
    def describe_custom_font():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
138
139
        def when_provided(client):
140
            response = client.get("/iw/hello.jpg?font=impact")
141
142
            expect(response.status_code) == 200
143
            expect(response.mimetype) == 'image/jpeg'
144
145
        def it_redirects_on_unknown_fonts(client):
146
            status, data = load(client.get("/iw/hello.jpg?font=__unknown__"))
147
148
            expect(status) == 302
149
            expect(data).contains('<a href="/iw/hello.jpg">')
150
151
        def it_keeps_font_after_redirect(client):
152
            status, data = load(client.get("/iw/what%3F.jpg?font=impact"))
153
154
            expect(status) == 302
155
            expect(data).contains('<a href="/iw/what~q.jpg?font=impact">')
156
157
    def describe_custom_size():
158
159
        def it_keeps_size_after_redirect(client):
160
            status, data = load(client.get(
161
                "/iw/what%3F.jpg?width=67&height=89"))
162
163
            expect(status) == 302
164
            expect(data).contains('<a href="/iw/what~q.jpg?')
165
            expect(data).contains('width=67')
166
            expect(data).contains('height=89')
167
168 View Code Duplication
    def describe_watermark():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
169
170
        def it_accept_supported_watermark(client):
171
            response = client.get("/iw/test.jpg?watermark=test")
172
173
            expect(response.status_code) == 200
174
            expect(response.mimetype) == 'image/jpeg'
175
176
        def it_redirects_with_unsupported_watermark(client):
177
            status, data = load(client.get(
178
                "/iw/test.jpg?watermark=unsupported"))
179
180
            expect(status) == 302
181
            expect(data).contains('<a href="/iw/test.jpg"')
182
183
        def it_keeps_watermark_after_redirect(client):
184
            status, data = load(client.get("/iw/test 2.jpg?watermark=test"))
185
186
            expect(status) == 302
187
            expect(data).contains('<a href="/iw/test_2.jpg?watermark=test"')
188
189
    def describe_preview():
190
191
        def it_keeps_flag_after_redirect(client):
192
            status, data = load(client.get(
193
                "/iw/i am still typi.jpg?preview=true"))
194
195
            expect(status) == 302
196
            expect(data).contains(
197
                '<a href="/iw/i_am_still_typi.jpg?preview=true">')
198
199
    def describe_latest():
200
201
        @pytest.fixture()
202
        def enable_cache():
203
            cache_filtered.disabled = False
204
            cache_unfiltered.disabled = False
205
            cache_filtered.items = []
206
            cache_unfiltered.items = []
207
208
        @pytest.fixture()
209
        def disable_cache():
210
            cache_filtered.disabled = True
211
            cache_unfiltered.disabled = True
212
            cache_filtered.items = []
213
            cache_unfiltered.items = []
214
215
        def it_returns_the_last_image(client, enable_cache):
216
            client.get("/iw/my_first_meme.jpg")
217
218
            status, data = load(client.get("/latest.jpg"))
219
220
            expect(status) == 302
221
            expect(data).contains(
222
                '<a href="http://localhost/iw/my_first_meme.jpg?preview=true">')
223
224
        def it_returns_a_placeholder_with_an_empty_cache(client, disable_cache):
225
            status, data = load(client.get("/latest.jpg"))
226
227
            expect(status) == 302
228
            expect(data).contains(
229
                '<a href="http://localhost/custom/your_meme/goes_here.jpg'
230
                '?alt=https://raw.githubusercontent.com/jacebrowning/memegen/'
231
                'master/memegen/static/images/missing.png">')
232
233
        def it_filters_blocked_words(client, enable_cache):
234
            client.get("/iw/nazis.jpg")
235
236
            status, data = load(client.get("/latest.jpg"))
237
238
            expect(status) == 302
239
            expect(data).excludes(
240
                '<a href="http://localhost/iw/nazis.jpg')
241
242
            status, data = load(client.get("/latest.jpg?filtered=false"))
243
244
            expect(status) == 302
245
            expect(data).contains(
246
                '<a href="http://localhost/iw/nazis.jpg?preview=true">')
247
248
        def it_filters_custom_images(client, enable_cache):
249
            client.get("/custom/test.jpg")
250
251
            status, data = load(client.get("/latest.jpg"))
252
253
            expect(status) == 302
254
            expect(data).excludes(
255
                '<a href="http://localhost/custom/test.jpg')
256
257
            status, data = load(client.get("/latest.jpg?filtered=false"))
258
259
            expect(status) == 302
260
            expect(data).contains(
261
                '<a href="http://localhost/custom/test.jpg?preview=true">')
262
263
    def describe_redirects():
264
265
        def when_missing_dashes(client):
266
            status, data = load(client.get(
267
                "/iw/HelloThere_World/How-areYOU.jpg"))
268
269
            expect(status) == 302
270
            expect(data).contains(
271
                '<a href="/iw/hello_there_world/how_are_you.jpg">')
272
273
        def when_no_text(client):
274
            status, data = load(client.get("/live.jpg"))
275
276
            expect(status) == 302
277
            expect(data).contains('<a href="/live/_/do_it_live!.jpg">')
278
279
        def when_aliased_template(client):
280
            status, data = load(client.get("/insanity-wolf/hello/world.jpg"))
281
282
            expect(status) == 302
283
            expect(data).contains('<a href="/iw/hello/world.jpg">')
284
285
        def when_jpeg_extension_without_text(client):
286
            status, data = load(client.get("/iw.jpeg"))
287
288
            expect(status) == 302
289
            expect(data).contains('<a href="/iw.jpg">')
290
291
        def when_jpeg_extension_with_text(client):
292
            status, data = load(client.get("/iw/hello/world.jpeg"))
293
294
            expect(status) == 302
295
            expect(data).contains('<a href="/iw/hello/world.jpg">')
296
297
    def describe_errors():
298
299
        def when_unknown_template(client):
300
            response = client.get("/make/sudo/give.me.jpg")
301
302
            expect(response.status_code) == 200
303
            expect(response.mimetype) == 'image/jpeg'
304
            # unit tests ensure this is a placeholder image
305
306
        @pytest.mark.xfail(os.name == 'nt', reason="Windows has a path limit")
307
        def when_too_much_text_for_a_filename(client):
308
            top = "hello"
309
            bottom = "_".join(["world"] * 50)
310
            url = "/iw/" + top + "/" + bottom + ".jpg"
311
            status, data = load(client.get(url))
312
313
            expect(status) == 414
314
            expect(data) == {
315
                'message': "Filename too long."
316
            }
317