-
Notifications
You must be signed in to change notification settings - Fork 63
test: add MaaS API key bulk revoke tests #1282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dbasunag
merged 4 commits into
opendatahub-io:main
from
SB159:test/api-key-bulk-operations
Mar 24, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
tests/model_serving/maas_billing/maas_subscription/test_api_key_bulk_operations.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
| import requests | ||
| from simple_logger.logger import get_logger | ||
|
|
||
| from tests.model_serving.maas_billing.maas_subscription.utils import ( | ||
| assert_bulk_revoke_success, | ||
| bulk_revoke_api_keys, | ||
| get_api_key, | ||
| resolve_api_key_username, | ||
| ) | ||
|
|
||
| LOGGER = get_logger(name=__name__) | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures( | ||
| "maas_subscription_controller_enabled_latest", | ||
| "maas_gateway_api", | ||
| "maas_api_gateway_reachable", | ||
| ) | ||
| class TestAPIKeyBulkOperations: | ||
| """Tests for MaaS API key bulk revoke operations.""" | ||
|
|
||
| @pytest.mark.tier1 | ||
| @pytest.mark.parametrize("ocp_token_for_actor", [{"type": "admin"}], indirect=True) | ||
| def test_bulk_revoke_own_keys( | ||
| self, | ||
| request_session_http: requests.Session, | ||
| base_url: str, | ||
| ocp_token_for_actor: str, | ||
| three_active_api_key_ids: list[str], | ||
| ) -> None: | ||
| """Verify a user can bulk revoke all their own active API keys.""" | ||
| username = resolve_api_key_username( | ||
| request_session_http=request_session_http, | ||
| base_url=base_url, | ||
| key_id=three_active_api_key_ids[0], | ||
| ocp_user_token=ocp_token_for_actor, | ||
| ) | ||
|
|
||
| revoked_count = assert_bulk_revoke_success( | ||
| request_session_http=request_session_http, | ||
| base_url=base_url, | ||
| ocp_user_token=ocp_token_for_actor, | ||
| username=username, | ||
| min_revoked_count=3, | ||
| ) | ||
| LOGGER.info(f"[bulk-revoke] User {username} bulk revoked {revoked_count} key(s)") | ||
|
|
||
| for key_id in three_active_api_key_ids: | ||
| get_resp, get_body = get_api_key( | ||
| request_session_http=request_session_http, | ||
| base_url=base_url, | ||
| key_id=key_id, | ||
| ocp_user_token=ocp_token_for_actor, | ||
| ) | ||
| assert get_resp.status_code == 200, ( | ||
| f"Expected 200 on GET /v1/api-keys/{key_id} after bulk-revoke, " | ||
| f"got {get_resp.status_code}: {get_resp.text[:200]}" | ||
| ) | ||
| assert get_body.get("status") == "revoked", ( | ||
| f"Expected key id={key_id} to have status='revoked', got: {get_body.get('status')}" | ||
| ) | ||
| LOGGER.info(f"[bulk-revoke] All {len(three_active_api_key_ids)} key(s) confirmed revoked") | ||
SB159 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @pytest.mark.tier1 | ||
| @pytest.mark.parametrize("ocp_token_for_actor", [{"type": "free"}], indirect=True) | ||
| def test_bulk_revoke_other_user_forbidden( | ||
| self, | ||
| request_session_http: requests.Session, | ||
| base_url: str, | ||
| ocp_token_for_actor: str, | ||
| ) -> None: | ||
| """Verify a non-admin user gets 403 when attempting to bulk revoke another user's keys.""" | ||
| bulk_resp, _ = bulk_revoke_api_keys( | ||
| request_session_http=request_session_http, | ||
| base_url=base_url, | ||
| ocp_user_token=ocp_token_for_actor, | ||
| username="someotheruser", | ||
| ) | ||
| assert bulk_resp.status_code == 403, ( | ||
| f"Expected 403 (non-admin cannot bulk revoke other users), " | ||
| f"got {bulk_resp.status_code}: {bulk_resp.text[:200]}" | ||
| ) | ||
| LOGGER.info("[bulk-revoke] Non-admin correctly received 403 when attempting to bulk revoke another user's keys") | ||
|
|
||
| @pytest.mark.tier1 | ||
| @pytest.mark.parametrize("ocp_token_for_actor", [{"type": "free"}], indirect=True) | ||
| def test_bulk_revoke_admin_can_revoke_any_user( | ||
| self, | ||
| request_session_http: requests.Session, | ||
| base_url: str, | ||
| active_api_key_id: str, | ||
| free_user_username: str, | ||
| admin_ocp_token: str, | ||
| ) -> None: | ||
| """Verify an admin can bulk revoke any user's active API keys.""" | ||
| revoked_count = assert_bulk_revoke_success( | ||
| request_session_http=request_session_http, | ||
| base_url=base_url, | ||
| ocp_user_token=admin_ocp_token, | ||
| username=free_user_username, | ||
| min_revoked_count=1, | ||
| ) | ||
| LOGGER.info(f"[bulk-revoke] Admin successfully revoked {revoked_count} key(s) for user {free_user_username}") | ||
SB159 marked this conversation as resolved.
Show resolved
Hide resolved
SB159 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.