You've already forked bthlabs-jsonrpc
Initial public releases
* `bthlabs-jsonrpc-aiohttp` v1.0.0 * `bthlabs-jsonrpc-core` v1.0.0 * `bthlabs-jsonrpc-django` v1.0.0
This commit is contained in:
0
packages/bthlabs-jsonrpc-aiohttp/tests/__init__.py
Normal file
0
packages/bthlabs-jsonrpc-aiohttp/tests/__init__.py
Normal file
10
packages/bthlabs-jsonrpc-aiohttp/tests/conftest.py
Normal file
10
packages/bthlabs-jsonrpc-aiohttp/tests/conftest.py
Normal file
@@ -0,0 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from unittest import mock
|
||||
|
||||
from aiohttp.web import Request
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_request():
|
||||
return mock.Mock(spec=Request)
|
||||
@@ -0,0 +1,170 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from unittest import mock
|
||||
|
||||
from bthlabs_jsonrpc_core import exceptions, serializer
|
||||
import pytest
|
||||
|
||||
from bthlabs_jsonrpc_aiohttp import executor
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_can_call():
|
||||
result = mock.AsyncMock()
|
||||
result.return_value = True
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def test_init(fake_request, fake_can_call):
|
||||
# When
|
||||
result = executor.AioHttpExecutor(fake_request, fake_can_call)
|
||||
|
||||
# Then
|
||||
assert result.request == fake_request
|
||||
assert result.can_call == fake_can_call
|
||||
|
||||
|
||||
async def test_list_methods(fake_request, fake_can_call):
|
||||
# Given
|
||||
the_executor = executor.AioHttpExecutor(fake_request, fake_can_call)
|
||||
|
||||
# When
|
||||
result = await the_executor.list_methods()
|
||||
|
||||
# Then
|
||||
assert result == ['system.list_methods']
|
||||
|
||||
|
||||
async def test_deserialize_data(fake_request, fake_can_call):
|
||||
# Given
|
||||
fake_request.json.return_value = 'spam'
|
||||
|
||||
the_executor = executor.AioHttpExecutor(fake_request, fake_can_call)
|
||||
|
||||
# When
|
||||
result = await the_executor.deserialize_data(fake_request)
|
||||
|
||||
# Then
|
||||
assert result == 'spam'
|
||||
|
||||
|
||||
async def test_deserialize_data_error(fake_request, fake_can_call):
|
||||
# Given
|
||||
fake_request.json.side_effect = RuntimeError('I HAZ FAIL')
|
||||
|
||||
the_executor = executor.AioHttpExecutor(fake_request, fake_can_call)
|
||||
|
||||
# When
|
||||
try:
|
||||
_ = await the_executor.deserialize_data(fake_request)
|
||||
except Exception as exception:
|
||||
assert isinstance(exception, exceptions.JSONRPCParseError)
|
||||
else:
|
||||
assert False, 'No exception raised?'
|
||||
|
||||
|
||||
def test_enrich_args(fake_request, fake_can_call):
|
||||
# Given
|
||||
the_executor = executor.AioHttpExecutor(fake_request, fake_can_call)
|
||||
|
||||
# When
|
||||
result = the_executor.enrich_args(['spam'])
|
||||
|
||||
# Then
|
||||
assert result == [fake_request, 'spam']
|
||||
|
||||
|
||||
async def test_before_call(fake_request, fake_can_call):
|
||||
# Given
|
||||
the_executor = executor.AioHttpExecutor(fake_request, fake_can_call)
|
||||
|
||||
# When
|
||||
await the_executor.before_call('test', ['spam'], {'spam': True})
|
||||
|
||||
# Then
|
||||
fake_can_call.assert_called_with(
|
||||
fake_request, 'test', ['spam'], {'spam': True},
|
||||
)
|
||||
|
||||
|
||||
async def test_before_call_access_denied(fake_request, fake_can_call):
|
||||
# Given
|
||||
fake_can_call.return_value = False
|
||||
|
||||
the_executor = executor.AioHttpExecutor(fake_request, fake_can_call)
|
||||
|
||||
# When
|
||||
try:
|
||||
await the_executor.before_call('test', ['spam'], {'spam': True})
|
||||
except Exception as exception:
|
||||
assert isinstance(exception, exceptions.JSONRPCAccessDeniedError)
|
||||
else:
|
||||
assert False, 'No exception raised?'
|
||||
|
||||
|
||||
@mock.patch('bthlabs_jsonrpc_core.registry.MethodRegistry.shared_registry')
|
||||
async def test_execute(mock_shared_registry, fake_request, fake_can_call):
|
||||
# Given
|
||||
fake_method_registry = mock.Mock()
|
||||
fake_method_registry.get_handler.return_value = None
|
||||
fake_method_registry.get_methods.return_value = []
|
||||
mock_shared_registry.return_value = fake_method_registry
|
||||
|
||||
batch = [
|
||||
{
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'call_1',
|
||||
'method': 'system.list_methods',
|
||||
'params': ['spam'],
|
||||
},
|
||||
{
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'call_2',
|
||||
'method': 'idontexist',
|
||||
},
|
||||
{
|
||||
'jsonrpc': '2.0',
|
||||
'method': 'system.list_methods',
|
||||
'params': {'spam': True},
|
||||
},
|
||||
]
|
||||
|
||||
fake_request.json.return_value = batch
|
||||
|
||||
the_executor = executor.AioHttpExecutor(fake_request, fake_can_call)
|
||||
|
||||
with mock.patch.object(the_executor, 'before_call') as mock_before_call:
|
||||
# When
|
||||
result = await the_executor.execute()
|
||||
|
||||
# Then
|
||||
assert isinstance(result, serializer.JSONRPCSerializer)
|
||||
|
||||
expected_result_data = [
|
||||
{
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'call_1',
|
||||
'result': ['system.list_methods'],
|
||||
},
|
||||
{
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'call_2',
|
||||
'error': {
|
||||
'code': exceptions.JSONRPCMethodNotFoundError.ERROR_CODE,
|
||||
'message': exceptions.JSONRPCMethodNotFoundError.ERROR_MESSAGE,
|
||||
},
|
||||
},
|
||||
]
|
||||
assert result.data == expected_result_data
|
||||
|
||||
fake_method_registry.get_handler.assert_called_with(
|
||||
'jsonrpc', 'idontexist',
|
||||
)
|
||||
|
||||
assert mock_before_call.call_count == 2
|
||||
mock_before_call.assert_any_call(
|
||||
'system.list_methods', [fake_request, 'spam'], {},
|
||||
)
|
||||
mock_before_call.assert_any_call(
|
||||
'system.list_methods', [fake_request], {'spam': True},
|
||||
)
|
||||
148
packages/bthlabs-jsonrpc-aiohttp/tests/views/test_JSONRPCView.py
Normal file
148
packages/bthlabs-jsonrpc-aiohttp/tests/views/test_JSONRPCView.py
Normal file
@@ -0,0 +1,148 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from unittest import mock
|
||||
|
||||
from aiohttp import web
|
||||
from bthlabs_jsonrpc_core import exceptions
|
||||
|
||||
from bthlabs_jsonrpc_aiohttp import views
|
||||
|
||||
|
||||
def test_init():
|
||||
# When
|
||||
result = views.JSONRPCView()
|
||||
|
||||
# Then
|
||||
assert result.namespace is None
|
||||
|
||||
|
||||
def test_init_with_namespace():
|
||||
# When
|
||||
result = views.JSONRPCView(namespace='testing')
|
||||
|
||||
# Then
|
||||
assert result.namespace == 'testing'
|
||||
|
||||
|
||||
async def test_can_call(fake_request):
|
||||
# Given
|
||||
view = views.JSONRPCView()
|
||||
|
||||
# When
|
||||
result = await view.can_call(fake_request, 'test', [], {})
|
||||
|
||||
# Then
|
||||
assert result is True
|
||||
|
||||
|
||||
async def test_view(aiohttp_client):
|
||||
# Given
|
||||
view = views.JSONRPCView()
|
||||
|
||||
app = web.Application()
|
||||
app.router.add_post('/', view)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
batch = [
|
||||
{
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'call_1',
|
||||
'method': 'system.list_methods',
|
||||
'params': ['spam'],
|
||||
},
|
||||
{
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'call_2',
|
||||
'method': 'idontexist',
|
||||
},
|
||||
{
|
||||
'jsonrpc': '2.0',
|
||||
'method': 'system.list_methods',
|
||||
'params': {'spam': True},
|
||||
},
|
||||
]
|
||||
|
||||
# When
|
||||
response = await client.post('/', json=batch)
|
||||
|
||||
# Then
|
||||
assert response.status == 200
|
||||
|
||||
data = await response.json()
|
||||
expected_result_data = [
|
||||
{
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'call_1',
|
||||
'result': ['system.list_methods'],
|
||||
},
|
||||
{
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'call_2',
|
||||
'error': {
|
||||
'code': exceptions.JSONRPCMethodNotFoundError.ERROR_CODE,
|
||||
'message': exceptions.JSONRPCMethodNotFoundError.ERROR_MESSAGE,
|
||||
},
|
||||
},
|
||||
]
|
||||
assert data == expected_result_data
|
||||
|
||||
|
||||
async def test_view_empty_response(aiohttp_client):
|
||||
# Given
|
||||
view = views.JSONRPCView()
|
||||
|
||||
app = web.Application()
|
||||
app.router.add_post('/', view)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
call = {
|
||||
'jsonrpc': '2.0',
|
||||
'method': 'system.list_methods',
|
||||
}
|
||||
|
||||
# When
|
||||
response = await client.post('/', json=call)
|
||||
|
||||
# Then
|
||||
assert response.status == 200
|
||||
|
||||
data = await response.content.read()
|
||||
assert data == b''
|
||||
|
||||
|
||||
async def test_view_permission_denied(aiohttp_client):
|
||||
# Given
|
||||
view = views.JSONRPCView()
|
||||
|
||||
app = web.Application()
|
||||
app.router.add_post('/', view)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
call = {
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'call_1',
|
||||
'method': 'system.list_methods',
|
||||
}
|
||||
|
||||
with mock.patch.object(view, 'can_call') as mock_can_call:
|
||||
mock_can_call.return_value = False
|
||||
|
||||
# When
|
||||
response = await client.post('/', json=call)
|
||||
|
||||
# Then
|
||||
assert response.status == 200
|
||||
|
||||
data = await response.json()
|
||||
expected_result_data = {
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'call_1',
|
||||
'error': {
|
||||
'code': exceptions.JSONRPCAccessDeniedError.ERROR_CODE,
|
||||
'message': exceptions.JSONRPCAccessDeniedError.ERROR_MESSAGE,
|
||||
'data': 'can_call',
|
||||
},
|
||||
}
|
||||
assert data == expected_result_data
|
||||
Reference in New Issue
Block a user