1414# limitations under the License.
1515
1616from __future__ import absolute_import
17- import unittest2
17+ from st2tests . api import FunctionalTest
1818
19- from st2common .constants .keyvalue import SYSTEM_SCOPE , USER_SCOPE
19+ from st2common .constants .keyvalue import SYSTEM_SCOPE , FULL_SYSTEM_SCOPE
20+ from st2common .constants .keyvalue import USER_SCOPE , FULL_USER_SCOPE
2021from st2common .exceptions .keyvalue import InvalidScopeException , InvalidUserException
2122from st2common .services .keyvalues import get_key_reference
23+ from st2common .services .keyvalues import get_all_system_kvp_names_for_user
24+ from st2common .persistence .auth import User
25+ from st2common .models .db .auth import UserDB
26+ from st2common .models .db .rbac import UserRoleAssignmentDB
27+ from st2common .models .db .rbac import PermissionGrantDB
28+ from st2common .rbac .types import PermissionType
29+ from st2common .rbac .types import ResourceType
30+ from st2common .persistence .rbac import UserRoleAssignment
31+ from st2common .persistence .rbac import PermissionGrant
32+ from st2common .persistence .rbac import Role
33+ from st2common .models .db .rbac import RoleDB
2234
2335
24- class KeyValueServicesTest (unittest2 . TestCase ):
36+ class KeyValueServicesTest (FunctionalTest ):
2537 def test_get_key_reference_system_scope (self ):
2638 ref = get_key_reference (scope = SYSTEM_SCOPE , name = "foo" )
2739 self .assertEqual (ref , "foo" )
@@ -41,3 +53,114 @@ def test_get_key_reference_invalid_scope_raises_exception(self):
4153 self .assertRaises (
4254 InvalidScopeException , get_key_reference , scope = "sketchy" , name = "foo"
4355 )
56+
57+ def test_get_all_system_kvp_names_for_user (self ):
58+ user1 , user2 = "user1" , "user2"
59+ kvp_1_uid = "%s:%s:s101" % (ResourceType .KEY_VALUE_PAIR , FULL_SYSTEM_SCOPE )
60+ kvp_2_uid = "%s:%s:s102" % (ResourceType .KEY_VALUE_PAIR , FULL_SYSTEM_SCOPE )
61+ kvp_3_uid = "%s:%s:%s:u101" % (
62+ ResourceType .KEY_VALUE_PAIR ,
63+ FULL_USER_SCOPE ,
64+ user1 ,
65+ )
66+ kvp_4_uid = "%s:%s:echo" % (ResourceType .ACTION , "core" )
67+ kvp_5_uid = "%s:%s:new_action" % (ResourceType .ACTION , "dummy" )
68+ kvp_6_uid = "%s:%s:s103" % (ResourceType .KEY_VALUE_PAIR , FULL_SYSTEM_SCOPE )
69+
70+ # Setup user1, grant, role, and assignment records
71+ user_1_db = UserDB (name = user1 )
72+ user_1_db = User .add_or_update (user_1_db )
73+
74+ grant_1_db = PermissionGrantDB (
75+ resource_uid = kvp_1_uid ,
76+ resource_type = ResourceType .KEY_VALUE_PAIR ,
77+ permission_types = [PermissionType .KEY_VALUE_PAIR_LIST ],
78+ )
79+ grant_1_db = PermissionGrant .add_or_update (grant_1_db )
80+
81+ grant_2_db = PermissionGrantDB (
82+ resource_uid = kvp_2_uid ,
83+ resource_type = ResourceType .KEY_VALUE_PAIR ,
84+ permission_types = [PermissionType .KEY_VALUE_PAIR_VIEW ],
85+ )
86+ grant_2_db = PermissionGrant .add_or_update (grant_2_db )
87+
88+ grant_3_db = PermissionGrantDB (
89+ resource_uid = kvp_3_uid ,
90+ resource_type = ResourceType .KEY_VALUE_PAIR ,
91+ permission_types = [PermissionType .KEY_VALUE_PAIR_ALL ],
92+ )
93+ grant_3_db = PermissionGrant .add_or_update (grant_3_db )
94+
95+ grant_4_db = PermissionGrantDB (
96+ resource_uid = kvp_4_uid ,
97+ resource_type = ResourceType .ACTION ,
98+ permission_types = [PermissionType .ACTION_VIEW ],
99+ )
100+ grant_4_db = PermissionGrant .add_or_update (grant_4_db )
101+
102+ grant_5_db = PermissionGrantDB (
103+ resource_uid = kvp_5_uid ,
104+ resource_type = ResourceType .ACTION ,
105+ permission_types = [PermissionType .ACTION_LIST ],
106+ )
107+ grant_5_db = PermissionGrant .add_or_update (grant_5_db )
108+
109+ role_1_db = RoleDB (
110+ name = "user1_custom_role_grant" ,
111+ permission_grants = [
112+ str (grant_1_db .id ),
113+ str (grant_2_db .id ),
114+ str (grant_3_db .id ),
115+ str (grant_4_db .id ),
116+ ],
117+ )
118+ role_1_db = Role .add_or_update (role_1_db )
119+
120+ role_1_assignment_db = UserRoleAssignmentDB (
121+ user = user_1_db .name ,
122+ role = role_1_db .name ,
123+ source = "assignments/%s.yaml" % user_1_db .name ,
124+ )
125+ UserRoleAssignment .add_or_update (role_1_assignment_db )
126+
127+ # Setup user2, grant, role, and assignment records
128+ user_2_db = UserDB (name = user2 )
129+ user_2_db = User .add_or_update (user_2_db )
130+
131+ grant_6_db = PermissionGrantDB (
132+ resource_uid = kvp_6_uid ,
133+ resource_type = ResourceType .KEY_VALUE_PAIR ,
134+ permission_types = [PermissionType .KEY_VALUE_PAIR_ALL ],
135+ )
136+ grant_6_db = PermissionGrant .add_or_update (grant_6_db )
137+
138+ role_2_db = RoleDB (
139+ name = "user2_custom_role_grant" ,
140+ permission_grants = [
141+ str (grant_5_db .id ),
142+ str (grant_6_db .id ),
143+ ],
144+ )
145+ role_2_db = Role .add_or_update (role_2_db )
146+
147+ role_2_assignment_db = UserRoleAssignmentDB (
148+ user = user_2_db .name ,
149+ role = role_2_db .name ,
150+ source = "assignments/%s.yaml" % user_2_db .name ,
151+ )
152+ UserRoleAssignment .add_or_update (role_2_assignment_db )
153+
154+ # Assert result of get_all_system_kvp_names_for_user for user1
155+ # The uids for non key value pair resource type should not be included in the result.
156+ # The user scoped key should not be included in the result.
157+ actual_result = get_all_system_kvp_names_for_user (user = user_1_db .name )
158+ expected_result = ["s101" , "s102" ]
159+ self .assertListEqual (actual_result , expected_result )
160+
161+ # Assert result of get_all_system_kvp_names_for_user for user2
162+ # The uids for non key value pair resource type should not be included in the result.
163+ # The user scoped key should not be included in the result.
164+ actual_result = get_all_system_kvp_names_for_user (user = user_2_db .name )
165+ expected_result = ["s103" ]
166+ self .assertListEqual (actual_result , expected_result )
0 commit comments