-
Notifications
You must be signed in to change notification settings - Fork 58
test: add MaaS API key expiration tests #1292
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
Merged
Changes from 2 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
153 changes: 153 additions & 0 deletions
153
tests/model_serving/maas_billing/maas_subscription/test_api_key_expiration.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,153 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
| import requests | ||
|
|
||
| from tests.model_serving.maas_billing.maas_subscription.utils import ( | ||
| assert_api_key_created_ok, | ||
| create_api_key, | ||
| get_api_key, | ||
| ) | ||
| from utilities.general import generate_random_name | ||
| from utilities.opendatahub_logger import get_logger | ||
|
|
||
| LOGGER = get_logger(name=__name__) | ||
dbasunag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| MAAS_API_KEY_MAX_EXPIRATION_DAYS = 30 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("ocp_token_for_actor", [{"type": "admin"}], indirect=True) | ||
| @pytest.mark.usefixtures( | ||
| "maas_subscription_controller_enabled_latest", | ||
| "maas_gateway_api", | ||
| "maas_api_gateway_reachable", | ||
| ) | ||
| class TestAPIKeyExpiration: | ||
| """Tests for API key expiration policy enforcement.""" | ||
|
|
||
| @pytest.mark.tier1 | ||
| def test_create_key_within_expiration_limit( | ||
| self, | ||
| request_session_http: requests.Session, | ||
| base_url: str, | ||
| ocp_token_for_actor: str, | ||
| ) -> None: | ||
| """Verify creating an API key with expiration below the limit succeeds.""" | ||
| expires_in_hours = max((MAAS_API_KEY_MAX_EXPIRATION_DAYS // 2) * 24, 24) | ||
| key_name = f"e2e-exp-within-{generate_random_name()}" | ||
|
|
||
| create_resp, create_body = create_api_key( | ||
| base_url=base_url, | ||
| ocp_user_token=ocp_token_for_actor, | ||
| request_session_http=request_session_http, | ||
| api_key_name=key_name, | ||
| expires_in=f"{expires_in_hours}h", | ||
| raise_on_error=False, | ||
| ) | ||
| assert_api_key_created_ok(create_resp, create_body, required_fields=("key", "expiresAt")) | ||
| LOGGER.info( | ||
| f"[expiration] Created key within limit: expires_in={expires_in_hours}h, " | ||
| f"expiresAt={create_body.get('expiresAt')}" | ||
| ) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @pytest.mark.tier1 | ||
| def test_create_key_at_expiration_limit( | ||
| self, | ||
| request_session_http: requests.Session, | ||
| base_url: str, | ||
| ocp_token_for_actor: str, | ||
| ) -> None: | ||
| """Verify creating an API key with expiration exactly at the limit succeeds.""" | ||
| expires_in_hours = MAAS_API_KEY_MAX_EXPIRATION_DAYS * 24 | ||
| key_name = f"e2e-exp-at-limit-{generate_random_name()}" | ||
|
|
||
| create_resp, create_body = create_api_key( | ||
| base_url=base_url, | ||
| ocp_user_token=ocp_token_for_actor, | ||
| request_session_http=request_session_http, | ||
| api_key_name=key_name, | ||
| expires_in=f"{expires_in_hours}h", | ||
| raise_on_error=False, | ||
| ) | ||
| assert_api_key_created_ok(create_resp, create_body, required_fields=("key", "expiresAt")) | ||
| LOGGER.info( | ||
| f"[expiration] Created key at limit: expires_in={expires_in_hours}h " | ||
| f"({MAAS_API_KEY_MAX_EXPIRATION_DAYS} days)" | ||
| ) | ||
|
|
||
| @pytest.mark.tier1 | ||
| def test_create_key_exceeds_expiration_limit( | ||
| self, | ||
| request_session_http: requests.Session, | ||
| base_url: str, | ||
| ocp_token_for_actor: str, | ||
| ) -> None: | ||
| """Verify creating an API key with expiration beyond the limit returns 400.""" | ||
| exceeds_days = MAAS_API_KEY_MAX_EXPIRATION_DAYS * 2 | ||
| key_name = f"e2e-exp-exceeds-{generate_random_name()}" | ||
|
|
||
| create_resp, _ = create_api_key( | ||
| base_url=base_url, | ||
| ocp_user_token=ocp_token_for_actor, | ||
| request_session_http=request_session_http, | ||
| api_key_name=key_name, | ||
| expires_in=f"{exceeds_days * 24}h", | ||
| raise_on_error=False, | ||
| ) | ||
| assert create_resp.status_code == 400, ( | ||
| f"Expected 400 for expiration exceeding limit " | ||
| f"({exceeds_days} days > {MAAS_API_KEY_MAX_EXPIRATION_DAYS} days limit), " | ||
| f"got {create_resp.status_code}: {create_resp.text[:200]}" | ||
| ) | ||
| error_text = create_resp.text.lower() | ||
| assert "exceed" in error_text or "maximum" in error_text, ( | ||
| f"Expected error body to mention 'exceed' or 'maximum': {create_resp.text[:200]}" | ||
| ) | ||
| LOGGER.info( | ||
| f"[expiration] Correctly rejected key: {exceeds_days} days > {MAAS_API_KEY_MAX_EXPIRATION_DAYS} days limit" | ||
| ) | ||
|
|
||
| @pytest.mark.tier1 | ||
| def test_create_key_without_expiration( | ||
| self, | ||
| request_session_http: requests.Session, | ||
| base_url: str, | ||
| ocp_token_for_actor: str, | ||
| active_api_key_id: str, | ||
| ) -> None: | ||
| """Verify a key created without an expiration field has no expiresAt value.""" | ||
| get_resp, get_body = get_api_key( | ||
| request_session_http=request_session_http, | ||
| base_url=base_url, | ||
| key_id=active_api_key_id, | ||
| ocp_user_token=ocp_token_for_actor, | ||
| ) | ||
| assert get_resp.status_code == 200, ( | ||
| f"Expected 200 on GET /v1/api-keys/{active_api_key_id}, got {get_resp.status_code}: {get_resp.text[:200]}" | ||
| ) | ||
| expires_at = get_body.get("expiresAt") | ||
| LOGGER.info(f"[expiration] Key without expiration field: expiresAt={expires_at!r}") | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @pytest.mark.tier2 | ||
| def test_create_key_with_short_expiration( | ||
| self, | ||
| request_session_http: requests.Session, | ||
| base_url: str, | ||
| ocp_token_for_actor: str, | ||
| short_expiration_api_key_id: str, | ||
| ) -> None: | ||
| """Verify a key created with a 1-hour expiration has a non-null expiresAt value.""" | ||
| get_resp, get_body = get_api_key( | ||
| request_session_http=request_session_http, | ||
| base_url=base_url, | ||
| key_id=short_expiration_api_key_id, | ||
| ocp_user_token=ocp_token_for_actor, | ||
| ) | ||
| assert get_resp.status_code == 200, ( | ||
| f"Expected 200 on GET /v1/api-keys/{short_expiration_api_key_id}, " | ||
| f"got {get_resp.status_code}: {get_resp.text[:200]}" | ||
| ) | ||
| assert get_body.get("expirationDate"), ( | ||
| f"Expected non-null 'expirationDate' for 1h key, got: {get_body.get('expirationDate')!r}" | ||
| ) | ||
SB159 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| LOGGER.info(f"[expiration] 1h key expirationDate={get_body['expirationDate']}") | ||
coderabbitai[bot] 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
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.