Debian Codegraph @

osc_lib/tests/cli/test_identity.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import argparse
14 from unittest import mock
15
16 from openstack import exceptions
17 from openstack.identity.v3 import project
18 import testtools
19
20 from osc_lib.cli import identity as cli_identity
21 from osc_lib.test import base
22
23
24 class IdentityUtilsTestCase(base.TestCase):
25 def test_add_project_owner_option_to_parser(self):
26 parser = argparse.ArgumentParser()
27 cli_identity.add_project_owner_option_to_parser(parser)
28 parsed_args = parser.parse_args(
29 ['--project', 'project1', '--project-domain', 'domain1']
30 )
31 self.assertEqual('project1', parsed_args.project)
32 self.assertEqual('domain1', parsed_args.project_domain)
33
34 def test_find_project(self):
35 sdk_connection = mock.Mock()
36 sdk_find_project = sdk_connection.identity.find_project
37 sdk_find_project.return_value = mock.sentinel.project1
38
39 ret = cli_identity.find_project(sdk_connection, 'project1')
40 self.assertEqual(mock.sentinel.project1, ret)
41 sdk_find_project.assert_called_once_with(
42 'project1', ignore_missing=False, domain_id=None
43 )
44
45 def test_find_project_with_domain(self):
46 domain1 = mock.Mock()
47 domain1.id = 'id-domain1'
48
49 sdk_connection = mock.Mock()
50 sdk_find_domain = sdk_connection.identity.find_domain
51 sdk_find_domain.return_value = domain1
52 sdk_find_project = sdk_connection.identity.find_project
53 sdk_find_project.return_value = mock.sentinel.project1
54
55 ret = cli_identity.find_project(sdk_connection, 'project1', 'domain1')
56 self.assertEqual(mock.sentinel.project1, ret)
57 sdk_find_domain.assert_called_once_with(
58 'domain1', ignore_missing=False
59 )
60 sdk_find_project.assert_called_once_with(
61 'project1', ignore_missing=False, domain_id='id-domain1'
62 )
63
64 def test_find_project_with_forbidden_exception(self):
65 sdk_connection = mock.Mock()
66 sdk_find_project = sdk_connection.identity.find_project
67 exc = exceptions.HttpException()
68 exc.status_code = 403
69 sdk_find_project.side_effect = exc
70
71 ret = cli_identity.find_project(sdk_connection, 'project1')
72
73 self.assertIsInstance(ret, project.Project)
74 self.assertEqual('project1', ret.id)
75 self.assertEqual('project1', ret.name)
76
77 def test_find_project_with_generic_exception(self):
78 sdk_connection = mock.Mock()
79 sdk_find_project = sdk_connection.identity.find_project
80 exc = exceptions.HttpException()
81 # Some value other than 403.
82 exc.status_code = 499
83 sdk_find_project.side_effect = exc
84
85 with testtools.ExpectedException(exceptions.HttpException):
86 cli_identity.find_project(sdk_connection, 'project1')