Completed
Push — master ( 651428...89ccd4 )
by Jace
17s
created

with_nominal_text()   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
c 3
b 0
f 0
dl 0
loc 10
rs 9.4285
1
# pylint: disable=unused-variable,unused-argument,misplaced-comparison-constant,expression-not-assigned,singleton-comparison
0 ignored issues
show
introduced by
Bad option value 'misplaced-comparison-constant'
Loading history...
introduced by
Bad option value 'singleton-comparison'
Loading history...
2
3
import os
4
5
import pytest
6
from expecter import expect
0 ignored issues
show
Configuration introduced by
The import expecter could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
7
8
from memegen.routes.image import cache_filtered, cache_unfiltered
9
10
from .conftest 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
            assert 200 == response.status_code
36
            assert 'image/jpeg' == response.mimetype
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
            assert 200 == response.status_code
45
            assert 'image/jpeg' == response.mimetype
46
47
    def describe_hidden():
48
49
        def when_jpg(client):
50
            response = client.get("/_aXcJaGVsbG8vd29ybGQJ.jpg")
51
52
            assert 200 == response.status_code
53
            assert 'image/jpeg' == response.mimetype
54
55
    def describe_custom_style():
56
57
        def when_provided(client):
58
            response = client.get("/sad-biden/hello.jpg?alt=scowl")
59
60
            assert 200 == response.status_code
61
            assert 'image/jpeg' == response.mimetype
62
63
        def it_redirects_to_lose_alt_when_default_style(client):
64
            response = client.get("/sad-biden/hello.jpg?alt=default")
65
66
            assert 302 == response.status_code
67
            assert '<a href="/sad-biden/hello.jpg">' in \
68
                load(response, as_json=False)
69
70
        def it_redirects_to_lose_alt_when_unknown_style(client):
71
            response = client.get("/sad-biden/hello.jpg?alt=__unknown__")
72
73
            assert 302 == response.status_code
74
            assert '<a href="/sad-biden/hello.jpg">' in \
75
                load(response, as_json=False)
76
77
        def it_keeps_alt_after_template_redirect(client):
78
            response = client.get("/sad-joe/hello.jpg?alt=scowl")
79
80
            assert 302 == response.status_code
81
            assert '<a href="/sad-biden/hello.jpg?alt=scowl">' in \
82
                load(response, as_json=False)
83
84
        def it_keeps_alt_after_text_redirect(client):
85
            response = client.get("/sad-biden.jpg?alt=scowl")
86
87
            expect(response.status_code) == 302
88
            expect(load(response, as_json=False)).contains("_vote.jpg")
89
            expect(load(response, as_json=False)).contains("alt=scowl")
90
91
        def when_url(client):
92
            url = "http://www.gstatic.com/webp/gallery/1.jpg"
93
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
94
95
            expect(response.status_code) == 200
96
            expect(response.mimetype) == 'image/jpeg'
97
98
        def it_returns_an_error_with_non_image_urls(client):
99
            url = "http://example.com"
100
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
101
102
            expect(response.status_code) == 415
103
104
        def it_handles_png_int32_pixels(client):
105
            url = "https://raw.githubusercontent.com/jacebrowning/memegen/master/tests/files/201692816359570.jpeg"
106
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
107
108
            expect(response.status_code) == 200
109
            expect(response.mimetype) == 'image/jpeg'
110
111
        def it_handles_jpg_cmyk_pixels(client):
112
            url = "https://raw.githubusercontent.com/jacebrowning/memegen/master/tests/files/Channel_digital_image_CMYK_color.jpg"
113
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
114
115
            expect(response.status_code) == 200
116
            expect(response.mimetype) == 'image/jpeg'
117
118
        def it_redirects_to_lose_alt_when_unknown_url(client):
119
            url = "http://example.com/not/a/real/image.jpg"
120
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
121
122
            expect(response.status_code) == 302
123
            expect(load(response, as_json=False)).contains(
124
                '<a href="/sad-biden/hello.jpg">')
125
126
        def it_redirects_to_lose_alt_when_bad_url(client):
127
            url = "http:invalid"
128
            response = client.get("/sad-biden/hello.jpg?alt=" + url)
129
130
            expect(response.status_code) == 302
131
            expect(load(response, as_json=False)).contains(
132
                '<a href="/sad-biden/hello.jpg">')
133
134 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...
135
136
        def when_provided(client):
137
            response = client.get("/iw/hello.jpg?font=impact")
138
139
            expect(response.status_code) == 200
140
            expect(response.mimetype) == 'image/jpeg'
141
142
        def it_redirects_on_unknown_fonts(client):
143
            response = client.get("/iw/hello.jpg?font=__unknown__")
144
145
            expect(response.status_code) == 302
146
            expect(load(response, as_json=False)).contains(
147
                '<a href="/iw/hello.jpg">')
148
149
        def it_keeps_font_after_redirect(client):
150
            response = client.get("/iw/what%3F.jpg?font=impact")
151
152
            expect(response.status_code) == 302
153
            expect(load(response, as_json=False)).contains(
154
                '<a href="/iw/what~q.jpg?font=impact">')
155
156
    def describe_custom_size():
157
158
        def it_keeps_size_after_redirect(client):
159
            response = client.get("/iw/what%3F.jpg?width=67&height=89")
160
161
            expect(response.status_code) == 302
162
            expect(load(response, as_json=False)).contains(
163
                '<a href="/iw/what~q.jpg?')
164
            expect(load(response, as_json=False)).contains(
165
                'width=67')
166
            expect(load(response, as_json=False)).contains(
167
                'height=89')
168
169
    def describe_watermark():
