26
26
from swift.common.swob import Request
27
27
from swift.account.server import AccountController, ACCOUNT_LISTING_LIMIT
28
from swift.common.utils import normalize_timestamp
28
from swift.common.utils import normalize_timestamp, replication, public
31
31
class TestAccountController(unittest.TestCase):
145
145
self.controller.PUT(req)
146
146
req = Request.blank('/sda1/p/a', environ={'REQUEST_METHOD': 'DELETE',
147
147
'HTTP_X_TIMESTAMP': '1'})
148
resp = self.controller.DELETE(req)
148
resp = req.get_response(self.controller)
149
149
req = Request.blank('/sda1/p/a', environ={'REQUEST_METHOD': 'HEAD'})
150
resp = self.controller.HEAD(req)
150
resp = req.get_response(self.controller)
151
151
self.assertEquals(resp.status_int, 404)
152
152
self.assertEquals(resp.headers['X-Account-Status'], 'Deleted')
231
231
format = '%D1%BD%8A9' # invalid UTF-8; should be %E1%BD%8A9 (E -> D)
232
232
req = Request.blank('/sda1/p/a?format=' + format,
233
233
environ={'REQUEST_METHOD': 'HEAD'})
234
resp = self.controller.HEAD(req)
234
resp = req.get_response(self.controller)
235
235
self.assertEquals(resp.status_int, 400)
237
237
def test_PUT_not_found(self):
1251
1251
for format in ('xml', 'json'):
1252
1252
req = Request.blank('/sda1/p/a?format=%s' % format,
1253
1253
environ={'REQUEST_METHOD': 'GET'})
1254
resp = self.controller.GET(req)
1254
resp = req.get_response(self.controller)
1255
1255
self.assertEquals(resp.status_int, 200)
1257
1257
def test_params_utf8(self):
1261
1261
req = Request.blank('/sda1/p/a?%s=\xce' % param,
1262
1262
environ={'REQUEST_METHOD': 'GET'})
1263
resp = self.controller.GET(req)
1263
resp = req.get_response(self.controller)
1264
1264
self.assertEquals(resp.status_int, 400,
1265
1265
"%d on param %s" % (resp.status_int, param))
1266
1266
# Good UTF8 sequence for delimiter, too long (1 byte delimiters only)
1348
1348
def test_list_allowed_methods(self):
1349
1349
""" Test list of allowed_methods """
1350
methods = ['DELETE', 'PUT', 'HEAD', 'GET', 'REPLICATE', 'POST']
1351
self.assertEquals(self.controller.allowed_methods, methods)
1353
def test_allowed_methods_from_configuration_file(self):
1355
Test list of allowed_methods which
1356
were set from configuration file.
1358
conf = {'devices': self.testdir, 'mount_check': 'false'}
1359
self.assertEquals(AccountController(conf).allowed_methods,
1360
['DELETE', 'PUT', 'HEAD', 'GET', 'REPLICATE',
1362
conf['replication_server'] = 'True'
1363
self.assertEquals(AccountController(conf).allowed_methods,
1365
conf['replication_server'] = 'False'
1366
self.assertEquals(AccountController(conf).allowed_methods,
1367
['DELETE', 'PUT', 'HEAD', 'GET', 'POST'])
1350
obj_methods = ['DELETE', 'PUT', 'HEAD', 'GET', 'POST']
1351
repl_methods = ['REPLICATE']
1352
for method_name in obj_methods:
1353
method = getattr(self.controller, method_name)
1354
self.assertFalse(hasattr(method, 'replication'))
1355
for method_name in repl_methods:
1356
method = getattr(self.controller, method_name)
1357
self.assertEquals(method.replication, True)
1369
1359
def test_correct_allowed_method(self):
1374
1364
inbuf = StringIO()
1375
1365
errbuf = StringIO()
1376
1366
outbuf = StringIO()
1367
self.controller = AccountController(
1368
{'devices': self.testdir, 'mount_check': 'false',
1369
'replication_server': 'false'})
1378
1371
def start_response(*args):
1379
1372
""" Sends args to outbuf """
1380
1373
outbuf.writelines(args)
1382
method = self.controller.allowed_methods[0]
1384
1376
env = {'REQUEST_METHOD': method,
1385
1377
'SCRIPT_NAME': '',
1386
1378
'PATH_INFO': '/sda1/p/a/c',
1396
1388
'wsgi.multiprocess': False,
1397
1389
'wsgi.run_once': False}
1399
answer = ['<html><h1>Method Not Allowed</h1><p>The method is not '
1400
'allowed for this resource.</p></html>']
1391
method_res = mock.MagicMock()
1392
mock_method = public(lambda x: mock.MagicMock(return_value=method_res))
1402
1393
with mock.patch.object(self.controller, method,
1403
return_value=mock.MagicMock()) as mock_method:
1395
mock_method.replication = False
1404
1396
response = self.controller.__call__(env, start_response)
1405
self.assertNotEqual(response, answer)
1406
self.assertEqual(mock_method.call_count, 1)
1397
self.assertEqual(response, method_res)
1408
1399
def test_not_allowed_method(self):
1413
1404
inbuf = StringIO()
1414
1405
errbuf = StringIO()
1415
1406
outbuf = StringIO()
1407
self.controller = AccountController(
1408
{'devices': self.testdir, 'mount_check': 'false',
1409
'replication_server': 'false'})
1417
1411
def start_response(*args):
1418
1412
""" Sends args to outbuf """
1419
1413
outbuf.writelines(args)
1421
method = self.controller.allowed_methods[0]
1423
1416
env = {'REQUEST_METHOD': method,
1424
1417
'SCRIPT_NAME': '',
1425
1418
'PATH_INFO': '/sda1/p/a/c',
1438
1431
answer = ['<html><h1>Method Not Allowed</h1><p>The method is not '
1439
1432
'allowed for this resource.</p></html>']
1433
mock_method = replication(public(lambda x: mock.MagicMock()))
1441
1434
with mock.patch.object(self.controller, method,
1442
return_value=mock.MagicMock()) as mock_method:
1443
self.controller.allowed_methods.remove(method)
1436
mock_method.replication = True
1444
1437
response = self.controller.__call__(env, start_response)
1445
self.assertEqual(mock_method.call_count, 0)
1446
1438
self.assertEqual(response, answer)
1447
self.controller.allowed_methods.append(method)
1449
1440
if __name__ == '__main__':
1450
1441
unittest.main()