mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-07 03:46:38 +00:00
e1ac5e0fc8
* Document multi-tenant workspace architecture * Add OSS and commercial workspace boundaries * docs: redesign multi-tenant workspace architecture * feat(tenancy): implement workspace isolation * docs(tenancy): record verification evidence * docs(tenancy): revise single-instance SaaS topology * docs(tenancy): refine architecture options * docs: finalize cloud v2 multi-tenant decisions * feat(tenancy): establish cloud isolation foundations * feat(tenancy): harden shared cloud runtime boundaries * docs(tenancy): record final isolation verification * fix(tenancy): close isolation and permission gaps * docs(tenancy): record final isolation verification * feat(tenancy): connect cloud workspace control plane * fix(build): install git for pinned SDK * docs(cloud): update control plane verification * chore: update multi-tenant SDK pin * fix(cloud): skip legacy model sync during startup * test(cloud): preserve minimal model manager fixtures * fix(cloud): preserve authenticated account context * fix(cloud): reuse authenticated account for user info * feat(cloud): complete Workspace settings navigation * test(web): cover Workspace dropdown menu * feat(web): place workspace controls in sidebar * refactor(web): streamline workspace controls * style(web): format workspace layout test * fix(cloud): surface runtime and workspace plan status * fix(plugin): keep runtime identity stable across restarts * fix(ui): widen and center workspace switcher * fix(ui): hide roles from workspace switcher * fix(ui): align workspace switcher with sidebar entries * feat(workspace): add in-product collaboration and direct Cloud launch * style: format collaboration changes * fix(workspace): bind collaboration APIs to tenant UoW * fix(cloud): preserve Core-owned collaboration state * test(cloud): require Space identity for invite registration * feat(cloud): complete secure invitation experience * style(web): format invitation flows * fix(cloud): recover box runtime without unscoped skill reload * feat(oss): enforce invitation account and owner billing flows * style: format OSS account service * test(oss): cover invitation logout handoff * fix(oss): resolve workspace owner in scoped session * feat(cloud): harden multi-tenant runtime resources * fix(cloud): bound runtime restart storms * fix(cloud): eliminate periodic runtime CPU spikes * fix(cloud): enforce instance capacity ceilings * fix(cloud): scope public login capability discovery * fix(cloud): bound tenant maintenance and monitoring work * fix(runtime): bound tenant resource amplification * fix(deps): pin green multi-tenant plugin SDK * fix(cloud): handle unavailable skill capability * fix(security): require authentication for image file endpoint (H-2) - Changed /api/v1/files/image from AuthType.NONE to USER_TOKEN_OR_API_KEY - Added Permission.RESOURCE_VIEW requirement - Prevents unauthenticated cross-tenant file access via leaked keys - Fixes HIGH severity finding from multi-tenant security review docs: add comprehensive database migration guide - Complete migration steps for OSS → multi-tenant - Backup, execution, verification procedures - Rollback scenarios and recovery plans - Performance tuning recommendations * test: add comprehensive cross-tenant isolation tests Added 7 critical test scenarios for multi-tenant boundaries: - Cross-tenant bot access prevention - Viewer role read-only enforcement - Removed member immediate access revocation - Model provider credential isolation - WebSocket message isolation - Invitation token workspace scoping - Multi-workspace context validation These tests address P0-2 coverage gaps for: - workspaces.py (membership & invitation flows) - user.py (authentication & authorization) - websocket_chat.py (real-time isolation) - plugins.py (resource access control) docs: finalize database migration guide * fix(security): resolve M-1, M-2, M-3 security findings M-1: WebSocket authorization TOCTOU race (FIXED) - Changed _revalidate_websocket_authorization to return RequestContext - Ensures validated context is used immediately without race window - Prevents removed members from sending messages during revalidation gap M-2: Model Manager cache workspace isolation (VERIFIED) - Confirmed _CacheKey already uses 4-tuple: (instance, workspace, generation, resource) - Cache is properly scoped per workspace, no cross-tenant leakage possible - No code change needed, documented as working correctly M-3: Invitation lock workspace scoping (FIXED) - Changed lock key from token_digest to workspace_uuid:token_digest - Prevents DoS where attacker locks token in Workspace A to block Workspace B - Locks now isolated per workspace All MEDIUM severity findings from security review now resolved. * fix(cloud): unblock tenant CI and enforce knowledge quotas * fix(tenancy): scope rerank model sync --------- Co-authored-by: dadachann <185672915+dadachann@users.noreply.github.com>
744 lines
24 KiB
Python
744 lines
24 KiB
Python
"""
|
|
Unit tests for WebhookService.
|
|
|
|
Tests webhook CRUD operations including:
|
|
- Webhook listing
|
|
- Webhook creation
|
|
- Webhook retrieval by ID
|
|
- Webhook updates
|
|
- Webhook deletion
|
|
- Enabled webhooks filtering
|
|
|
|
Source: src/langbot/pkg/api/http/service/webhook.py
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import datetime
|
|
|
|
import pytest
|
|
import sqlalchemy
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
from unittest.mock import AsyncMock, Mock
|
|
from types import SimpleNamespace
|
|
|
|
from langbot.pkg.api.http.authz import WorkspaceRequiredError
|
|
from langbot.pkg.api.http.service.webhook import WebhookService
|
|
from langbot.pkg.entity.persistence.base import Base
|
|
from langbot.pkg.entity.persistence.webhook import Webhook
|
|
from langbot.pkg.entity.persistence.workspace import Workspace
|
|
|
|
|
|
pytestmark = pytest.mark.asyncio
|
|
WORKSPACE_UUID = 'workspace-a'
|
|
|
|
|
|
def _create_mock_webhook(
|
|
webhook_id: int = 1,
|
|
name: str = 'Test Webhook',
|
|
url: str = 'http://example.com/webhook',
|
|
description: str = 'Test Description',
|
|
enabled: bool = True,
|
|
) -> Mock:
|
|
"""Helper to create mock Webhook entity."""
|
|
webhook = Mock(spec=Webhook)
|
|
webhook.id = webhook_id
|
|
webhook.name = name
|
|
webhook.url = url
|
|
webhook.description = description
|
|
webhook.enabled = enabled
|
|
return webhook
|
|
|
|
|
|
def _create_mock_result(items: list = None, first_item=None, scalar_value=None):
|
|
"""Create mock result object for persistence queries."""
|
|
result = Mock()
|
|
result.all = Mock(return_value=items or [])
|
|
result.first = Mock(return_value=first_item)
|
|
result.scalar = Mock(return_value=scalar_value)
|
|
result.rowcount = 1
|
|
return result
|
|
|
|
|
|
def _create_write_result(rowcount: int = 1, inserted_id: int = 1):
|
|
result = Mock()
|
|
result.rowcount = rowcount
|
|
result.inserted_primary_key = [inserted_id]
|
|
return result
|
|
|
|
|
|
class TestWebhookServiceGetWebhooks:
|
|
"""Tests for get_webhooks method."""
|
|
|
|
async def test_get_webhooks_empty_list(self):
|
|
"""Returns empty list when no webhooks exist."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
mock_result = _create_mock_result([])
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
|
ap.persistence_mgr.serialize_model = Mock(
|
|
side_effect=lambda model_cls, entity: {
|
|
'id': entity.id,
|
|
'name': entity.name,
|
|
'url': entity.url,
|
|
}
|
|
)
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
result = await service.get_webhooks(WORKSPACE_UUID)
|
|
|
|
# Verify
|
|
assert result == []
|
|
|
|
async def test_get_webhooks_returns_serialized_list(self):
|
|
"""Returns serialized list of webhooks."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
|
|
webhook1 = _create_mock_webhook(webhook_id=1, name='Webhook 1')
|
|
webhook2 = _create_mock_webhook(webhook_id=2, name='Webhook 2')
|
|
|
|
mock_result = _create_mock_result([webhook1, webhook2])
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
|
ap.persistence_mgr.serialize_model = Mock(
|
|
side_effect=lambda model_cls, entity: {
|
|
'id': entity.id,
|
|
'name': entity.name,
|
|
'url': entity.url,
|
|
'description': entity.description,
|
|
'enabled': entity.enabled,
|
|
}
|
|
)
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
result = await service.get_webhooks(WORKSPACE_UUID)
|
|
|
|
# Verify
|
|
assert len(result) == 2
|
|
assert result[0]['name'] == 'Webhook 1'
|
|
assert result[1]['name'] == 'Webhook 2'
|
|
|
|
|
|
class TestWebhookServiceCreateWebhook:
|
|
"""Tests for create_webhook method."""
|
|
|
|
async def test_create_webhook_full_params(self):
|
|
"""Creates webhook with all parameters."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
|
|
# Mock insert result
|
|
insert_result = Mock()
|
|
insert_result.inserted_primary_key = [1]
|
|
|
|
# Mock select result for retrieving created webhook
|
|
created_webhook = _create_mock_webhook(
|
|
webhook_id=1,
|
|
name='New Webhook',
|
|
url='http://new.example.com/webhook',
|
|
description='New Description',
|
|
enabled=True,
|
|
)
|
|
select_result = _create_mock_result(first_item=created_webhook)
|
|
|
|
# execute_async returns different results
|
|
call_count = 0
|
|
|
|
async def mock_execute(query):
|
|
nonlocal call_count
|
|
call_count += 1
|
|
if call_count == 1:
|
|
return _create_mock_result(scalar_value=0) # Count
|
|
if call_count == 2:
|
|
return insert_result # Insert
|
|
return select_result # Select
|
|
|
|
ap.persistence_mgr.execute_async = AsyncMock(side_effect=mock_execute)
|
|
ap.persistence_mgr.serialize_model = Mock(
|
|
return_value={
|
|
'id': 1,
|
|
'name': 'New Webhook',
|
|
'url': 'http://new.example.com/webhook',
|
|
'description': 'New Description',
|
|
'enabled': True,
|
|
}
|
|
)
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
result = await service.create_webhook(
|
|
WORKSPACE_UUID,
|
|
name='New Webhook',
|
|
url='http://new.example.com/webhook',
|
|
description='New Description',
|
|
enabled=True,
|
|
)
|
|
|
|
# Verify
|
|
assert result['name'] == 'New Webhook'
|
|
assert result['url'] == 'http://new.example.com/webhook'
|
|
assert result['description'] == 'New Description'
|
|
assert result['enabled'] is True
|
|
|
|
async def test_create_webhook_defaults(self):
|
|
"""Creates webhook with default description and enabled."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
|
|
created_webhook = _create_mock_webhook(
|
|
webhook_id=1,
|
|
name='Minimal Webhook',
|
|
url='http://minimal.example.com',
|
|
description='', # Default
|
|
enabled=True, # Default
|
|
)
|
|
|
|
call_count = 0
|
|
|
|
async def mock_execute(query):
|
|
nonlocal call_count
|
|
call_count += 1
|
|
if call_count == 1:
|
|
return _create_mock_result(scalar_value=0)
|
|
if call_count == 2:
|
|
return _create_write_result() # Insert
|
|
return _create_mock_result(first_item=created_webhook)
|
|
|
|
ap.persistence_mgr.execute_async = AsyncMock(side_effect=mock_execute)
|
|
ap.persistence_mgr.serialize_model = Mock(
|
|
return_value={
|
|
'id': 1,
|
|
'name': 'Minimal Webhook',
|
|
'url': 'http://minimal.example.com',
|
|
'description': '',
|
|
'enabled': True,
|
|
}
|
|
)
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute - only name and url required
|
|
result = await service.create_webhook(
|
|
WORKSPACE_UUID,
|
|
name='Minimal Webhook',
|
|
url='http://minimal.example.com',
|
|
)
|
|
|
|
# Verify defaults
|
|
assert result['description'] == ''
|
|
assert result['enabled'] is True
|
|
|
|
async def test_create_webhook_disabled(self):
|
|
"""Creates webhook with enabled=False."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
|
|
created_webhook = _create_mock_webhook(webhook_id=1, enabled=False)
|
|
|
|
call_count = 0
|
|
|
|
async def mock_execute(query):
|
|
nonlocal call_count
|
|
call_count += 1
|
|
if call_count == 1:
|
|
return _create_mock_result(scalar_value=0)
|
|
if call_count == 2:
|
|
return _create_write_result()
|
|
return _create_mock_result(first_item=created_webhook)
|
|
|
|
ap.persistence_mgr.execute_async = AsyncMock(side_effect=mock_execute)
|
|
ap.persistence_mgr.serialize_model = Mock(return_value={'id': 1, 'enabled': False})
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
result = await service.create_webhook(
|
|
WORKSPACE_UUID,
|
|
name='Disabled',
|
|
url='http://disabled.com',
|
|
enabled=False,
|
|
)
|
|
|
|
# Verify
|
|
assert result['enabled'] is False
|
|
|
|
async def test_create_webhook_rejects_workspace_at_capacity(self):
|
|
ap = SimpleNamespace(
|
|
instance_config=SimpleNamespace(
|
|
data={'webhooks': {'max_per_workspace': 2}},
|
|
),
|
|
persistence_mgr=SimpleNamespace(
|
|
execute_async=AsyncMock(return_value=_create_mock_result(scalar_value=2)),
|
|
),
|
|
)
|
|
|
|
service = WebhookService(ap)
|
|
|
|
with pytest.raises(ValueError, match=r'Maximum number of webhooks \(2\) reached'):
|
|
await service.create_webhook(
|
|
WORKSPACE_UUID,
|
|
name='Too many',
|
|
url='https://example.invalid',
|
|
)
|
|
|
|
ap.persistence_mgr.execute_async.assert_awaited_once()
|
|
|
|
async def test_max_per_workspace_clamps_invalid_and_oversized_values(self):
|
|
ap = SimpleNamespace(
|
|
instance_config=SimpleNamespace(
|
|
data={'webhooks': {'max_per_workspace': 999999}},
|
|
)
|
|
)
|
|
service = WebhookService(ap)
|
|
assert service.max_per_workspace() == 64
|
|
|
|
ap.instance_config.data['webhooks']['max_per_workspace'] = 0
|
|
assert service.max_per_workspace() == 1
|
|
|
|
ap.instance_config.data['webhooks']['max_per_workspace'] = 'invalid'
|
|
assert service.max_per_workspace() == 16
|
|
|
|
|
|
class TestWebhookServiceGetWebhook:
|
|
"""Tests for get_webhook method."""
|
|
|
|
async def test_get_webhook_by_id_found(self):
|
|
"""Returns webhook when found by ID."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
|
|
webhook = _create_mock_webhook(webhook_id=1, name='Found Webhook')
|
|
mock_result = _create_mock_result(first_item=webhook)
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
|
ap.persistence_mgr.serialize_model = Mock(
|
|
return_value={
|
|
'id': 1,
|
|
'name': 'Found Webhook',
|
|
'url': 'http://example.com/webhook',
|
|
}
|
|
)
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
result = await service.get_webhook(WORKSPACE_UUID, 1)
|
|
|
|
# Verify
|
|
assert result is not None
|
|
assert result['id'] == 1
|
|
assert result['name'] == 'Found Webhook'
|
|
|
|
async def test_get_webhook_by_id_not_found(self):
|
|
"""Returns None when webhook not found."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
|
|
mock_result = _create_mock_result(first_item=None)
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
result = await service.get_webhook(WORKSPACE_UUID, 999)
|
|
|
|
# Verify
|
|
assert result is None
|
|
|
|
async def test_get_webhook_by_id_zero(self):
|
|
"""Handles ID=0 (edge case) correctly."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
|
|
mock_result = _create_mock_result(first_item=None)
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
result = await service.get_webhook(WORKSPACE_UUID, 0)
|
|
|
|
# Verify - should return None (no webhook with ID 0)
|
|
assert result is None
|
|
|
|
|
|
class TestWebhookServiceUpdateWebhook:
|
|
"""Tests for update_webhook method."""
|
|
|
|
async def test_update_webhook_name_only(self):
|
|
"""Updates only the name field."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result())
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
await service.update_webhook(WORKSPACE_UUID, 1, name='Updated Name')
|
|
|
|
# Verify
|
|
ap.persistence_mgr.execute_async.assert_called_once()
|
|
|
|
async def test_update_webhook_url_only(self):
|
|
"""Updates only the url field."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result())
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
await service.update_webhook(WORKSPACE_UUID, 1, url='http://updated.example.com')
|
|
|
|
# Verify
|
|
ap.persistence_mgr.execute_async.assert_called_once()
|
|
|
|
async def test_update_webhook_description_only(self):
|
|
"""Updates only the description field."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result())
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
await service.update_webhook(WORKSPACE_UUID, 1, description='Updated description')
|
|
|
|
# Verify
|
|
ap.persistence_mgr.execute_async.assert_called_once()
|
|
|
|
async def test_update_webhook_enabled_only(self):
|
|
"""Updates only the enabled field."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result())
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
await service.update_webhook(WORKSPACE_UUID, 1, enabled=False)
|
|
|
|
# Verify
|
|
ap.persistence_mgr.execute_async.assert_called_once()
|
|
|
|
async def test_update_webhook_all_fields(self):
|
|
"""Updates all fields at once."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result())
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
await service.update_webhook(
|
|
WORKSPACE_UUID,
|
|
1,
|
|
name='All Updated',
|
|
url='http://all.updated.com',
|
|
description='All updated description',
|
|
enabled=False,
|
|
)
|
|
|
|
# Verify
|
|
ap.persistence_mgr.execute_async.assert_called_once()
|
|
|
|
async def test_update_webhook_no_fields(self):
|
|
"""Does nothing when no fields provided."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
existing = _create_mock_webhook(webhook_id=1)
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_mock_result(first_item=existing))
|
|
ap.persistence_mgr.serialize_model = Mock(return_value={'id': 1})
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute - no update parameters
|
|
await service.update_webhook(WORKSPACE_UUID, 1)
|
|
|
|
# No write is issued; one scoped existence lookup is performed.
|
|
ap.persistence_mgr.execute_async.assert_called_once()
|
|
|
|
|
|
class TestWebhookServiceDeleteWebhook:
|
|
"""Tests for delete_webhook method."""
|
|
|
|
async def test_delete_webhook_by_id(self):
|
|
"""Deletes webhook by ID."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result())
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
await service.delete_webhook(WORKSPACE_UUID, 1)
|
|
|
|
# Verify
|
|
ap.persistence_mgr.execute_async.assert_called_once()
|
|
|
|
async def test_delete_webhook_nonexistent_id(self):
|
|
"""Delete operation completes even for nonexistent ID."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result(rowcount=0))
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute - should not raise
|
|
await service.delete_webhook(WORKSPACE_UUID, 999)
|
|
|
|
# Verify - still called
|
|
ap.persistence_mgr.execute_async.assert_called_once()
|
|
|
|
|
|
class TestWebhookServiceGetEnabledWebhooks:
|
|
"""Tests for get_enabled_webhooks method."""
|
|
|
|
async def test_get_enabled_webhooks_empty(self):
|
|
"""Returns empty list when no enabled webhooks."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
mock_result = _create_mock_result([])
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
|
ap.persistence_mgr.serialize_model = Mock(return_value={})
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
result = await service.get_enabled_webhooks(WORKSPACE_UUID)
|
|
|
|
# Verify
|
|
assert result == []
|
|
|
|
async def test_get_enabled_webhooks_filters_enabled(self):
|
|
"""Returns only enabled webhooks."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
|
|
# All returned webhooks should be enabled (SQL filter)
|
|
webhook1 = _create_mock_webhook(webhook_id=1, name='Enabled 1', enabled=True)
|
|
webhook2 = _create_mock_webhook(webhook_id=2, name='Enabled 2', enabled=True)
|
|
|
|
mock_result = _create_mock_result([webhook1, webhook2])
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
|
ap.persistence_mgr.serialize_model = Mock(
|
|
side_effect=lambda model_cls, entity: {
|
|
'id': entity.id,
|
|
'name': entity.name,
|
|
'enabled': entity.enabled,
|
|
}
|
|
)
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
result = await service.get_enabled_webhooks(WORKSPACE_UUID)
|
|
|
|
# Verify
|
|
assert len(result) == 2
|
|
assert all(w['enabled'] for w in result)
|
|
|
|
async def test_get_enabled_webhooks_filters_disabled(self):
|
|
"""Does not return disabled webhooks."""
|
|
# Setup
|
|
ap = SimpleNamespace()
|
|
ap.persistence_mgr = SimpleNamespace()
|
|
|
|
# Empty result because query filters on enabled=True
|
|
mock_result = _create_mock_result([])
|
|
ap.persistence_mgr.execute_async = AsyncMock(return_value=mock_result)
|
|
ap.persistence_mgr.serialize_model = Mock(return_value={})
|
|
|
|
service = WebhookService(ap)
|
|
|
|
# Execute
|
|
result = await service.get_enabled_webhooks(WORKSPACE_UUID)
|
|
|
|
# Verify - should be empty (SQL would filter disabled)
|
|
assert result == []
|
|
|
|
|
|
ISOLATION_WORKSPACE_A = '00000000-0000-0000-0000-00000000000a'
|
|
ISOLATION_WORKSPACE_B = '00000000-0000-0000-0000-00000000000b'
|
|
|
|
|
|
class _RealPersistenceManager:
|
|
def __init__(self, engine):
|
|
self.engine = engine
|
|
|
|
async def execute_async(self, *args, **kwargs):
|
|
async with self.engine.connect() as connection:
|
|
result = await connection.execute(*args, **kwargs)
|
|
await connection.commit()
|
|
return result
|
|
|
|
@staticmethod
|
|
def serialize_model(model, data, masked_columns=None):
|
|
return {
|
|
column.name: (
|
|
getattr(data, column.name).isoformat()
|
|
if isinstance(getattr(data, column.name), datetime.datetime)
|
|
else getattr(data, column.name)
|
|
)
|
|
for column in model.__table__.columns
|
|
if column.name not in (masked_columns or [])
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
async def tenant_webhook_service(tmp_path):
|
|
engine = create_async_engine(f'sqlite+aiosqlite:///{tmp_path / "webhooks.db"}')
|
|
async with engine.begin() as connection:
|
|
await connection.run_sync(Base.metadata.create_all)
|
|
await connection.execute(
|
|
sqlalchemy.insert(Workspace),
|
|
[
|
|
{
|
|
'uuid': ISOLATION_WORKSPACE_A,
|
|
'instance_uuid': 'instance',
|
|
'name': 'A',
|
|
'slug': 'a',
|
|
'source': 'cloud_projection',
|
|
},
|
|
{
|
|
'uuid': ISOLATION_WORKSPACE_B,
|
|
'instance_uuid': 'instance',
|
|
'name': 'B',
|
|
'slug': 'b',
|
|
'source': 'cloud_projection',
|
|
},
|
|
],
|
|
)
|
|
service = WebhookService(SimpleNamespace(persistence_mgr=_RealPersistenceManager(engine)))
|
|
yield service
|
|
await engine.dispose()
|
|
|
|
|
|
async def test_webhook_service_requires_workspace(tenant_webhook_service):
|
|
with pytest.raises(WorkspaceRequiredError):
|
|
await tenant_webhook_service.get_webhooks(None)
|
|
|
|
|
|
async def test_same_name_webhooks_are_isolated(tenant_webhook_service):
|
|
created_a = await tenant_webhook_service.create_webhook(
|
|
ISOLATION_WORKSPACE_A,
|
|
'deploy',
|
|
'https://a.invalid',
|
|
)
|
|
created_b = await tenant_webhook_service.create_webhook(
|
|
ISOLATION_WORKSPACE_B,
|
|
'deploy',
|
|
'https://b.invalid',
|
|
)
|
|
|
|
assert created_a['workspace_uuid'] == ISOLATION_WORKSPACE_A
|
|
assert created_b['workspace_uuid'] == ISOLATION_WORKSPACE_B
|
|
assert [item['url'] for item in await tenant_webhook_service.get_webhooks(ISOLATION_WORKSPACE_A)] == ['***']
|
|
assert [item['url'] for item in await tenant_webhook_service.get_webhooks(ISOLATION_WORKSPACE_B)] == ['***']
|
|
assert [
|
|
item['url'] for item in await tenant_webhook_service.get_webhooks(ISOLATION_WORKSPACE_A, include_secret=True)
|
|
] == ['https://a.invalid']
|
|
assert [
|
|
item['url'] for item in await tenant_webhook_service.get_webhooks(ISOLATION_WORKSPACE_B, include_secret=True)
|
|
] == ['https://b.invalid']
|
|
|
|
|
|
async def test_cross_workspace_id_guessing_is_not_found(tenant_webhook_service):
|
|
created = await tenant_webhook_service.create_webhook(
|
|
ISOLATION_WORKSPACE_A,
|
|
'secret',
|
|
'https://a.invalid/hook',
|
|
)
|
|
webhook_id = created['id']
|
|
|
|
assert await tenant_webhook_service.get_webhook(ISOLATION_WORKSPACE_B, webhook_id) is None
|
|
assert not await tenant_webhook_service.update_webhook(
|
|
ISOLATION_WORKSPACE_B,
|
|
webhook_id,
|
|
name='stolen',
|
|
)
|
|
assert not await tenant_webhook_service.delete_webhook(ISOLATION_WORKSPACE_B, webhook_id)
|
|
assert (await tenant_webhook_service.get_webhook(ISOLATION_WORKSPACE_A, webhook_id))['name'] == 'secret'
|
|
|
|
|
|
async def test_update_and_delete_are_scoped(tenant_webhook_service):
|
|
created = await tenant_webhook_service.create_webhook(
|
|
ISOLATION_WORKSPACE_A,
|
|
'old',
|
|
'https://a.invalid/old',
|
|
)
|
|
assert await tenant_webhook_service.update_webhook(
|
|
ISOLATION_WORKSPACE_A,
|
|
created['id'],
|
|
name='new',
|
|
enabled=False,
|
|
)
|
|
assert await tenant_webhook_service.get_enabled_webhooks(ISOLATION_WORKSPACE_A) == []
|
|
assert await tenant_webhook_service.delete_webhook(ISOLATION_WORKSPACE_A, created['id'])
|
|
assert await tenant_webhook_service.get_webhook(ISOLATION_WORKSPACE_A, created['id']) is None
|
|
|
|
|
|
async def test_masked_webhook_url_roundtrip_preserves_replace_and_clear(tenant_webhook_service):
|
|
created = await tenant_webhook_service.create_webhook(
|
|
ISOLATION_WORKSPACE_A,
|
|
'roundtrip',
|
|
'https://a.invalid/bearer-secret',
|
|
)
|
|
|
|
masked = await tenant_webhook_service.get_webhook(ISOLATION_WORKSPACE_A, created['id'])
|
|
assert masked['url'] == '***'
|
|
assert await tenant_webhook_service.update_webhook(
|
|
ISOLATION_WORKSPACE_A,
|
|
created['id'],
|
|
name='preserved',
|
|
url=masked['url'],
|
|
)
|
|
preserved = await tenant_webhook_service.get_webhook(
|
|
ISOLATION_WORKSPACE_A,
|
|
created['id'],
|
|
include_secret=True,
|
|
)
|
|
assert preserved['url'] == 'https://a.invalid/bearer-secret'
|
|
|
|
assert await tenant_webhook_service.update_webhook(
|
|
ISOLATION_WORKSPACE_A,
|
|
created['id'],
|
|
url='https://a.invalid/replacement',
|
|
)
|
|
replaced = await tenant_webhook_service.get_webhook(
|
|
ISOLATION_WORKSPACE_A,
|
|
created['id'],
|
|
include_secret=True,
|
|
)
|
|
assert replaced['url'] == 'https://a.invalid/replacement'
|
|
|
|
assert await tenant_webhook_service.update_webhook(ISOLATION_WORKSPACE_A, created['id'], url='')
|
|
cleared = await tenant_webhook_service.get_webhook(
|
|
ISOLATION_WORKSPACE_A,
|
|
created['id'],
|
|
include_secret=True,
|
|
)
|
|
assert cleared['url'] == ''
|