1
0
bthlabs-jsonrpc/packages/bthlabs-jsonrpc-django/tests/views/test_JSONRPCView.py
Tomek Wójcik c75ea4ea9d Initial public releases
* `bthlabs-jsonrpc-aiohttp` v1.0.0
* `bthlabs-jsonrpc-core` v1.0.0
* `bthlabs-jsonrpc-django` v1.0.0
2022-06-04 10:41:53 +02:00

92 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
from bthlabs_jsonrpc_core import exceptions
def test_view(client):
# Given
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 = client.post('/rpc', data=batch, content_type='application/json')
# Then
assert response.status_code == 200
data = 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
def test_view_empty_response(client, call):
# Given
call.pop('id')
# When
response = client.post('/rpc', data=call, content_type='application/json')
# Then
assert response.status_code == 200
assert response.content == b''
def test_view_with_auth_checks(client, user, call):
# Given
client.force_login(user)
# When
response = client.post(
'/rpc/private', data=call, content_type='application/json',
)
# Then
assert response.status_code == 200
data = response.json()
expected_result_data = {
'jsonrpc': '2.0',
'id': 'system.list_methods',
'result': ['system.list_methods'],
}
assert data == expected_result_data
def test_view_with_auth_checks_permission_denied(client, call):
# When
response = client.post(
'/rpc/private', data=call, content_type='application/json',
)
# Then
assert response.status_code == 403