170
171
        def it_accept_supported_watermark(client):
172
            response = client.get("/iw/test.jpg?watermark=test")
173
174
            expect(response.status_code) == 200
175
            expect(response.mimetype) == 'image/jpeg'
176
177
        def it_redirects_with_unsupported_watermark(client):
178
            response = client.get("/iw/test.jpg?watermark=unsupported")
179
180
            expect(response.status_code) == 302
181
            expect(load(response, as_json=False)).contains(
182
                '<a href="/iw/test.jpg"')
183
184
        def it_keeps_watermark_after_redirect(client):
185
            response = client.get("/iw/test 2.jpg?watermark=test")
186
187
            expect(response.status_code) == 302
188
            expect(load(response, as_json=False)).contains(
189
                '<a href="/iw/test_2.jpg?watermark=test"')
190
191
    def describe_preview():
192
193
        def it_keeps_flag_after_redirect(client):
194
            response = client.get("/iw/i am still typi.jpg?preview=true")
195
196
            expect(response.status_code) == 302
197
            expect(load(response, as_json=False)).contains(
198
                '<a href="/iw/i_am_still_typi.jpg?preview=true">')
199
200
    def describe_latest():
201
202
        @pytest.fixture()
203
        def enable_cache():
204
            cache_filtered.disabled = False
205
            cache_unfiltered.disabled = False
206
            cache_filtered.items = []
207
            cache_unfiltered.items = []
208
209
        @pytest.fixture()
210
        def disable_cache():
211
            cache_filtered.disabled = True
212
            cache_unfiltered.disabled = True
213
            cache_filtered.items = []
214
            cache_unfiltered.items = []
215
216
        def it_returns_the_last_image(client, enable_cache):
217
            client.get("/iw/my_first_meme.jpg")
218
219
            response = client.get("/latest.jpg")
220
221
            expect(response.status_code) == 302
222
            expect(load(response, as_json=False)).contains(
223
                '<a href="http://localhost/iw/my_first_meme.jpg?preview=true">')
224
225
        def it_returns_a_placeholder_with_an_empty_cache(client, disable_cache):
226
            response = client.get("/latest.jpg")
227
228
            expect(response.status_code) == 302
229
            expect(load(response, as_json=False)).contains(
230
                '<a href="http://localhost/custom/your_meme/goes_here.jpg'
231
                '?alt=https://raw.githubusercontent.com/jacebrowning/memegen/'
232
                'master/memegen/static/images/missing.png">')
233
234
        def it_filters_blocked_words(client, enable_cache):
235
            client.get("/iw/nazis.jpg")
236
237
            response = client.get("/latest.jpg")
238
239
            expect(response.status_code) == 302
240
            expect(load(response, as_json=False)).excludes(
241
                '<a href="http://localhost/iw/nazis.jpg')
242
243
            response = client.get("/latest.jpg?filtered=false")
244
245
            expect(response.status_code) == 302
246
            expect(load(response, as_json=False)).contains(
247
                '<a href="http://localhost/iw/nazis.jpg?preview=true">')
248
249
        def it_filters_custom_images(client, enable_cache):
250
            client.get("/custom/test.jpg")
251
252
            response = client.get("/latest.jpg")
253
254
            expect(response.status_code) == 302
255
            expect(load(response, as_json=False)).excludes(
256
                '<a href="http://localhost/custom/test.jpg')
257
258
            response = client.get("/latest.jpg?filtered=false")
259
260
            expect(response.status_code) == 302
261
            expect(load(response, as_json=False)).contains(
262
                '<a href="http://localhost/custom/test.jpg?preview=true">')
263
264
    def describe_redirects():
265
266
        def when_missing_dashes(client):
267
            response = client.get("/iw/HelloThere_World/How-areYOU.jpg")
268
269
            assert 302 == response.status_code
270
            assert '<a href="/iw/hello_there_world/how_are_you.jpg">' in \
271
                load(response, as_json=False)
272
273
        def when_no_text(client):
274
            response = client.get("/live.jpg")
275
276
            assert 302 == response.status_code
277
            assert '<a href="/live/_/do_it_live!.jpg">' in \
278
                load(response, as_json=False)
279
280
        def when_aliased_template(client):
281
            response = client.get("/insanity-wolf/hello/world.jpg")
282
283
            assert 302 == response.status_code
284
            assert '<a href="/iw/hello/world.jpg">' in \
285
                load(response, as_json=False)
286
287
        def when_jpeg_extension_without_text(client):
288
            response = client.get("/iw.jpeg")
289
290
            assert 302 == response.status_code
291
            assert '<a href="/iw.jpg">' in \
292
                load(response, as_json=False)
293
294
        def when_jpeg_extension_with_text(client):
295
            response = client.get("/iw/hello/world.jpeg")
296
297
            assert 302 == response.status_code
298
            assert '<a href="/iw/hello/world.jpg">' in \
299
                load(response, as_json=False)
300
301
    def describe_errors():
302
303
        def when_unknown_template(client):
304
            response = client.get("/make/sudo/give.me.jpg")
305
306
            assert 200 == response.status_code
307
            assert 'image/jpeg' == response.mimetype
308
            # unit tests ensure this is a placeholder image
309
310
        @pytest.mark.xfail(os.name == 'nt', reason="Windows has a path limit")
311
        def when_too_much_text_for_a_filename(client):
312
            top = "hello"
313
            bottom = "_".join(["world"] * 50)
314
            response = client.get("/iw/" + top + "/" + bottom + ".jpg")
315
316
            assert 414 == response.status_code
317
            assert {
318
                'message': "Filename too long."
319
            } == load(response)
320