22
22
from glance import context
23
23
import glance .db .sqlalchemy .api as db
24
24
import glance .registry .client .v1 .api as registry
25
- from glance .tests import utils
25
+ from glance .tests .unit import base
26
+ from glance .tests .unit import utils as unit_test_utils
26
27
27
28
28
29
class TestCacheMiddlewareURLMatching (testtools .TestCase ):
@@ -87,13 +88,20 @@ def get_caching_iter(self, image_id, image_checksum, app_iter):
87
88
self .image_checksum = image_checksum
88
89
89
90
self .cache = DummyCache ()
91
+ self .policy = unit_test_utils .FakePolicyEnforcer ()
90
92
91
93
92
- class TestCacheMiddlewareChecksumVerification (testtools .TestCase ):
94
+ class TestCacheMiddlewareChecksumVerification (base .IsolatedUnitTest ):
95
+ def setUp (self ):
96
+ super (TestCacheMiddlewareChecksumVerification , self ).setUp ()
97
+ self .context = context .RequestContext (is_admin = True )
98
+ self .request = webob .Request .blank ('' )
99
+ self .request .context = self .context
100
+
93
101
def test_checksum_v1_header (self ):
94
102
cache_filter = ChecksumTestCacheFilter ()
95
103
headers = {"x-image-meta-checksum" : "1234567890" }
96
- resp = webob .Response (headers = headers )
104
+ resp = webob .Response (request = self . request , headers = headers )
97
105
cache_filter ._process_GET_response (resp , None )
98
106
99
107
self .assertEqual ("1234567890" , cache_filter .cache .image_checksum )
@@ -104,14 +112,14 @@ def test_checksum_v2_header(self):
104
112
"x-image-meta-checksum" : "1234567890" ,
105
113
"Content-MD5" : "abcdefghi"
106
114
}
107
- resp = webob .Response (headers = headers )
115
+ resp = webob .Response (request = self . request , headers = headers )
108
116
cache_filter ._process_GET_response (resp , None )
109
117
110
118
self .assertEqual ("abcdefghi" , cache_filter .cache .image_checksum )
111
119
112
120
def test_checksum_missing_header (self ):
113
121
cache_filter = ChecksumTestCacheFilter ()
114
- resp = webob .Response ()
122
+ resp = webob .Response (request = self . request )
115
123
cache_filter ._process_GET_response (resp , None )
116
124
117
125
self .assertEqual (None , cache_filter .cache .image_checksum )
@@ -143,14 +151,10 @@ def get_image_size(self, image_id):
143
151
pass
144
152
145
153
self .cache = DummyCache ()
154
+ self .policy = unit_test_utils .FakePolicyEnforcer ()
146
155
147
156
148
- class TestCacheMiddlewareProcessRequest (utils .BaseTestCase ):
149
- def setUp (self ):
150
- super (TestCacheMiddlewareProcessRequest , self ).setUp ()
151
- self .stubs = stubout .StubOutForTesting ()
152
- self .addCleanup (self .stubs .UnsetAll )
153
-
157
+ class TestCacheMiddlewareProcessRequest (base .IsolatedUnitTest ):
154
158
def test_v1_deleted_image_fetch (self ):
155
159
"""
156
160
Test for determining that when an admin tries to download a deleted
@@ -182,6 +186,7 @@ def fake_process_v1_request(request, image_id, image_iterator):
182
186
183
187
image_id = 'test1'
184
188
request = webob .Request .blank ('/v1/images/%s' % image_id )
189
+ request .context = context .RequestContext ()
185
190
186
191
cache_filter = ProcessRequestTestCacheFilter ()
187
192
self .stubs .Set (cache_filter , '_process_v1_request' ,
@@ -307,23 +312,32 @@ def fake_image_tag_get_all(context, image_id, session=None):
307
312
self .assertEqual (response .headers ['Content-Length' ],
308
313
'123456789' )
309
314
315
+ def test_process_request_without_download_image_policy (self ):
316
+ """
317
+ Test for cache middleware skip processing when request
318
+ context has not 'download_image' role.
319
+ """
320
+ image_id = 'test1'
321
+ request = webob .Request .blank ('/v1/images/%s' % image_id )
322
+ request .context = context .RequestContext ()
310
323
311
- class TestProcessResponse (utils .BaseTestCase ):
312
- def setUp (self ):
313
- super (TestProcessResponse , self ).setUp ()
314
- self .stubs = stubout .StubOutForTesting ()
324
+ cache_filter = ProcessRequestTestCacheFilter ()
315
325
316
- def tearDown ( self ):
317
- super ( TestProcessResponse , self ). tearDown ( )
318
- self . stubs . UnsetAll ()
326
+ rules = { 'download_image' : '!' }
327
+ self . set_policy_rules ( rules )
328
+ cache_filter . policy = glance . api . policy . Enforcer ()
319
329
330
+ self .assertEqual (None , cache_filter .process_request (request ))
331
+
332
+
333
+ class TestCacheMiddlewareProcessResponse (base .IsolatedUnitTest ):
320
334
def test_process_v1_DELETE_response (self ):
321
335
image_id = 'test1'
322
336
request = webob .Request .blank ('/v1/images/%s' % image_id )
323
337
request .context = context .RequestContext ()
324
338
cache_filter = ProcessRequestTestCacheFilter ()
325
339
headers = {"x-image-meta-deleted" : True }
326
- resp = webob .Response (headers = headers )
340
+ resp = webob .Response (request = request , headers = headers )
327
341
actual = cache_filter ._process_DELETE_response (resp , image_id )
328
342
self .assertEqual (actual , resp )
329
343
@@ -335,7 +349,7 @@ def test_get_status_code(self):
335
349
self .assertEqual (200 , actual )
336
350
337
351
def test_process_response (self ):
338
- def fake_fetch_request_info ():
352
+ def fake_fetch_request_info (* args , ** kwargs ):
339
353
return ('test1' , 'GET' )
340
354
341
355
cache_filter = ProcessRequestTestCacheFilter ()
@@ -344,6 +358,28 @@ def fake_fetch_request_info():
344
358
request = webob .Request .blank ('/v1/images/%s' % image_id )
345
359
request .context = context .RequestContext ()
346
360
headers = {"x-image-meta-deleted" : True }
347
- resp1 = webob .Response (headers = headers )
348
- actual = cache_filter .process_response (resp1 )
349
- self .assertEqual (actual , resp1 )
361
+ resp = webob .Response (request = request , headers = headers )
362
+ actual = cache_filter .process_response (resp )
363
+ self .assertEqual (actual , resp )
364
+
365
+ def test_process_response_without_download_image_policy (self ):
366
+ """
367
+ Test for cache middleware raise webob.exc.HTTPForbidden directly
368
+ when request context has not 'download_image' role.
369
+ """
370
+ def fake_fetch_request_info (* args , ** kwargs ):
371
+ return ('test1' , 'GET' )
372
+
373
+ cache_filter = ProcessRequestTestCacheFilter ()
374
+ cache_filter ._fetch_request_info = fake_fetch_request_info
375
+ rules = {'download_image' : '!' }
376
+ self .set_policy_rules (rules )
377
+ cache_filter .policy = glance .api .policy .Enforcer ()
378
+
379
+ image_id = 'test1'
380
+ request = webob .Request .blank ('/v1/images/%s' % image_id )
381
+ request .context = context .RequestContext ()
382
+ resp = webob .Response (request = request )
383
+ self .assertRaises (webob .exc .HTTPForbidden ,
384
+ cache_filter .process_response , resp )
385
+ self .assertEqual (['' ], resp .app_iter )
0 commit comments