2022-06-04 08:41:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2024-01-15 20:20:10 +00:00
|
|
|
# type: ignore
|
2022-06-04 08:41:53 +00:00
|
|
|
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
|
2022-06-04 08:41:53 +00:00
|
|
|
|
|
|
|
|
2024-01-15 20:20:10 +00:00
|
|
|
def test_view(client: Client):
|
2022-06-04 08:41:53 +00:00
|
|
|
# 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):
|
2022-06-04 08:41:53 +00:00
|
|
|
# Given
|
2024-01-15 20:20:10 +00:00
|
|
|
single_call.pop('id')
|
2022-06-04 08:41:53 +00:00
|
|
|
|
|
|
|
# When
|
2024-01-15 20:20:10 +00:00
|
|
|
response = client.post(
|
|
|
|
'/rpc', data=single_call, content_type='application/json',
|
|
|
|
)
|
2022-06-04 08:41:53 +00:00
|
|
|
|
|
|
|
# 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):
|
2022-06-04 08:41:53 +00:00
|
|
|
# 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',
|
2022-06-04 08:41:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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',
|
2022-06-04 08:41:53 +00:00
|
|
|
'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):
|
2022-06-04 08:41:53 +00:00
|
|
|
# When
|
|
|
|
response = client.post(
|
2024-01-15 20:20:10 +00:00
|
|
|
'/rpc/private', data=single_call, content_type='application/json',
|
2022-06-04 08:41:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Then
|
|
|
|
assert response.status_code == 403
|