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

98 lines
2.3 KiB
Python
Raw Permalink Normal View History

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