9
9
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
10
# License for the specific language governing permissions and limitations
11
11
# under the License.
12
- import random
13
12
13
+ import random
14
14
from unittest import mock
15
15
16
+ from neutron_lib import context
17
+
18
+ from neutron .db import rbac_db_models
16
19
from neutron .objects import address_group
17
20
from neutron .objects import address_scope
18
21
from neutron .objects import network
26
29
27
30
class TestRBACObjectMixin (object ):
28
31
32
+ _test_class = None
33
+ _parent_class = None
34
+
29
35
def get_random_object_fields (self , obj_cls = None ):
30
36
fields = (super (TestRBACObjectMixin , self ).
31
37
get_random_object_fields (obj_cls ))
@@ -34,6 +40,35 @@ def get_random_object_fields(self, obj_cls=None):
34
40
fields ['action' ] = rnd_actions [idx ]
35
41
return fields
36
42
43
+ def _create_random_parent_object (self ):
44
+ objclass_fields = self .get_random_db_fields (self ._parent_class )
45
+ _obj = self ._parent_class (self .context , ** objclass_fields )
46
+ _obj .create ()
47
+ return _obj
48
+
49
+ def test_rbac_shared_on_parent_object (self ):
50
+ if not self ._test_class or not self ._parent_class :
51
+ self .skipTest ('Mixin class, skipped test' )
52
+ project_id = self .objs [0 ].project_id
53
+ _obj_shared = self ._create_random_parent_object ()
54
+ # Create a second object that won't be shared and thus won't be
55
+ # retrieved by the non-admin users.
56
+ self ._create_random_parent_object ()
57
+ for idx in range (3 ):
58
+ project = 'project_%s' % idx
59
+ rbac = self ._test_class (
60
+ self .context , project_id = project_id , target_project = project ,
61
+ action = rbac_db_models .ACCESS_SHARED ,
62
+ object_id = _obj_shared .id )
63
+ rbac .create ()
64
+
65
+ for idx in range (3 ):
66
+ project = 'project_%s' % idx
67
+ ctx_no_admin = context .Context (user_id = 'user' , tenant_id = project ,
68
+ is_admin = False )
69
+ objects = self ._parent_class .get_objects (ctx_no_admin )
70
+ self .assertEqual ([_obj_shared .id ], [_obj .id for _obj in objects ])
71
+
37
72
38
73
class RBACBaseObjectTestCase (neutron_test_base .BaseTestCase ):
39
74
0 commit comments