99# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1010# License for the specific language governing permissions and limitations
1111# under the License.
12- import random
1312
13+ import random
1414from unittest import mock
1515
16+ from neutron_lib import context
17+
18+ from neutron .db import rbac_db_models
1619from neutron .objects import address_group
1720from neutron .objects import address_scope
1821from neutron .objects import network
2629
2730class TestRBACObjectMixin (object ):
2831
32+ _test_class = None
33+ _parent_class = None
34+
2935 def get_random_object_fields (self , obj_cls = None ):
3036 fields = (super (TestRBACObjectMixin , self ).
3137 get_random_object_fields (obj_cls ))
@@ -34,6 +40,35 @@ def get_random_object_fields(self, obj_cls=None):
3440 fields ['action' ] = rnd_actions [idx ]
3541 return fields
3642
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+
3772
3873class RBACBaseObjectTestCase (neutron_test_base .BaseTestCase ):
3974
0 commit comments