Make X-Project-ID a required header

We decided to make X-Project-ID a required header for every request. If
not present, a 400 error should be raised.

Partially-Implements: blueprint api-v1.1-header-changes
Change-Id: I6c6c21585e9a738ff7c0f74ce6a284229ebec2aa
This commit is contained in:
Flavio Percoco 2014-09-02 12:41:11 +02:00
parent 09bfa90fb4
commit 53733f344e
5 changed files with 26 additions and 39 deletions

View File

@ -150,7 +150,8 @@ class TestInsertQueue(base.V1_1FunctionalTestBase):
def test_insert_queue_header_asterisk(self):
"""Insert Queue with 'Accept': '*/*'."""
path = '/queues/asteriskinheader'
headers = {"Accept": '*/*'}
headers = {"Accept": '*/*',
'X-Project-ID': '518b51ea133c4facadae42c328d6b77b'}
self.addCleanup(self.client.delete, url=path, headers=headers)
result = self.client.put(path, headers=headers)

View File

@ -46,6 +46,11 @@ def extract_project_id(req, resp, params):
X-PROJECT-ID cannot be an empty string. Specify the right header X-PROJECT-ID
and retry.'''))
# TODO(flaper87): Make version comparison smarter to support v2.
if not params['project_id'] and 'v1.1' in req.path:
raise falcon.HTTPBadRequest('Project-Id Missing',
_(u'The header X-PROJECT-ID was missing'))
def validate_queue_identification(validate, req, resp, params):
"""Hook for validating the queue name and project id in requests.

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations under
# the License.
import uuid
from falcon import testing as ftest
from zaqar.openstack.common import jsonutils
@ -46,6 +48,10 @@ class TestBase(testing.TestBase):
self.srmock = ftest.StartResponseMock()
self.headers = {
'Client-ID': str(uuid.uuid4()),
}
def tearDown(self):
if self.conf.pooling:
self.boot.control.pools_controller.drop_all()
@ -65,10 +71,15 @@ class TestBase(testing.TestBase):
:returns: standard WSGI iterable response
"""
if project_id is not None:
headers = dict(kwargs['headers']) if 'headers' in kwargs else {}
headers['X-Project-ID'] = project_id
kwargs['headers'] = headers
# NOTE(flaper87): We create a copy regardless the headers
# were passed or not. This will prevent modifying `self.headers`
# in cases where simulate methods are called like:
# self.simulate_put(path, headers=self.headers)
headers = kwargs.get('headers', self.headers).copy()
project_id = ('518b51ea133c4facadae42c328d6b77b' if project_id
is None else project_id)
headers['X-Project-ID'] = headers.get('X-Project-ID', project_id)
kwargs['headers'] = headers
return self.app(ftest.create_environ(path=path, **kwargs),
self.srmock)
@ -133,24 +144,6 @@ class V1_1Base(TestBase):
def _empty_message_list(self, body):
self.assertEqual(jsonutils.loads(body[0])['messages'], [])
def simulate_request(self, path, project_id=None, **kwargs):
"""Simulate a request.
Simulates a WSGI request to the API for testing.
:param path: Request path for the desired resource
:param kwargs: Same as falcon.testing.create_environ()
:returns: standard WSGI iterable response
"""
if project_id is not None:
headers = dict(kwargs['headers']) if 'headers' in kwargs else {}
headers['X-Project-ID'] = project_id
kwargs['headers'] = headers
return self.app(ftest.create_environ(path=path, **kwargs),
self.srmock)
class V1_1BaseFaulty(TestBaseFaulty):
"""Base class for V1.1 API Faulty Tests.

View File

@ -381,11 +381,13 @@ class MessagesBaseTest(base.V1Base):
def test_no_uuid(self):
path = self.queue_path + '/messages'
self.simulate_post(path, '7e7e7e', body='[{"body": 0, "ttl": 100}]')
self.simulate_post(path, '7e7e7e',
headers={},
body='[{"body": 0, "ttl": 100}]')
self.assertEqual(self.srmock.status, falcon.HTTP_400)
self.simulate_get(path, '7e7e7e')
self.simulate_get(path, '7e7e7e', headers={})
self.assertEqual(self.srmock.status, falcon.HTTP_400)
# NOTE(cpp-cabrera): regression test against bug #1210633

View File

@ -259,26 +259,12 @@ class QueueLifecycleBaseTest(base.V1_1Base):
uri = self.queue_path + '/' + name
self.simulate_put(uri, headers=altheader, body=body)
create_queue('g1', None, '{"answer": 42}')
create_queue('g2', None, '{"answer": 42}')
create_queue('q1', project_id, '{"node": 31}')
create_queue('q2', project_id, '{"node": 32}')
create_queue('q3', project_id, '{"node": 33}')
create_queue('q3', alt_project_id, '{"alt": 1}')
# List (global queues)
result = self.simulate_get(self.queue_path,
query_string='limit=2&detailed=true')
result_doc = jsonutils.loads(result[0])
queues = result_doc['queues']
self.assertEqual(len(queues), 2)
for queue in queues:
self.assertEqual(queue['metadata'], {'answer': 42})
# List (limit)
result = self.simulate_get(self.queue_path, headers=header,
query_string='limit=2')