1# Copyright 2015 Huawei Technologies India Pvt Ltd.
2# All Rights Reserved
3#
4#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5#    not use this file except in compliance with the License. You may obtain
6#    a copy of the License at
7#
8#         http://www.apache.org/licenses/LICENSE-2.0
9#
10#    Unless required by applicable law or agreed to in writing, software
11#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13#    License for the specific language governing permissions and limitations
14#    under the License.
15
16import sys
17
18import testscenarios
19
20from neutronclient.neutron.v2_0 import rbac
21from neutronclient.tests.unit import test_cli20
22
23load_tests = testscenarios.load_tests_apply_scenarios
24
25
26class CLITestV20RBACBaseJSON(test_cli20.CLITestV20Base):
27    non_admin_status_resources = ['rbac_policy']
28
29    scenarios = [
30        ('network rbac objects',
31         {'object_type_name': 'network', 'object_type_val': 'network'}),
32        ('qos policy rbac objects',
33         {'object_type_name': 'qos-policy', 'object_type_val': 'qos_policy'}),
34    ]
35
36    def test_create_rbac_policy_with_mandatory_params(self):
37        # Create rbac: rbac_object --type <object_type_name> --action
38        # access_as_shared
39        resource = 'rbac_policy'
40        cmd = rbac.CreateRBACPolicy(test_cli20.MyApp(sys.stdout), None)
41        name = 'rbac_object'
42        myid = 'myid'
43        args = [name, '--type', self.object_type_name,
44                '--action', 'access_as_shared']
45        position_names = ['object_id', 'object_type',
46                          'target_tenant', 'action']
47        position_values = [name, self.object_type_val, '*',
48                           'access_as_shared']
49        self._test_create_resource(resource, cmd, name, myid, args,
50                                   position_names, position_values)
51
52    def test_create_rbac_policy_with_all_params(self):
53        # Create rbac: rbac_object --type <object_type_name>
54        # --target-tenant tenant_id --action access_as_external
55        resource = 'rbac_policy'
56        cmd = rbac.CreateRBACPolicy(test_cli20.MyApp(sys.stdout), None)
57        name = 'rbac_object'
58        myid = 'myid'
59        args = [name, '--type', self.object_type_name,
60                '--target-tenant', 'tenant_id',
61                '--action', 'access_as_external']
62        position_names = ['object_id', 'object_type',
63                          'target_tenant', 'action']
64        position_values = [name, self.object_type_val, 'tenant_id',
65                           'access_as_external']
66        self._test_create_resource(resource, cmd, name, myid, args,
67                                   position_names, position_values)
68
69    def test_create_rbac_policy_with_unicode(self):
70        # Create rbac policy u'\u7f51\u7edc'.
71        resource = 'rbac_policy'
72        cmd = rbac.CreateRBACPolicy(test_cli20.MyApp(sys.stdout), None)
73        name = u'\u7f51\u7edc'
74        myid = 'myid'
75        args = [name, '--type', self.object_type_name,
76                '--target-tenant', 'tenant_id',
77                '--action', 'access_as_external']
78        position_names = ['object_id', 'object_type',
79                          'target_tenant', 'action']
80        position_values = [name, self.object_type_val, 'tenant_id',
81                           'access_as_external']
82        self._test_create_resource(resource, cmd, name, myid, args,
83                                   position_names, position_values)
84
85    def test_update_rbac_policy(self):
86        # rbac-update <rbac-uuid> --target-tenant <other-tenant-uuid>.
87        resource = 'rbac_policy'
88        cmd = rbac.UpdateRBACPolicy(test_cli20.MyApp(sys.stdout), None)
89        self._test_update_resource(resource, cmd, 'myid',
90                                   ['myid', '--target-tenant', 'tenant_id'],
91                                   {'target_tenant': 'tenant_id', })
92
93    def test_delete_rbac_policy(self):
94        # rbac-delete my-id.
95        resource = 'rbac_policy'
96        cmd = rbac.DeleteRBACPolicy(test_cli20.MyApp(sys.stdout), None)
97        my_id = 'myid1'
98        args = [my_id]
99        self._test_delete_resource(resource, cmd, my_id, args)
100
101    def test_list_rbac_policies(self):
102        # rbac-list.
103        resources = "rbac_policies"
104        cmd = rbac.ListRBACPolicy(test_cli20.MyApp(sys.stdout), None)
105        self._test_list_resources(resources, cmd, True)
106
107    def test_list_rbac_policies_pagination(self):
108        # rbac-list with pagination.
109        resources = "rbac_policies"
110        cmd = rbac.ListRBACPolicy(test_cli20.MyApp(sys.stdout), None)
111        self._test_list_resources_with_pagination(resources, cmd)
112
113    def test_list_rbac_policies_sort(self):
114        # sorted list:
115        # rbac-list --sort-key name --sort-key id --sort-key asc
116        # --sort-key desc
117        resources = "rbac_policies"
118        cmd = rbac.ListRBACPolicy(test_cli20.MyApp(sys.stdout), None)
119        self._test_list_resources(resources, cmd,
120                                  sort_key=["name", "id"],
121                                  sort_dir=["asc", "desc"])
122
123    def test_list_rbac_policies_limit(self):
124        # size (1000) limited list: rbac-list -P.
125        resources = "rbac_policies"
126        cmd = rbac.ListRBACPolicy(test_cli20.MyApp(sys.stdout), None)
127        self._test_list_resources(resources, cmd, page_size=1000)
128
129    def test_show_rbac_policy(self):
130        # rbac-show test_id.
131        resource = 'rbac_policy'
132        cmd = rbac.ShowRBACPolicy(test_cli20.MyApp(sys.stdout), None)
133        args = ['--fields', 'id', self.test_id]
134        self._test_show_resource(resource, cmd, self.test_id, args, ['id'])
135