Create an API client for integration tests with test_actor's API key.
(server_check, test_actor)
| 722 | |
| 723 | @pytest_asyncio.fixture(scope="module") |
| 724 | async def api_client(server_check, test_actor): |
| 725 | """Create an API client for integration tests with test_actor's API key.""" |
| 726 | import requests |
| 727 | |
| 728 | from mirix.orm.errors import NoResultFound |
| 729 | from mirix.schemas.user import User as PydanticUser |
| 730 | from mirix.security.api_keys import generate_api_key |
| 731 | from mirix.services.admin_user_manager import ClientAuthManager |
| 732 | from mirix.services.client_manager import ClientManager |
| 733 | from mirix.services.user_manager import UserManager |
| 734 | |
| 735 | # Generate and set API key for test client |
| 736 | client_mgr = ClientManager() |
| 737 | api_key = generate_api_key() |
| 738 | await client_mgr.set_client_api_key(test_actor.id, api_key) |
| 739 | |
| 740 | # Ensure admin user exists for this client |
| 741 | user_mgr = UserManager() |
| 742 | admin_user_id = ClientAuthManager.get_admin_user_id_for_client(test_actor.id) |
| 743 | try: |
| 744 | await user_mgr.get_user_by_id(admin_user_id) |
| 745 | except NoResultFound: |
| 746 | # Create admin user if it doesn't exist |
| 747 | await user_mgr.create_user( |
| 748 | PydanticUser( |
| 749 | id=admin_user_id, |
| 750 | name="Admin", |
| 751 | status="active", |
| 752 | timezone="UTC", |
| 753 | organization_id=test_actor.organization_id, |
| 754 | is_admin=True, |
| 755 | ), |
| 756 | ) |
| 757 | |
| 758 | class APIClient: |
| 759 | def __init__(self, base_url, api_key): |
| 760 | self.base_url = base_url |
| 761 | # Use X-API-Key header for programmatic access (middleware validates and injects x-client-id) |
| 762 | self.headers = {"X-API-Key": api_key} |
| 763 | |
| 764 | def get(self, path, **kwargs): |
| 765 | kwargs.setdefault("timeout", 10) |
| 766 | return requests.get(f"{self.base_url}{path}", headers=self.headers, **kwargs) |
| 767 | |
| 768 | def post(self, path, **kwargs): |
| 769 | kwargs.setdefault("timeout", 10) |
| 770 | return requests.post(f"{self.base_url}{path}", headers=self.headers, **kwargs) |
| 771 | |
| 772 | def patch(self, path, **kwargs): |
| 773 | kwargs.setdefault("timeout", 10) |
| 774 | return requests.patch(f"{self.base_url}{path}", headers=self.headers, **kwargs) |
| 775 | |
| 776 | def delete(self, path, **kwargs): |
| 777 | kwargs.setdefault("timeout", 10) |
| 778 | return requests.delete(f"{self.base_url}{path}", headers=self.headers, **kwargs) |
| 779 | |
| 780 | return APIClient("http://localhost:8000", api_key) |
| 781 |
nothing calls this directly
no test coverage detected