Completed
Push — master ( 874f22...ba55f4 )
by Jace
24s
created

describe_latest()   C

Complexity

Conditions 7

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
dl 0
loc 63
rs 6.1545
c 3
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A it_returns_the_last_image() 0 8 1
A enable_cache() 0 6 1
A it_returns_a_placeholder_with_an_empty_cache() 0 6 1
A it_filters_custom_images() 0 14 1
A it_filters_blocked_words() 0 14 1
A disable_cache() 0 6 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# pylint: disable=unused-variable,unused-argument,misplaced-comparison-constant,expression-not-assigned
0 ignored issues
show
introduced by
Bad option value 'misplaced-comparison-constant'
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")
27
28
            assert 200 == response.status_code
29
            assert 'image/jpeg' == response.mimetype
30
            assert os.path.isfile(path)
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
            assert 302 == response.status_code
88
            assert '-vote.jpg?alt=scowl">' in \
89
                load(response, as_json=False)
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_preview():
170
171
        def it_keeps_flag_after_redirect(client):
172
            response = client.get("/iw/i am still typi.jpg?preview=true")
173
174
            expect(response.status_code) == 302
175
            expect(load(response, as_json=False)).contains(
176
                '<a href="/iw/i-am-still-typi.jpg?preview=true">')
177
178
    def describe_latest():
179
180
        @pytest.fixture()
181
        def enable_cache():
182
            cache_filtered.disabled = False
183
            cache_unfiltered.disabled = False
184
            cache_filtered.items = []
185
            cache_unfiltered.items = []
186
187
        @pytest.fixture()
188
        def disable_cache():
189
            cache_filtered.disabled = True
190
            cache_unfiltered.disabled = True
191
            cache_filtered.items = []
192
            cache_unfiltered.items = []
193
194
        def it_returns_the_last_image(client, enable_cache):
195
            client.get("/iw/my-first-meme.jpg")
196
197
            response = client.get("/latest.jpg")
198
199
            expect(response.status_code) == 302
200
            expect(load(response, as_json=False)).contains(
201
                '<a href="http://localhost/iw/my-first-meme.jpg">')
202
203
        def it_returns_a_placeholder_with_an_empty_cache(client, disable_cache):
204
            response = client.get("/latest.jpg")
205
206
            expect(response.status_code) == 302
207
            expect(load(response, as_json=False)).contains(
208
                '<a href="http://localhost/custom/your-meme/goes-here.jpg'
209
                '?alt=https://raw.githubusercontent.com/jacebrowning/memegen/'
210
                'master/memegen/static/images/missing.png">')
211
212
        def it_filters_blocked_words(client, enable_cache):
213
            client.get("/iw/nazis.jpg")
214
215
            response = client.get("/latest.jpg")
216
217
            expect(response.status_code) == 302
218
            expect(load(response, as_json=False)).excludes(
219
                '<a href="http://localhost/iw/nazis.jpg">')
220
221
            response = client.get("/latest.jpg?filtered=False")
222
223
            expect(response.status_code) == 302
224
            expect(load(response, as_json=False)).contains(
225
                '<a href="http://localhost/iw/nazis.jpg">')
226
227
        def it_filters_custom_images(client, enable_cache):
228
            client.get("/custom/test.jpg")
229
230
            response = client.get("/latest.jpg")
231
232
            expect(response.status_code) == 302
233
            expect(load(response, as_json=False)).excludes(
234
                '<a href="http://localhost/custom/test.jpg">')
235
236
            response = client.get("/latest.jpg?filtered=False")
237
238
            expect(response.status_code) == 302
239
            expect(load(response, as_json=False)).contains(
240
                '<a href="http://localhost/custom/test.jpg">')
241
242
    def describe_redirects():
243
244
        def when_missing_dashes(client):
245
            response = client.get("/iw/HelloThere_World/How-areYOU.jpg")
246
247
            assert 302 == response.status_code
248
            assert '<a href="/iw/hello-there-world/how-are-you.jpg">' in \
249
                load(response, as_json=False)
250
251
        def when_no_text(client):
252
            response = client.get("/live.jpg")
253
254
            assert 302 == response.status_code
255
            assert '<a href="/live/_/do-it-live!.jpg">' in \
256
                load(response, as_json=False)
257
258
        def when_aliased_template(client):
259
            response = client.get("/insanity-wolf/hello/world.jpg")
260
261
            assert 302 == response.status_code
262
            assert '<a href="/iw/hello/world.jpg">' in \
263
                load(response, as_json=False)
264
265
        def when_jpeg_extension_without_text(client):
266
            response = client.get("/iw.jpeg")
267
268
            assert 302 == response.status_code
269
            assert '<a href="/iw.jpg">' in \
270
                load(response, as_json=False)
271
272
        def when_jpeg_extension_with_text(client):
273
            response = client.get("/iw/hello/world.jpeg")
274
275
            assert 302 == response.status_code
276
            assert '<a href="/iw/hello/world.jpg">' in \
277
                load(response, as_json=False)
278
279
    def describe_errors():
280
281
        def when_unknown_template(client):
282
            response = client.get("/make/sudo/give.me.jpg")
283
284
            assert 200 == response.status_code
285
            assert 'image/jpeg' == response.mimetype
286
            # unit tests ensure this is a placeholder image
287
288
        @pytest.mark.xfail(os.name == 'nt', reason="Windows has a path limit")
289
        def when_too_much_text_for_a_filename(client):
290
            top = "hello"
291
            bottom = "-".join(["world"] * 50)
292
            response = client.get("/iw/" + top + "/" + bottom + ".jpg")
293
294
            assert 414 == response.status_code
295
            assert {
296
                'message': "Filename too long."
297
            } == load(response)
298