Allow missing '/' in api.v2.utils.do_init()
This fixes a bug in "api.v2.utils.do_init()" where an URL can be misformatted if the suffix does not have a heading '/'. Co-Authored-By: Luka Peschke <luka.peschke@objectif-libre.com> Change-Id: I40ef5e24c2294421c63f745500503af19c9a10d4 Story: 2006609 Task: 36778
This commit is contained in:
parent
fec8682c3d
commit
0f6806cbbd
@ -326,6 +326,8 @@ def do_init(app, blueprint_name, resources):
|
||||
for resource_info in schema(resources):
|
||||
resource = _load_resource(resource_info['module'],
|
||||
resource_info['resource_class'])
|
||||
if resource_info['url'] and not resource_info['url'].startswith('/'):
|
||||
resource_info['url'] = '/' + resource_info['url']
|
||||
api.add_resource(resource, resource_info['url'])
|
||||
|
||||
if not blueprint_name.startswith('/'):
|
||||
|
@ -18,6 +18,7 @@ import voluptuous
|
||||
from werkzeug.exceptions import BadRequest
|
||||
from werkzeug import MultiDict
|
||||
|
||||
from cloudkitty.api.v2.scope import state
|
||||
from cloudkitty.api.v2 import utils as api_utils
|
||||
from cloudkitty import tests
|
||||
|
||||
@ -35,6 +36,38 @@ class ApiUtilsDoInitTest(tests.TestCase):
|
||||
]
|
||||
api_utils.do_init(app, 'example', resources)
|
||||
|
||||
def test_do_init_suffix_without_heading_slash(self):
|
||||
app = flask.Flask('cloudkitty')
|
||||
resources = [
|
||||
{
|
||||
'module': 'cloudkitty.api.v2.scope.state',
|
||||
'resource_class': 'ScopeState',
|
||||
'url': 'suffix',
|
||||
},
|
||||
]
|
||||
with mock.patch.object(api_utils, '_get_blueprint_and_api') as fmock:
|
||||
blueprint_mock, api_mock = mock.MagicMock(), mock.MagicMock()
|
||||
fmock.return_value = (blueprint_mock, api_mock)
|
||||
api_utils.do_init(app, 'prefix', resources)
|
||||
api_mock.add_resource.assert_called_once_with(
|
||||
state.ScopeState, '/suffix')
|
||||
|
||||
def test_do_init_suffix_without_heading_slash_no_prefix(self):
|
||||
app = flask.Flask('cloudkitty')
|
||||
resources = [
|
||||
{
|
||||
'module': 'cloudkitty.api.v2.scope.state',
|
||||
'resource_class': 'ScopeState',
|
||||
'url': 'suffix',
|
||||
},
|
||||
]
|
||||
with mock.patch.object(api_utils, '_get_blueprint_and_api') as fmock:
|
||||
blueprint_mock, api_mock = mock.MagicMock(), mock.MagicMock()
|
||||
fmock.return_value = (blueprint_mock, api_mock)
|
||||
api_utils.do_init(app, '', resources)
|
||||
api_mock.add_resource.assert_called_once_with(
|
||||
state.ScopeState, '/suffix')
|
||||
|
||||
def test_do_init_invalid_resource(self):
|
||||
app = flask.Flask('cloudkitty')
|
||||
resources = [
|
||||
|
@ -0,0 +1,5 @@
|
||||
---
|
||||
fixes:
|
||||
- |
|
||||
It is not required anymore to prefix the url of a resource with a ``/``
|
||||
when using ``cloudkitty.api.v2.utils.do_init``.
|
Loading…
x
Reference in New Issue
Block a user