1
0
Fork 0
bthlabs-jsonrpc/packages/bthlabs-jsonrpc-django/tests/views/test_JSONRPCView.py

98 lines
2.3 KiB
Python

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