mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 20:14:36 +00:00
Compare commits
1 Commits
fix/api-ap
...
fix/utils-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa4d46fd87 |
@@ -52,9 +52,6 @@ class ApiKeyService:
|
||||
|
||||
async def verify_api_key(self, key: str) -> bool:
|
||||
"""Verify if an API key is valid"""
|
||||
if not isinstance(key, str) or not key.startswith('lbk_'):
|
||||
return False
|
||||
|
||||
result = await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.select(apikey.ApiKey).where(apikey.ApiKey.key == key)
|
||||
)
|
||||
|
||||
@@ -83,7 +83,7 @@ def get_func_schema(function: typing.Callable) -> dict:
|
||||
|
||||
parameters['properties'][param.name] = {
|
||||
'type': param_type,
|
||||
'description': args_doc[param.name],
|
||||
'description': args_doc.get(param.name, ''),
|
||||
}
|
||||
|
||||
# add schema for array
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from langbot.pkg.api.http.service.apikey import ApiKeyService
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize('api_key', [None, 123, b'lbk_bytes', '', 'plain_key', ' LBK_bad', 'sk-lbk_fake'])
|
||||
async def test_verify_api_key_rejects_non_lbk_keys_without_db_query(api_key):
|
||||
persistence_mgr = SimpleNamespace(execute_async=AsyncMock())
|
||||
service = ApiKeyService(SimpleNamespace(persistence_mgr=persistence_mgr))
|
||||
|
||||
result = await service.verify_api_key(api_key)
|
||||
|
||||
assert result is False
|
||||
persistence_mgr.execute_async.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
('db_row', 'expected'),
|
||||
[
|
||||
(object(), True),
|
||||
(None, False),
|
||||
],
|
||||
)
|
||||
async def test_verify_api_key_keeps_db_validation_for_lbk_keys(db_row, expected):
|
||||
query_result = Mock()
|
||||
query_result.first.return_value = db_row
|
||||
persistence_mgr = SimpleNamespace(execute_async=AsyncMock(return_value=query_result))
|
||||
service = ApiKeyService(SimpleNamespace(persistence_mgr=persistence_mgr))
|
||||
|
||||
result = await service.verify_api_key('lbk_valid_format')
|
||||
|
||||
assert result is expected
|
||||
persistence_mgr.execute_async.assert_awaited_once()
|
||||
15
tests/unit_tests/utils/test_funcschema.py
Normal file
15
tests/unit_tests/utils/test_funcschema.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from langbot.pkg.utils.funcschema import get_func_schema
|
||||
|
||||
|
||||
def test_get_func_schema_uses_empty_description_for_undocumented_parameter():
|
||||
def sample_function(documented: str, undocumented: int):
|
||||
"""Sample function.
|
||||
|
||||
Args:
|
||||
documented(str): documented parameter description
|
||||
"""
|
||||
|
||||
schema = get_func_schema(sample_function)
|
||||
|
||||
assert schema['parameters']['properties']['documented']['description'] == 'documented parameter description'
|
||||
assert schema['parameters']['properties']['undocumented']['description'] == ''
|
||||
Reference in New Issue
Block a user