"Fossies" - the Fresh Open Source Software Archive

Member "monasca-events-api-2.0.0/monasca_events_api/tests/unit/test_versions.py" (14 Oct 2020, 3337 Bytes) of package /linux/misc/openstack/monasca-events-api-2.0.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 last Fossies "Diffs" side-by-side code changes report for "test_versions.py": 0.3.0_vs_0.4.0.

    1 # Copyright 2017 FUJITSU LIMITED
    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 six.moves.urllib.parse import urlparse as urlparse
   16 
   17 import falcon
   18 
   19 from monasca_events_api.app.controller import versions
   20 from monasca_events_api.tests.unit import base
   21 
   22 
   23 def _get_versioned_url(version_id):
   24     return '/version/%s' % version_id
   25 
   26 
   27 class TestVersionApi(base.BaseApiTestCase):
   28 
   29     def setUp(self):
   30         super(TestVersionApi, self).setUp()
   31         self.versions = versions.Versions()
   32         self.app.add_route("/version/", self.versions)
   33         self.app.add_route("/version/{version_id}", self.versions)
   34 
   35     def test_request_for_incorrect_version(self):
   36         incorrect_version = 'v2.0'
   37         uri = _get_versioned_url(incorrect_version)
   38 
   39         res = self.simulate_request(
   40             path=uri,
   41             method='GET',
   42             headers={
   43                 'Content-Type': 'application/json'
   44             }
   45         )
   46 
   47         self.assertEqual(falcon.HTTP_400, res.status)
   48 
   49     def test_should_return_supported_event_api_version(self):
   50 
   51         def _check_global_links(current_endpoint, links):
   52             expected_links = {'self': current_endpoint,
   53                               'version': '/version',
   54                               'healthcheck': '/healthcheck'}
   55             _check_links(links, expected_links)
   56 
   57         def _check_links(links, expected_links):
   58             for link in links:
   59                 self.assertIn('rel', link)
   60                 self.assertIn('href', link)
   61                 key = link.get('rel')
   62                 href_path = urlparse(link.get('href')).path
   63                 self.assertIn(key, expected_links.keys())
   64                 self.assertEqual(expected_links[key], href_path)
   65 
   66         def _check_elements(elements, expected_versions):
   67             self.assertIsInstance(elements, list)
   68             for el in elements:
   69                 self.assertItemsEqual([
   70                     u'id',
   71                     u'links',
   72                     u'status',
   73                     u'updated'
   74                 ], el.keys())
   75                 id_v = el.get('id')
   76                 self.assertEqual(expected_versions, id_v)
   77 
   78         supported_versions = ['v1.0']
   79         version_endpoint = '/version'
   80         for version in supported_versions:
   81             endpoint = '%s/%s' % (version_endpoint, version)
   82             res = self.simulate_request(
   83                 path=endpoint,
   84                 method='GET',
   85                 headers={
   86                     'Content-Type': 'application/json'
   87                 }
   88             )
   89             self.assertEqual(falcon.HTTP_200, res.status)
   90             response = res.json
   91             self.assertIn('links', response)
   92             _check_global_links(endpoint, response['links'])
   93             self.assertIn('elements', response)
   94             _check_elements(response['elements'], version)