Convert maximum length to integer in name_check
This patch converts maximum_length to integer in name_check middleware if read from proxy-server.conf file. Change-Id: I28d66fd1b7ce56ca5540cb7189d084c36e063d43 Closes-Bug: 1372397
This commit is contained in:
@@ -59,7 +59,7 @@ class NameCheckMiddleware(object):
|
|||||||
self.conf = conf
|
self.conf = conf
|
||||||
self.forbidden_chars = self.conf.get('forbidden_chars',
|
self.forbidden_chars = self.conf.get('forbidden_chars',
|
||||||
FORBIDDEN_CHARS)
|
FORBIDDEN_CHARS)
|
||||||
self.maximum_length = self.conf.get('maximum_length', MAX_LENGTH)
|
self.maximum_length = int(self.conf.get('maximum_length', MAX_LENGTH))
|
||||||
self.forbidden_regexp = self.conf.get('forbidden_regexp',
|
self.forbidden_regexp = self.conf.get('forbidden_regexp',
|
||||||
FORBIDDEN_REGEXP)
|
FORBIDDEN_REGEXP)
|
||||||
if self.forbidden_regexp:
|
if self.forbidden_regexp:
|
||||||
@@ -120,18 +120,20 @@ class NameCheckMiddleware(object):
|
|||||||
if self.check_character(req):
|
if self.check_character(req):
|
||||||
return HTTPBadRequest(
|
return HTTPBadRequest(
|
||||||
request=req,
|
request=req,
|
||||||
body=("Object/Container name contains forbidden chars from %s"
|
body=("Object/Container/Account name contains forbidden "
|
||||||
|
"chars from %s"
|
||||||
% self.forbidden_chars))(env, start_response)
|
% self.forbidden_chars))(env, start_response)
|
||||||
elif self.check_length(req):
|
elif self.check_length(req):
|
||||||
return HTTPBadRequest(
|
return HTTPBadRequest(
|
||||||
request=req,
|
request=req,
|
||||||
body=("Object/Container name longer than the allowed maximum "
|
body=("Object/Container/Account name longer than the "
|
||||||
|
"allowed maximum "
|
||||||
"%s" % self.maximum_length))(env, start_response)
|
"%s" % self.maximum_length))(env, start_response)
|
||||||
elif self.check_regexp(req):
|
elif self.check_regexp(req):
|
||||||
return HTTPBadRequest(
|
return HTTPBadRequest(
|
||||||
request=req,
|
request=req,
|
||||||
body=("Object/Container name contains a forbidden substring "
|
body=("Object/Container/Account name contains a forbidden "
|
||||||
"from regular expression %s"
|
"substring from regular expression %s"
|
||||||
% self.forbidden_regexp))(env, start_response)
|
% self.forbidden_regexp))(env, start_response)
|
||||||
else:
|
else:
|
||||||
# Pass on to downstream WSGI component
|
# Pass on to downstream WSGI component
|
||||||
|
@@ -58,17 +58,39 @@ class TestNameCheckMiddleware(unittest.TestCase):
|
|||||||
self.test_check)
|
self.test_check)
|
||||||
self.assertEquals(
|
self.assertEquals(
|
||||||
resp.body,
|
resp.body,
|
||||||
("Object/Container name contains forbidden chars from %s"
|
("Object/Container/Account name contains forbidden chars "
|
||||||
% self.conf['forbidden_chars']))
|
"from %s" % self.conf['forbidden_chars']))
|
||||||
self.assertEquals(resp.status_int, 400)
|
self.assertEquals(resp.status_int, 400)
|
||||||
|
|
||||||
|
def test_maximum_length_from_config(self):
|
||||||
|
# test invalid length
|
||||||
|
orig_test_check = self.test_check
|
||||||
|
conf = {'maximum_length': "500"}
|
||||||
|
self.test_check = name_check.filter_factory(conf)(FakeApp())
|
||||||
|
path = '/V1.0/a/c' + 'o' * (500 - 8)
|
||||||
|
resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}
|
||||||
|
).get_response(self.test_check)
|
||||||
|
self.assertEquals(
|
||||||
|
resp.body,
|
||||||
|
("Object/Container/Account name longer than the allowed "
|
||||||
|
"maximum 500"))
|
||||||
|
self.assertEquals(resp.status_int, 400)
|
||||||
|
|
||||||
|
# test valid length
|
||||||
|
path = '/V1.0/a/c' + 'o' * (MAX_LENGTH - 10)
|
||||||
|
resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}
|
||||||
|
).get_response(self.test_check)
|
||||||
|
self.assertEquals(resp.status_int, 200)
|
||||||
|
self.assertEquals(resp.body, 'OK')
|
||||||
|
self.test_check = orig_test_check
|
||||||
|
|
||||||
def test_invalid_length(self):
|
def test_invalid_length(self):
|
||||||
path = '/V1.0/' + 'c' * (MAX_LENGTH - 5)
|
path = '/V1.0/' + 'c' * (MAX_LENGTH - 5)
|
||||||
resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}
|
resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}
|
||||||
).get_response(self.test_check)
|
).get_response(self.test_check)
|
||||||
self.assertEquals(
|
self.assertEquals(
|
||||||
resp.body,
|
resp.body,
|
||||||
("Object/Container name longer than the allowed maximum %s"
|
("Object/Container/Account name longer than the allowed maximum %s"
|
||||||
% self.conf['maximum_length']))
|
% self.conf['maximum_length']))
|
||||||
self.assertEquals(resp.status_int, 400)
|
self.assertEquals(resp.status_int, 400)
|
||||||
|
|
||||||
@@ -80,8 +102,8 @@ class TestNameCheckMiddleware(unittest.TestCase):
|
|||||||
self.test_check)
|
self.test_check)
|
||||||
self.assertEquals(
|
self.assertEquals(
|
||||||
resp.body,
|
resp.body,
|
||||||
("Object/Container name contains a forbidden substring "
|
("Object/Container/Account name contains a forbidden "
|
||||||
"from regular expression %s"
|
"substring from regular expression %s"
|
||||||
% self.conf['forbidden_regexp']))
|
% self.conf['forbidden_regexp']))
|
||||||
self.assertEquals(resp.status_int, 400)
|
self.assertEquals(resp.status_int, 400)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user