|
| 1 | +"""Unit tests for the POST /agent_api_keys/webhook-trigger convenience endpoint.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from unittest.mock import AsyncMock, MagicMock |
| 6 | + |
| 7 | +import pytest |
| 8 | +import src.api.routes.agent_api_keys as mod |
| 9 | +from fastapi import HTTPException |
| 10 | +from src.api.routes.agent_api_keys import create_webhook_trigger |
| 11 | +from src.api.schemas.agent_api_keys import CreateWebhookTriggerRequest |
| 12 | +from src.domain.entities.agent_api_keys import AgentAPIKeyType |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.unit |
| 16 | +@pytest.mark.asyncio |
| 17 | +class TestCreateWebhookTrigger: |
| 18 | + async def _call(self, monkeypatch, request, *, existing=None, base_env=None): |
| 19 | + # The agent-authorization helper does real authz work; stub it to a no-op. |
| 20 | + monkeypatch.setattr(mod, "_check_agent_or_collapse_to_404", AsyncMock()) |
| 21 | + if base_env is not None: |
| 22 | + monkeypatch.setenv("AGENTEX_PUBLIC_URL", base_env) |
| 23 | + else: |
| 24 | + monkeypatch.delenv("AGENTEX_PUBLIC_URL", raising=False) |
| 25 | + |
| 26 | + agent_use_case = MagicMock() |
| 27 | + agent_use_case.get = AsyncMock(return_value=MagicMock(id="agent-1")) |
| 28 | + |
| 29 | + akuc = MagicMock() |
| 30 | + akuc.get_by_agent_id_and_name = AsyncMock(return_value=existing) |
| 31 | + akuc.create = AsyncMock( |
| 32 | + return_value=MagicMock(id="key-1", api_key_type=request.source) |
| 33 | + ) |
| 34 | + |
| 35 | + resp = await create_webhook_trigger( |
| 36 | + request=request, |
| 37 | + agent_api_key_use_case=akuc, |
| 38 | + agent_use_case=agent_use_case, |
| 39 | + authorization_service=MagicMock(), |
| 40 | + ) |
| 41 | + return resp, akuc |
| 42 | + |
| 43 | + async def test_creates_key_and_composes_url(self, monkeypatch): |
| 44 | + req = CreateWebhookTriggerRequest( |
| 45 | + agent_name="golden-agent", |
| 46 | + source=AgentAPIKeyType.GITHUB, |
| 47 | + name="acme/widgets", |
| 48 | + forward_path="github-pr/cfg-9", |
| 49 | + ) |
| 50 | + resp, akuc = await self._call( |
| 51 | + monkeypatch, req, base_env="https://sgp.example.com" |
| 52 | + ) |
| 53 | + |
| 54 | + assert len(resp.secret) >= 32 # auto-generated signing secret |
| 55 | + assert ( |
| 56 | + resp.webhook_url |
| 57 | + == "https://sgp.example.com/agents/forward/name/golden-agent/github-pr/cfg-9" |
| 58 | + ) |
| 59 | + assert resp.webhook_path == "/agents/forward/name/golden-agent/github-pr/cfg-9" |
| 60 | + assert resp.source == AgentAPIKeyType.GITHUB |
| 61 | + # key registered under the signature-lookup name + type |
| 62 | + assert akuc.create.await_args.kwargs["name"] == "acme/widgets" |
| 63 | + assert akuc.create.await_args.kwargs["api_key_type"] == AgentAPIKeyType.GITHUB |
| 64 | + assert akuc.create.await_args.kwargs["api_key"] == resp.secret |
| 65 | + |
| 66 | + async def test_uses_provided_secret_and_no_url_without_base(self, monkeypatch): |
| 67 | + req = CreateWebhookTriggerRequest( |
| 68 | + agent_name="a", |
| 69 | + source=AgentAPIKeyType.GITHUB, |
| 70 | + name="o/r", |
| 71 | + forward_path="gh", |
| 72 | + secret="mysecret", |
| 73 | + ) |
| 74 | + resp, _ = await self._call(monkeypatch, req) |
| 75 | + assert resp.secret == "mysecret" |
| 76 | + assert resp.webhook_url is None # no AGENTEX_PUBLIC_URL configured |
| 77 | + assert resp.webhook_path == "/agents/forward/name/a/gh" |
| 78 | + |
| 79 | + async def test_conflict_when_key_exists(self, monkeypatch): |
| 80 | + req = CreateWebhookTriggerRequest( |
| 81 | + agent_name="a", source=AgentAPIKeyType.GITHUB, name="o/r", forward_path="gh" |
| 82 | + ) |
| 83 | + with pytest.raises(HTTPException) as exc: |
| 84 | + await self._call(monkeypatch, req, existing=MagicMock()) |
| 85 | + assert exc.value.status_code == 409 |
| 86 | + |
| 87 | + async def test_rejects_non_webhook_source(self): |
| 88 | + req = CreateWebhookTriggerRequest( |
| 89 | + agent_name="a", source=AgentAPIKeyType.EXTERNAL, name="x", forward_path="gh" |
| 90 | + ) |
| 91 | + with pytest.raises(HTTPException) as exc: |
| 92 | + await create_webhook_trigger( |
| 93 | + request=req, |
| 94 | + agent_api_key_use_case=MagicMock(), |
| 95 | + agent_use_case=MagicMock(), |
| 96 | + authorization_service=MagicMock(), |
| 97 | + ) |
| 98 | + assert exc.value.status_code == 400 |
0 commit comments