-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
724402c
commit 51df90a
Showing
14 changed files
with
198 additions
and
96 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
backend/tests/integration/common_utils/managers/api_key.py
This file contains 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,92 @@ | ||
from uuid import uuid4 | ||
|
||
import requests | ||
|
||
from danswer.db.models import UserRole | ||
from ee.danswer.server.api_key.models import APIKeyArgs | ||
from tests.integration.common_utils.constants import API_SERVER_URL | ||
from tests.integration.common_utils.constants import GENERAL_HEADERS | ||
from tests.integration.common_utils.test_models import TestAPIKey | ||
from tests.integration.common_utils.test_models import TestUser | ||
|
||
|
||
class APIKeyManager: | ||
@staticmethod | ||
def create( | ||
name: str | None = None, | ||
api_key_role: UserRole = UserRole.ADMIN, | ||
user_performing_action: TestUser | None = None, | ||
) -> TestAPIKey: | ||
name = f"{name}-api-key" if name else f"test-api-key-{uuid4()}" | ||
api_key_request = APIKeyArgs( | ||
name=name, | ||
role=api_key_role, | ||
) | ||
api_key_response = requests.post( | ||
f"{API_SERVER_URL}/admin/api-key", | ||
json=api_key_request.model_dump(), | ||
headers=user_performing_action.headers | ||
if user_performing_action | ||
else GENERAL_HEADERS, | ||
) | ||
api_key_response.raise_for_status() | ||
api_key = api_key_response.json() | ||
result_api_key = TestAPIKey( | ||
api_key_id=api_key["api_key_id"], | ||
api_key_display=api_key["api_key_display"], | ||
api_key=api_key["api_key"], | ||
api_key_name=name, | ||
api_key_role=api_key_role, | ||
user_id=api_key["user_id"], | ||
headers=GENERAL_HEADERS, | ||
) | ||
result_api_key.headers["Authorization"] = f"Bearer {result_api_key.api_key}" | ||
return result_api_key | ||
|
||
@staticmethod | ||
def delete( | ||
api_key: TestAPIKey, | ||
user_performing_action: TestUser | None = None, | ||
) -> None: | ||
api_key_response = requests.delete( | ||
f"{API_SERVER_URL}/admin/api-key/{api_key.api_key_id}", | ||
headers=user_performing_action.headers | ||
if user_performing_action | ||
else GENERAL_HEADERS, | ||
) | ||
api_key_response.raise_for_status() | ||
|
||
@staticmethod | ||
def get_all( | ||
user_performing_action: TestUser | None = None, | ||
) -> list[TestAPIKey]: | ||
api_key_response = requests.get( | ||
f"{API_SERVER_URL}/admin/api-key", | ||
headers=user_performing_action.headers | ||
if user_performing_action | ||
else GENERAL_HEADERS, | ||
) | ||
api_key_response.raise_for_status() | ||
return [TestAPIKey(**api_key) for api_key in api_key_response.json()] | ||
|
||
@staticmethod | ||
def verify( | ||
api_key: TestAPIKey, | ||
verify_deleted: bool = False, | ||
user_performing_action: TestUser | None = None, | ||
) -> None: | ||
retrieved_keys = APIKeyManager.get_all( | ||
user_performing_action=user_performing_action | ||
) | ||
for key in retrieved_keys: | ||
if key.api_key_id == api_key.api_key_id: | ||
if verify_deleted: | ||
raise ValueError("API Key found when it should have been deleted") | ||
if ( | ||
key.api_key_name == api_key.api_key_name | ||
and key.api_key_role == api_key.api_key_role | ||
): | ||
return | ||
|
||
if not verify_deleted: | ||
raise Exception("API Key not found") |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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.