1# Copyright 2016 IBM Corp.
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
15from novaclient import exceptions
16from novaclient.tests.unit import utils as test_utils
17
18
19class ExceptionsTestCase(test_utils.TestCase):
20
21    def _test_from_response(self, body, expected_message):
22        data = {
23            'status_code': 404,
24            'headers': {
25                'content-type': 'application/json',
26                'x-openstack-request-id': (
27                    'req-d9df03b0-4150-4b53-8157-7560ccf39f75'),
28            }
29        }
30        response = test_utils.TestResponse(data)
31        fake_url = 'http://localhost:8774/v2.1/fake/flavors/test'
32        error = exceptions.from_response(response, body, fake_url, 'GET')
33        self.assertIsInstance(error, exceptions.NotFound)
34        self.assertEqual(expected_message, error.message)
35
36    def test_from_response_webob_pre_1_6_0(self):
37        # Tests error responses before webob 1.6.0 where the error details
38        # are nested in the response body.
39        message = "Flavor test could not be found."
40        self._test_from_response(
41            {"itemNotFound": {"message": message, "code": 404}},
42            message)
43
44    def test_from_response_webob_post_1_6_0(self):
45        # Tests error responses from webob 1.6.0 where the error details
46        # are in the response body.
47        message = "Flavor test could not be found."
48        self._test_from_response({"message": message, "code": 404}, message)
49