mirror of
https://github.com/dograh-hq/dograh.git
synced 2026-06-22 08:38:13 +02:00
chore: update documentation
This commit is contained in:
parent
7cc0467cfb
commit
da4a8a005a
21 changed files with 314 additions and 179 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from api.services import quota_service
|
||||
|
|
@ -284,6 +285,69 @@ async def test_authorize_workflow_run_managed_v2_stores_hosted_correlation(
|
|||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_authorize_workflow_run_service_token_from_wrong_org_prompts_new_token(
|
||||
monkeypatch,
|
||||
):
|
||||
api_key = "mps_sk_12345678"
|
||||
get_config = AsyncMock(
|
||||
return_value=_dograh_config(api_key, managed_service_version=2)
|
||||
)
|
||||
request = httpx.Request(
|
||||
"POST",
|
||||
"http://localhost:8004/api/v1/billing/accounts/42/run-authorization",
|
||||
)
|
||||
response = httpx.Response(
|
||||
403,
|
||||
json={"detail": "Service key organization mismatch"},
|
||||
request=request,
|
||||
)
|
||||
authorize = AsyncMock(
|
||||
side_effect=httpx.HTTPStatusError(
|
||||
"Failed to authorize MPS workflow run start",
|
||||
request=request,
|
||||
response=response,
|
||||
)
|
||||
)
|
||||
|
||||
monkeypatch.setattr(quota_service, "DEPLOYMENT_MODE", "saas")
|
||||
_patch_workflow_context(monkeypatch)
|
||||
monkeypatch.setattr(
|
||||
quota_service,
|
||||
"get_effective_ai_model_configuration_for_workflow",
|
||||
get_config,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
quota_service.mps_service_key_client,
|
||||
"authorize_workflow_run_start",
|
||||
authorize,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
quota_service.mps_service_key_client,
|
||||
"check_service_key_usage",
|
||||
AsyncMock(),
|
||||
)
|
||||
|
||||
result = await quota_service.authorize_workflow_run_start(
|
||||
workflow_id=7,
|
||||
workflow_run_id=88,
|
||||
)
|
||||
|
||||
assert result.has_quota is False
|
||||
assert result.error_code == "service_key_org_mismatch"
|
||||
assert result.error_message == quota_service.SERVICE_TOKEN_ORG_MISMATCH_MESSAGE
|
||||
assert "new service token from the Developers tab" in result.error_message
|
||||
authorize.assert_awaited_once_with(
|
||||
organization_id=42,
|
||||
workflow_run_id=88,
|
||||
service_key=api_key,
|
||||
require_correlation_id=True,
|
||||
minimum_credits=quota_service.MINIMUM_DOGRAH_CREDITS_FOR_CALL,
|
||||
created_by="provider-123",
|
||||
metadata={"dograh_user_id": "123", "workflow_id": 7},
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_authorize_workflow_run_oss_uses_key_paths_not_workflow_org(
|
||||
monkeypatch,
|
||||
|
|
|
|||
98
api/tests/test_user_configuration_upsert.py
Normal file
98
api/tests/test_user_configuration_upsert.py
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
import pytest
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
from api.db.models import UserConfigurationModel
|
||||
from api.db.user_client import UserClient
|
||||
from api.enums import UserConfigurationKey
|
||||
|
||||
|
||||
class _FakeResult:
|
||||
def __init__(self, value: dict):
|
||||
self._value = value
|
||||
|
||||
def scalar_one(self) -> dict:
|
||||
return self._value
|
||||
|
||||
|
||||
class _FakeSession:
|
||||
def __init__(self, result_value: dict):
|
||||
self.result_value = result_value
|
||||
self.statements = []
|
||||
self.committed = False
|
||||
self.rolled_back = False
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
return False
|
||||
|
||||
async def execute(self, stmt):
|
||||
self.statements.append(stmt)
|
||||
return _FakeResult(self.result_value)
|
||||
|
||||
async def commit(self):
|
||||
self.committed = True
|
||||
|
||||
async def rollback(self):
|
||||
self.rolled_back = True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_upsert_user_configuration_value_uses_atomic_conflict_update():
|
||||
result_value = {"completed_actions": ["web_call_started"]}
|
||||
fake_session = _FakeSession(result_value)
|
||||
client = UserClient.__new__(UserClient)
|
||||
client.async_session = lambda: fake_session
|
||||
|
||||
value = await client.upsert_user_configuration_value(
|
||||
86,
|
||||
UserConfigurationKey.ONBOARDING.value,
|
||||
result_value,
|
||||
)
|
||||
|
||||
assert value == result_value
|
||||
assert fake_session.committed is True
|
||||
assert fake_session.rolled_back is False
|
||||
assert len(fake_session.statements) == 1
|
||||
|
||||
compiled = str(fake_session.statements[0].compile(dialect=postgresql.dialect()))
|
||||
assert "ON CONFLICT ON CONSTRAINT _user_configuration_key_uc DO UPDATE" in compiled
|
||||
assert "configuration = excluded.configuration" in compiled
|
||||
assert "last_validated_at" not in compiled
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_upsert_user_configuration_value_updates_existing_row(
|
||||
db_session,
|
||||
async_session,
|
||||
):
|
||||
user, _ = await db_session.get_or_create_user_by_provider_id(
|
||||
"user-config-upsert-test"
|
||||
)
|
||||
|
||||
first = await db_session.upsert_user_configuration_value(
|
||||
user.id,
|
||||
UserConfigurationKey.ONBOARDING.value,
|
||||
{"skipped": False},
|
||||
)
|
||||
second = await db_session.upsert_user_configuration_value(
|
||||
user.id,
|
||||
UserConfigurationKey.ONBOARDING.value,
|
||||
{"skipped": True},
|
||||
)
|
||||
|
||||
assert first == {"skipped": False}
|
||||
assert second == {"skipped": True}
|
||||
|
||||
result = await async_session.execute(
|
||||
select(UserConfigurationModel).where(
|
||||
UserConfigurationModel.user_id == user.id,
|
||||
UserConfigurationModel.key == UserConfigurationKey.ONBOARDING.value,
|
||||
)
|
||||
)
|
||||
rows = result.scalars().all()
|
||||
|
||||
assert len(rows) == 1
|
||||
assert rows[0].configuration == {"skipped": True}
|
||||
Loading…
Add table
Add a link
Reference in a new issue