mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-21 20:06:06 +00:00
feat(tenancy): implement workspace isolation
This commit is contained in:
@@ -14,15 +14,23 @@ 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(
|
||||
@@ -47,6 +55,14 @@ def _create_mock_result(items: list = None, first_item=None):
|
||||
result = Mock()
|
||||
result.all = Mock(return_value=items or [])
|
||||
result.first = Mock(return_value=first_item)
|
||||
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
|
||||
|
||||
|
||||
@@ -71,7 +87,7 @@ class TestWebhookServiceGetWebhooks:
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
result = await service.get_webhooks()
|
||||
result = await service.get_webhooks(WORKSPACE_UUID)
|
||||
|
||||
# Verify
|
||||
assert result == []
|
||||
@@ -100,7 +116,7 @@ class TestWebhookServiceGetWebhooks:
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
result = await service.get_webhooks()
|
||||
result = await service.get_webhooks(WORKSPACE_UUID)
|
||||
|
||||
# Verify
|
||||
assert len(result) == 2
|
||||
@@ -119,6 +135,7 @@ class TestWebhookServiceCreateWebhook:
|
||||
|
||||
# Mock insert result
|
||||
insert_result = Mock()
|
||||
insert_result.inserted_primary_key = [1]
|
||||
|
||||
# Mock select result for retrieving created webhook
|
||||
created_webhook = _create_mock_webhook(
|
||||
@@ -155,6 +172,7 @@ class TestWebhookServiceCreateWebhook:
|
||||
|
||||
# Execute
|
||||
result = await service.create_webhook(
|
||||
WORKSPACE_UUID,
|
||||
name='New Webhook',
|
||||
url='http://new.example.com/webhook',
|
||||
description='New Description',
|
||||
@@ -187,7 +205,7 @@ class TestWebhookServiceCreateWebhook:
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count == 1:
|
||||
return Mock() # Insert
|
||||
return _create_write_result() # Insert
|
||||
return _create_mock_result(first_item=created_webhook)
|
||||
|
||||
ap.persistence_mgr.execute_async = AsyncMock(side_effect=mock_execute)
|
||||
@@ -204,7 +222,11 @@ class TestWebhookServiceCreateWebhook:
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute - only name and url required
|
||||
result = await service.create_webhook(name='Minimal Webhook', url='http://minimal.example.com')
|
||||
result = await service.create_webhook(
|
||||
WORKSPACE_UUID,
|
||||
name='Minimal Webhook',
|
||||
url='http://minimal.example.com',
|
||||
)
|
||||
|
||||
# Verify defaults
|
||||
assert result['description'] == ''
|
||||
@@ -224,7 +246,7 @@ class TestWebhookServiceCreateWebhook:
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count == 1:
|
||||
return Mock()
|
||||
return _create_write_result()
|
||||
return _create_mock_result(first_item=created_webhook)
|
||||
|
||||
ap.persistence_mgr.execute_async = AsyncMock(side_effect=mock_execute)
|
||||
@@ -233,7 +255,12 @@ class TestWebhookServiceCreateWebhook:
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
result = await service.create_webhook(name='Disabled', url='http://disabled.com', enabled=False)
|
||||
result = await service.create_webhook(
|
||||
WORKSPACE_UUID,
|
||||
name='Disabled',
|
||||
url='http://disabled.com',
|
||||
enabled=False,
|
||||
)
|
||||
|
||||
# Verify
|
||||
assert result['enabled'] is False
|
||||
@@ -262,7 +289,7 @@ class TestWebhookServiceGetWebhook:
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
result = await service.get_webhook(1)
|
||||
result = await service.get_webhook(WORKSPACE_UUID, 1)
|
||||
|
||||
# Verify
|
||||
assert result is not None
|
||||
@@ -281,7 +308,7 @@ class TestWebhookServiceGetWebhook:
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
result = await service.get_webhook(999)
|
||||
result = await service.get_webhook(WORKSPACE_UUID, 999)
|
||||
|
||||
# Verify
|
||||
assert result is None
|
||||
@@ -298,7 +325,7 @@ class TestWebhookServiceGetWebhook:
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
result = await service.get_webhook(0)
|
||||
result = await service.get_webhook(WORKSPACE_UUID, 0)
|
||||
|
||||
# Verify - should return None (no webhook with ID 0)
|
||||
assert result is None
|
||||
@@ -312,12 +339,12 @@ class TestWebhookServiceUpdateWebhook:
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.persistence_mgr.execute_async = AsyncMock()
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result())
|
||||
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
await service.update_webhook(1, name='Updated Name')
|
||||
await service.update_webhook(WORKSPACE_UUID, 1, name='Updated Name')
|
||||
|
||||
# Verify
|
||||
ap.persistence_mgr.execute_async.assert_called_once()
|
||||
@@ -327,12 +354,12 @@ class TestWebhookServiceUpdateWebhook:
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.persistence_mgr.execute_async = AsyncMock()
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result())
|
||||
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
await service.update_webhook(1, url='http://updated.example.com')
|
||||
await service.update_webhook(WORKSPACE_UUID, 1, url='http://updated.example.com')
|
||||
|
||||
# Verify
|
||||
ap.persistence_mgr.execute_async.assert_called_once()
|
||||
@@ -342,12 +369,12 @@ class TestWebhookServiceUpdateWebhook:
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.persistence_mgr.execute_async = AsyncMock()
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result())
|
||||
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
await service.update_webhook(1, description='Updated description')
|
||||
await service.update_webhook(WORKSPACE_UUID, 1, description='Updated description')
|
||||
|
||||
# Verify
|
||||
ap.persistence_mgr.execute_async.assert_called_once()
|
||||
@@ -357,12 +384,12 @@ class TestWebhookServiceUpdateWebhook:
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.persistence_mgr.execute_async = AsyncMock()
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result())
|
||||
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
await service.update_webhook(1, enabled=False)
|
||||
await service.update_webhook(WORKSPACE_UUID, 1, enabled=False)
|
||||
|
||||
# Verify
|
||||
ap.persistence_mgr.execute_async.assert_called_once()
|
||||
@@ -372,12 +399,13 @@ class TestWebhookServiceUpdateWebhook:
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.persistence_mgr.execute_async = AsyncMock()
|
||||
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',
|
||||
@@ -393,15 +421,17 @@ class TestWebhookServiceUpdateWebhook:
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.persistence_mgr.execute_async = AsyncMock()
|
||||
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(1)
|
||||
await service.update_webhook(WORKSPACE_UUID, 1)
|
||||
|
||||
# Verify - no execute call since no update_data
|
||||
ap.persistence_mgr.execute_async.assert_not_called()
|
||||
# No write is issued; one scoped existence lookup is performed.
|
||||
ap.persistence_mgr.execute_async.assert_called_once()
|
||||
|
||||
|
||||
class TestWebhookServiceDeleteWebhook:
|
||||
@@ -412,12 +442,12 @@ class TestWebhookServiceDeleteWebhook:
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.persistence_mgr.execute_async = AsyncMock()
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result())
|
||||
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
await service.delete_webhook(1)
|
||||
await service.delete_webhook(WORKSPACE_UUID, 1)
|
||||
|
||||
# Verify
|
||||
ap.persistence_mgr.execute_async.assert_called_once()
|
||||
@@ -427,12 +457,12 @@ class TestWebhookServiceDeleteWebhook:
|
||||
# Setup
|
||||
ap = SimpleNamespace()
|
||||
ap.persistence_mgr = SimpleNamespace()
|
||||
ap.persistence_mgr.execute_async = AsyncMock()
|
||||
ap.persistence_mgr.execute_async = AsyncMock(return_value=_create_write_result(rowcount=0))
|
||||
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute - should not raise
|
||||
await service.delete_webhook(999)
|
||||
await service.delete_webhook(WORKSPACE_UUID, 999)
|
||||
|
||||
# Verify - still called
|
||||
ap.persistence_mgr.execute_async.assert_called_once()
|
||||
@@ -453,7 +483,7 @@ class TestWebhookServiceGetEnabledWebhooks:
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
result = await service.get_enabled_webhooks()
|
||||
result = await service.get_enabled_webhooks(WORKSPACE_UUID)
|
||||
|
||||
# Verify
|
||||
assert result == []
|
||||
@@ -481,7 +511,7 @@ class TestWebhookServiceGetEnabledWebhooks:
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
result = await service.get_enabled_webhooks()
|
||||
result = await service.get_enabled_webhooks(WORKSPACE_UUID)
|
||||
|
||||
# Verify
|
||||
assert len(result) == 2
|
||||
@@ -501,7 +531,170 @@ class TestWebhookServiceGetEnabledWebhooks:
|
||||
service = WebhookService(ap)
|
||||
|
||||
# Execute
|
||||
result = await service.get_enabled_webhooks()
|
||||
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'] == ''
|
||||
|
||||
Reference in New Issue
Block a user