From 1982fa8f2f1bf06c25ad534409fbd768352f9da6 Mon Sep 17 00:00:00 2001 From: fei <204683769+feiiiiii5@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:07:34 +0800 Subject: [PATCH 1/2] fix(users): preserve user type in role revocations --- mock_tests/test_users.py | 91 ++++++++++++++++++++++++++++++++++++++++ weaviate/users/base.py | 2 +- 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 mock_tests/test_users.py diff --git a/mock_tests/test_users.py b/mock_tests/test_users.py new file mode 100644 index 000000000..e8ed51865 --- /dev/null +++ b/mock_tests/test_users.py @@ -0,0 +1,91 @@ +from typing import Any, Dict, List, Union + +import pytest +from pytest_httpserver import HTTPServer +from werkzeug import Request, Response + +import weaviate +from mock_tests.conftest import MOCK_IP, MOCK_PORT, MOCK_PORT_GRPC + + +@pytest.fixture +def revoke_requests(weaviate_mock: HTTPServer) -> List[Dict[str, Any]]: + requests: List[Dict[str, Any]] = [] + + def handler(request: Request) -> Response: + requests.append(request.get_json()) + return Response(status=200) + + weaviate_mock.expect_request( + "/v1/authz/users/alice/revoke", method="POST" + ).respond_with_handler(handler) + return requests + + +@pytest.mark.parametrize("role_names", ["reader", ["reader"]]) +@pytest.mark.parametrize( + ("user_type", "expected_body"), + [ + ("db", {"roles": ["reader"], "userType": "db"}), + ("oidc", {"roles": ["reader"], "userType": "oidc"}), + ], +) +def test_revoke_roles_user_type( + weaviate_client: weaviate.WeaviateClient, + revoke_requests: List[Dict[str, Any]], + user_type: str, + role_names: Union[str, List[str]], + expected_body: Dict[str, Any], +) -> None: + getattr(weaviate_client.users, user_type).revoke_roles(user_id="alice", role_names=role_names) + + assert revoke_requests == [expected_body] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("role_names", ["reader", ["reader"]]) +@pytest.mark.parametrize( + ("user_type", "expected_body"), + [ + ("db", {"roles": ["reader"], "userType": "db"}), + ("oidc", {"roles": ["reader"], "userType": "oidc"}), + ], +) +async def test_revoke_roles_user_type_async( + revoke_requests: List[Dict[str, Any]], + user_type: str, + role_names: Union[str, List[str]], + expected_body: Dict[str, Any], +) -> None: + async with weaviate.use_async_with_local( + port=MOCK_PORT, host=MOCK_IP, grpc_port=MOCK_PORT_GRPC, skip_init_checks=True + ) as client: + await getattr(client.users, user_type).revoke_roles(user_id="alice", role_names=role_names) + + assert revoke_requests == [expected_body] + + +@pytest.mark.parametrize("role_names", ["reader", ["reader"]]) +def test_revoke_roles_deprecated( + weaviate_client: weaviate.WeaviateClient, + revoke_requests: List[Dict[str, Any]], + role_names: Union[str, List[str]], +) -> None: + with pytest.warns(DeprecationWarning, match="users.db.revoke_roles"): + weaviate_client.users.revoke_roles(user_id="alice", role_names=role_names) + + assert revoke_requests == [{"roles": ["reader"]}] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("role_names", ["reader", ["reader"]]) +async def test_revoke_roles_deprecated_async( + revoke_requests: List[Dict[str, Any]], role_names: Union[str, List[str]] +) -> None: + async with weaviate.use_async_with_local( + port=MOCK_PORT, host=MOCK_IP, grpc_port=MOCK_PORT_GRPC, skip_init_checks=True + ) as client: + with pytest.warns(DeprecationWarning, match="users.db.revoke_roles"): + await client.users.revoke_roles(user_id="alice", role_names=role_names) + + assert revoke_requests == [{"roles": ["reader"]}] diff --git a/weaviate/users/base.py b/weaviate/users/base.py index 21692e757..2fac9715f 100644 --- a/weaviate/users/base.py +++ b/weaviate/users/base.py @@ -119,7 +119,7 @@ def resp(res: Response) -> None: response_callback=resp, method=self._connection.post, path=path, - weaviate_object={"roles": roles}, + weaviate_object=payload, error_msg=f"Could not revoke roles {roles} from user {user_id}", status_codes=_ExpectedStatusCodes(ok_in=[200], error="Revoke roles from user"), ) From bb59982a7b6036f7e8220d5423e79abb645896cd Mon Sep 17 00:00:00 2001 From: Chen Yufeiyang Date: Sat, 14 Nov 2026 14:36:42 +0800 Subject: [PATCH 2/2] test(users): cover OIDC role retention on db revocation --- integration/test_rbac.py | 8 ++++ mock_tests/test_users.py | 91 ---------------------------------------- 2 files changed, 8 insertions(+), 91 deletions(-) delete mode 100644 mock_tests/test_users.py diff --git a/integration/test_rbac.py b/integration/test_rbac.py index 6df1c162d..7f805195a 100644 --- a/integration/test_rbac.py +++ b/integration/test_rbac.py @@ -730,6 +730,7 @@ def test_get_assigned_users_db(client_factory: ClientFactory, request: SubReques permissions=Permissions.roles(role="test", read=True), ) + client.users.db.delete(user_id=names) client.users.db.create(user_id=names) client.users.db.assign_roles(user_id=names, role_names=names) @@ -738,6 +739,13 @@ def test_get_assigned_users_db(client_factory: ClientFactory, request: SubReques assert assigned_users[0].user_id == names assert assigned_users[0].user_type == UserTypes.DB_DYNAMIC + # An OIDC user with the same id keeps the role when the db user's is revoked. + client.users.oidc.assign_roles(user_id=names, role_names=names) + client.users.db.revoke_roles(user_id=names, role_names=names) + assigned_users = client.roles.get_user_assignments(names) + assert len(assigned_users) == 1 + assert assigned_users[0].user_type == UserTypes.OIDC + client.roles.delete(names) client.users.db.delete(user_id=names) diff --git a/mock_tests/test_users.py b/mock_tests/test_users.py deleted file mode 100644 index e8ed51865..000000000 --- a/mock_tests/test_users.py +++ /dev/null @@ -1,91 +0,0 @@ -from typing import Any, Dict, List, Union - -import pytest -from pytest_httpserver import HTTPServer -from werkzeug import Request, Response - -import weaviate -from mock_tests.conftest import MOCK_IP, MOCK_PORT, MOCK_PORT_GRPC - - -@pytest.fixture -def revoke_requests(weaviate_mock: HTTPServer) -> List[Dict[str, Any]]: - requests: List[Dict[str, Any]] = [] - - def handler(request: Request) -> Response: - requests.append(request.get_json()) - return Response(status=200) - - weaviate_mock.expect_request( - "/v1/authz/users/alice/revoke", method="POST" - ).respond_with_handler(handler) - return requests - - -@pytest.mark.parametrize("role_names", ["reader", ["reader"]]) -@pytest.mark.parametrize( - ("user_type", "expected_body"), - [ - ("db", {"roles": ["reader"], "userType": "db"}), - ("oidc", {"roles": ["reader"], "userType": "oidc"}), - ], -) -def test_revoke_roles_user_type( - weaviate_client: weaviate.WeaviateClient, - revoke_requests: List[Dict[str, Any]], - user_type: str, - role_names: Union[str, List[str]], - expected_body: Dict[str, Any], -) -> None: - getattr(weaviate_client.users, user_type).revoke_roles(user_id="alice", role_names=role_names) - - assert revoke_requests == [expected_body] - - -@pytest.mark.asyncio -@pytest.mark.parametrize("role_names", ["reader", ["reader"]]) -@pytest.mark.parametrize( - ("user_type", "expected_body"), - [ - ("db", {"roles": ["reader"], "userType": "db"}), - ("oidc", {"roles": ["reader"], "userType": "oidc"}), - ], -) -async def test_revoke_roles_user_type_async( - revoke_requests: List[Dict[str, Any]], - user_type: str, - role_names: Union[str, List[str]], - expected_body: Dict[str, Any], -) -> None: - async with weaviate.use_async_with_local( - port=MOCK_PORT, host=MOCK_IP, grpc_port=MOCK_PORT_GRPC, skip_init_checks=True - ) as client: - await getattr(client.users, user_type).revoke_roles(user_id="alice", role_names=role_names) - - assert revoke_requests == [expected_body] - - -@pytest.mark.parametrize("role_names", ["reader", ["reader"]]) -def test_revoke_roles_deprecated( - weaviate_client: weaviate.WeaviateClient, - revoke_requests: List[Dict[str, Any]], - role_names: Union[str, List[str]], -) -> None: - with pytest.warns(DeprecationWarning, match="users.db.revoke_roles"): - weaviate_client.users.revoke_roles(user_id="alice", role_names=role_names) - - assert revoke_requests == [{"roles": ["reader"]}] - - -@pytest.mark.asyncio -@pytest.mark.parametrize("role_names", ["reader", ["reader"]]) -async def test_revoke_roles_deprecated_async( - revoke_requests: List[Dict[str, Any]], role_names: Union[str, List[str]] -) -> None: - async with weaviate.use_async_with_local( - port=MOCK_PORT, host=MOCK_IP, grpc_port=MOCK_PORT_GRPC, skip_init_checks=True - ) as client: - with pytest.warns(DeprecationWarning, match="users.db.revoke_roles"): - await client.users.revoke_roles(user_id="alice", role_names=role_names) - - assert revoke_requests == [{"roles": ["reader"]}]