/
test_static.py
200 lines (166 loc) · 8.29 KB
/
test_static.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import os
import mock
from collections import OrderedDict
import pytest
from django.http import HttpResponse, HttpResponseNotFound, HttpResponsePermanentRedirect
from django.shortcuts import redirect
from django.conf.urls import url
from barbeque.static import ServeStaticFileMiddleware
def foo_view(request):
return redirect('bar', permanent=True)
def bar_view(request):
return HttpResponse('Hllo FooBar!')
urlpatterns = [
url(r'^bar$', bar_view, name='bar'),
url(r'^foo$', foo_view, name='foo'),
]
class TestServeStaticFileMiddleware:
@pytest.fixture(autouse=True)
def setup(self, settings):
settings.ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
settings.STATIC_ROOT = os.path.join(settings.ROOT_DIR, 'tests', 'resources', 'static')
# settings.STATICFILES_STORAGE = (
# 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage')
settings.STATICFILES_STORAGE = (
'barbeque.storage.CompactManifestStaticFilesStorage')
@pytest.fixture
def patch_settings(self, settings):
"""
Patch settings for tests fith django client
"""
settings.STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
settings.MIDDLEWARE_CLASSES = [
'barbeque.static.ServeStaticFileMiddleware',
]
settings.INSTALLED_APPS = settings.INSTALLED_APPS + ('django.contrib.staticfiles',)
settings.ROOT_URLCONF = 'barbeque.tests.test_static'
def test_file_exists(self, rf):
request = rf.get('/static/test.jpg')
middleware = ServeStaticFileMiddleware()
response = middleware.process_response(request, HttpResponseNotFound(''))
assert response.status_code == 200
assert response['Content-Type'] == 'image/jpeg'
assert len(response.items()) == 3
assert response.has_header('Content-Length')
assert response.has_header('Last-Modified')
def test_file_missing(self, rf):
request = rf.get('/static/doesnotexist.jpg')
middleware = ServeStaticFileMiddleware()
response = middleware.process_response(request, HttpResponseNotFound(''))
assert response.status_code == 404
def test_unknown_prefix(self, rf):
request = rf.get('/foo/test.jpg')
middleware = ServeStaticFileMiddleware()
response = middleware.process_response(request, HttpResponseNotFound(''))
assert response.status_code == 404
def test_redirect_for_static(self, rf):
request = rf.get('/static/test.jpg')
middleware = ServeStaticFileMiddleware()
response = middleware.process_response(
request, HttpResponsePermanentRedirect('/static/test.jpg/'))
assert response.status_code == 200
def test_redirect_other(self, rf):
request = rf.get('/foo')
middleware = ServeStaticFileMiddleware()
redirect = HttpResponsePermanentRedirect('/foo/')
response = middleware.process_response(request, redirect)
assert response == redirect
@mock.patch('barbeque.static.ServeStaticFileMiddleware.process_response')
def test_new_style_middleware(self, process_response_mock, rf):
request = rf.get('/static/test.jpg')
get_response_mock = mock.Mock()
get_response_mock.return_value = HttpResponseNotFound()
middleware = ServeStaticFileMiddleware(get_response=get_response_mock)
middleware(request)
get_response_mock.assert_called_with(request)
process_response_mock.assert_called_with(
request, get_response_mock.return_value)
def test_with_client_hit(self, client, patch_settings):
response = client.get('/static/test.jpg')
assert response.status_code == 200
def test_with_client_redirect(self, client, patch_settings):
response = client.get('/foo')
assert response.status_code == 301
assert response['Location'].endswith('/bar')
def test_with_client_query_params(self, client, patch_settings):
response = client.get('/static/test.jpg?v=1')
assert response.status_code == 200
assert response['Content-Type'] == 'image/jpeg'
class TestServeStaticFileMiddlewareWithHashedFiles:
@pytest.fixture(autouse=True)
def setup(self, settings):
settings.ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
settings.STATIC_ROOT = os.path.join(settings.ROOT_DIR, 'tests', 'resources', 'static')
settings.STATICFILES_STORAGE = (
'django.contrib.staticfiles.storage.ManifestStaticFilesStorage')
@pytest.fixture
def patch_settings(self, settings):
"""
Patch settings for tests fith django client
"""
settings.STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
settings.MIDDLEWARE_CLASSES = [
'barbeque.static.ServeStaticFileMiddleware',
]
settings.INSTALLED_APPS = settings.INSTALLED_APPS + ('django.contrib.staticfiles',)
settings.ROOT_URLCONF = 'barbeque.tests.test_static'
def test_unhash_file_name(self):
middleware = ServeStaticFileMiddleware()
assert middleware.unhash_file_name(
'/static/test_hash.11aa22bb33cc.jpg') == ('/static/test_hash.jpg')
assert middleware.unhash_file_name('test_hash.jpg') == 'test_hash.jpg'
assert middleware.unhash_file_name(
'test_hash.11aa22bb33cc.11aa22bb33cc.jpg') == ('test_hash.11aa22bb33cc.jpg')
assert middleware.unhash_file_name('test_hash.11aa22bb33cc') == 'test_hash'
assert middleware.unhash_file_name('11aa22bb33cc') == '11aa22bb33cc'
assert middleware.unhash_file_name('11aa22bb33cc.jpg') == '11aa22bb33cc.jpg'
# assert middleware.unhash_file_name('.11aa22bb33cc.jpg') == '.11aa22bb33cc.jpg'
def test_hash_file_exists(self, rf):
request = rf.get('/static/test_hash.11aa22bb33cc.jpg')
middleware = ServeStaticFileMiddleware()
response = middleware.process_response(request, HttpResponseNotFound(''))
assert response.status_code == 200
assert response['Content-Type'] == 'image/jpeg'
assert len(response.items()) == 3
assert response.has_header('Content-Length')
assert response.has_header('Last-Modified')
def test_hash_file_original_exists(self, rf):
request = rf.get('/static/test_hash.jpg')
middleware = ServeStaticFileMiddleware()
response = middleware.process_response(request, HttpResponseNotFound(''))
assert response.status_code == 200
assert response['Content-Type'] == 'image/jpeg'
assert len(response.items()) == 3
assert response.has_header('Content-Length')
assert response.has_header('Last-Modified')
def test_old_hash(self, rf):
request = rf.get('/static/test_hash.44dd55ee66ff.jpg')
middleware = ServeStaticFileMiddleware()
response = middleware.process_response(request, HttpResponseNotFound(''))
assert len(response.items()) == 3
assert response.has_header('Content-Length')
assert response.has_header('Last-Modified')
def test_hash_file_exists_with_client_hit(self, client, patch_settings):
response = client.get('/static/test_hash.11aa22bb33cc.jpg')
assert response.status_code == 200
def test_hash_file_original_exists_with_client_hit(self, client, patch_settings):
response = client.get('/static/test_hash.jpg')
assert response.status_code == 200
def test_hash_old_hash_with_client_hit(self, client, patch_settings):
response = client.get('/static/test_hash.44dd55ee66ff.jpg')
assert response.status_code == 200
@mock.patch('django.contrib.staticfiles.storage.ManifestStaticFilesStorage.load_manifest')
def test_no_staticfiles_manifest(self, manifest_mock, rf):
manifest_mock.return_value = OrderedDict()
request = rf.get('/static/test_hash.jpg')
middleware = ServeStaticFileMiddleware()
response = middleware.process_response(request, HttpResponseNotFound(''))
assert response.status_code == 404