Skip to content

Commit bcbce95

Browse files
committed
fix: test adding a new group
1 parent 6fd0843 commit bcbce95

1 file changed

Lines changed: 73 additions & 14 deletions

File tree

tests/model_registry/test_user_permission.py

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from typing import Self, Callable, ContextManager
2+
from typing import Self, Callable, ContextManager, Generator
33
import shlex
44
import os
55
from simple_logger.logger import get_logger
@@ -12,17 +12,19 @@
1212
from kubernetes.dynamic import DynamicClient
1313
from ocp_resources.namespace import Namespace
1414
from ocp_resources.model_registry import ModelRegistry
15+
from ocp_resources.role_binding import RoleBinding
1516
from utilities.constants import DscComponents, Protocols
1617
from mr_openapi.exceptions import ForbiddenException
1718
from model_registry import ModelRegistry as ModelRegistryClient
1819

1920
LOGGER = get_logger(name=__name__)
2021
TEST_NAMESPACE = "model-registry-test-ns"
22+
NEW_GROUP_NAME = "test-model-registry-group"
2123

2224

2325
def get_token(user_name: str, password: str, admin_client: DynamicClient) -> str:
2426
"""
25-
Get a token for a user
27+
Get an OpenShift token for a user
2628
"""
2729

2830
current_context = run_command(command=["oc", "config", "current-context"])[1].strip()
@@ -44,13 +46,13 @@ def assert_mr_client(
4446
admin_client: DynamicClient,
4547
context: ContextManager,
4648
mr_instance: ModelRegistry,
47-
mr_namespace: Namespace,
49+
mr_namespace_name: str,
4850
) -> None:
4951
"""
50-
Initiate MR client
52+
Assert that the Model Registry client can be created and used
5153
"""
5254

53-
namespace_instance = admin_client.resources.get(api_version="v1", kind="Namespace").get(name=mr_namespace)
55+
namespace_instance = admin_client.resources.get(api_version="v1", kind="Namespace").get(name=mr_namespace_name)
5456
svc = get_mr_service_by_label(client=admin_client, ns=namespace_instance, mr_instance=mr_instance)
5557
server, port = get_endpoint_from_mr_service(svc, Protocols.REST).split(":")
5658

@@ -93,6 +95,20 @@ def _context(user_name: str):
9395
return _context
9496

9597

98+
@pytest.fixture
99+
def new_group(request: pytest.FixtureRequest) -> Generator[str, None, None]:
100+
"""
101+
Fixture to create a new OpenShift group and add a user, then delete the group after the test.
102+
"""
103+
104+
group_name, user_name = request.param
105+
run_command(command=["oc", "adm", "groups", "new", group_name, user_name])
106+
try:
107+
yield group_name
108+
finally:
109+
run_command(command=["oc", "delete", "group", group_name])
110+
111+
96112
@pytest.mark.parametrize(
97113
"updated_dsc_component_state_scope_class",
98114
[
@@ -109,7 +125,7 @@ def _context(user_name: str):
109125
)
110126
class TestUserPermission:
111127
"""
112-
Test Role-based access control
128+
Test suite for verifying user and group permissions for the Model Registry.
113129
"""
114130

115131
@pytest.mark.smoke
@@ -124,15 +140,15 @@ def test_user_permission(
124140
self: Self,
125141
updated_dsc_component_state_scope_class: Namespace,
126142
model_registry_instance: ModelRegistry,
127-
model_registry_namespace: Namespace,
143+
model_registry_namespace: str,
128144
admin_client: DynamicClient,
129145
user_name: str,
130146
password: str,
131147
context_manager: ContextManager,
132148
):
133149
"""
134-
Cluster admin user should be able to access the model registry,
135-
other users should not be able to access the model registry
150+
Test that a user with permission can access the Model Registry,
151+
and a user without permission receives a ForbiddenException.
136152
"""
137153
assert model_registry_instance.name == MR_INSTANCE_NAME
138154
user_token = get_token(user_name=user_name, password=password, admin_client=admin_client)
@@ -142,7 +158,7 @@ def test_user_permission(
142158
admin_client=admin_client,
143159
context=context_manager,
144160
mr_instance=model_registry_instance,
145-
mr_namespace=model_registry_namespace,
161+
mr_namespace_name=model_registry_namespace,
146162
)
147163

148164
@pytest.mark.smoke
@@ -156,14 +172,15 @@ def test_user_added_to_group(
156172
self: Self,
157173
updated_dsc_component_state_scope_class: Namespace,
158174
model_registry_instance: ModelRegistry,
159-
model_registry_namespace: Namespace,
175+
model_registry_namespace: str,
160176
admin_client: DynamicClient,
161177
user_name: str,
162178
password: str,
163179
user_in_group_context: Callable[[str], ContextManager],
164180
):
165181
"""
166-
User can initiate MR only when they are added to the model-registry-users group
182+
Test that a user cannot access the Model Registry before being added to a group,
183+
and can access it after being added to the group.
167184
"""
168185
assert model_registry_instance.name == MR_INSTANCE_NAME
169186

@@ -175,7 +192,7 @@ def test_user_added_to_group(
175192
admin_client=admin_client,
176193
context=pytest.raises(ForbiddenException),
177194
mr_instance=model_registry_instance,
178-
mr_namespace=model_registry_namespace,
195+
mr_namespace_name=model_registry_namespace,
179196
)
180197

181198
LOGGER.info("Add user to the model registry users group")
@@ -188,5 +205,47 @@ def test_user_added_to_group(
188205
admin_client=admin_client,
189206
context=nullcontext(),
190207
mr_instance=model_registry_instance,
191-
mr_namespace=model_registry_namespace,
208+
mr_namespace_name=model_registry_namespace,
209+
)
210+
211+
@pytest.mark.smoke
212+
@pytest.mark.parametrize(
213+
"user_name, password, new_group",
214+
[
215+
("ldap-user1", os.environ.get("NON_ADMIN_PASSWORD"), (NEW_GROUP_NAME, "ldap-user1")),
216+
],
217+
indirect=["new_group"],
218+
)
219+
def test_create_group(
220+
self: Self,
221+
updated_dsc_component_state_scope_class: Namespace,
222+
model_registry_instance: ModelRegistry,
223+
model_registry_namespace: str,
224+
admin_client: DynamicClient,
225+
user_name: str,
226+
password: str,
227+
new_group: str,
228+
):
229+
"""
230+
Test creating a group, granting it model registry access, and verifying user access.
231+
"""
232+
233+
LOGGER.info("Group created and user added to it")
234+
235+
with RoleBinding(
236+
client=admin_client,
237+
namespace=model_registry_namespace,
238+
name="test-model-registry-group-edit",
239+
role_ref_name="edit",
240+
role_ref_kind="ClusterRole",
241+
subjects_kind="Group",
242+
subjects_name=NEW_GROUP_NAME,
243+
):
244+
user_token = get_token(user_name=user_name, password=password, admin_client=admin_client)
245+
assert_mr_client(
246+
user_token=user_token,
247+
admin_client=admin_client,
248+
context=nullcontext(),
249+
mr_instance=model_registry_instance,
250+
mr_namespace_name=model_registry_namespace,
192251
)

0 commit comments

Comments
 (0)