"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-3.1.0/monasca_api/tests/test_helpers.py" (27 Sep 2019, 4992 Bytes) of package /linux/misc/openstack/monasca-api-3.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_helpers.py": 3.0.0_vs_3.1.0.

    1 # Copyright 2018 OP5 AB
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # a copy of the License at
    6 #
    7 #      http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 from falcon import errors
   16 from falcon import testing
   17 
   18 from oslo_policy import policy as os_policy
   19 
   20 from monasca_api.api.core import request
   21 from monasca_api.common.policy import policy_engine as policy
   22 from monasca_api.tests import base
   23 import monasca_api.v2.reference.helpers as helpers
   24 
   25 from monasca_common.rest import utils as rest_utils
   26 
   27 
   28 class TestHelpersFunction(base.BaseTestCase):
   29 
   30     def test_from_json(self):
   31         body_json = {'test_body': 'test'}
   32         req = request.Request(
   33             testing.create_environ(
   34                 body=rest_utils.as_json(body_json),
   35             )
   36         )
   37         response = helpers.from_json(req)
   38         self.assertEqual(body_json, response)
   39 
   40     def test_from_json_incorrect_message(self):
   41         req = request.Request(
   42             testing.create_environ(
   43                 body='incorrect message',
   44             )
   45         )
   46         self.assertRaises(errors.HTTPBadRequest, helpers.from_json, req)
   47 
   48     def test_to_json(self):
   49         test_dict = {'test_body': 'test'}
   50         expected_json = '{"test_body":"test"}'
   51         response = helpers.to_json(test_dict)
   52         self.assertEqual(expected_json, response)
   53 
   54     def test_validate_json_content_type(self):
   55         req = request.Request(
   56             testing.create_environ(
   57                 headers={'Content-Type': 'application/json'}
   58             )
   59         )
   60         helpers.validate_json_content_type(req)
   61 
   62     def test_validate_json_content_type_incorrect_content_type(self):
   63         req = request.Request(
   64             testing.create_environ(
   65                 headers={'Content-Type': 'multipart/form-data'}
   66             )
   67         )
   68         self.assertRaises(errors.HTTPBadRequest, helpers.validate_json_content_type, req)
   69 
   70     def test_validate_json_content_type_missing_content_type(self):
   71         req = request.Request(testing.create_environ())
   72         self.assertRaises(errors.HTTPBadRequest, helpers.validate_json_content_type, req)
   73 
   74 
   75 class TestGetXTenantOrTenantId(base.BaseApiTestCase):
   76     def setUp(self):
   77         super(TestGetXTenantOrTenantId, self).setUp()
   78         rules = [
   79             os_policy.RuleDefault("example:allowed", "@"),
   80             os_policy.RuleDefault("example:denied", "!"),
   81             os_policy.RuleDefault("example:authorized",
   82                                   "role:role_1 or role:role_2")
   83         ]
   84         policy.reset()
   85         policy.init()
   86         policy._ENFORCER.register_defaults(rules)
   87 
   88     def test_return_tenant_id_on_authorized_roles(self):
   89 
   90         for role in ['role_1', 'role_2']:
   91             req_context = self._get_request_context(role)
   92             self.assertEqual(
   93                 'fake_tenant_id',
   94                 helpers.get_x_tenant_or_tenant_id(
   95                     req_context, ['example:authorized']
   96                 )
   97             )
   98 
   99     def test_return_tenant_id_on_allowed_rules(self):
  100         req_context = self._get_request_context()
  101         self.assertEqual(
  102             'fake_tenant_id',
  103             helpers.get_x_tenant_or_tenant_id(
  104                 req_context,
  105                 ['example:allowed']
  106             )
  107         )
  108 
  109     def test_return_project_id_on_unauthorized_role(self):
  110         req_context = self._get_request_context()
  111         self.assertEqual('fake_project_id',
  112                          helpers.get_x_tenant_or_tenant_id(
  113                              req_context,
  114                              ['example:authorized']))
  115 
  116     def test_return_project_id_on_denied_rules(self):
  117         req_context = self._get_request_context()
  118         self.assertEqual(
  119             'fake_project_id',
  120             helpers.get_x_tenant_or_tenant_id(
  121                 req_context,
  122                 ['example:denied']
  123             )
  124         )
  125 
  126     def test_return_project_id_on_unavailable_tenant_id(self):
  127         req_context = self._get_request_context()
  128         req_context.query_string = ''
  129         self.assertEqual(
  130             'fake_project_id',
  131             helpers.get_x_tenant_or_tenant_id(
  132                 req_context,
  133                 ['example:allowed']
  134             )
  135         )
  136 
  137     @staticmethod
  138     def _get_request_context(role='fake_role'):
  139         return request.Request(
  140             testing.create_environ(
  141                 path="/",
  142                 query_string="tenant_id=fake_tenant_id",
  143                 headers={
  144                     "X_PROJECT_ID": "fake_project_id",
  145                     "X_ROLES": role
  146                 }
  147             )
  148         )