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-django/tests/__init_.py
Normal file
0
packages/bthlabs-jsonrpc-django/tests/__init_.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from unittest import mock
|
||||
|
||||
from bthlabs_jsonrpc_django import auth_checks
|
||||
|
||||
|
||||
def test_has_perms_regular_user(rf, user):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = user
|
||||
|
||||
check = auth_checks.has_perms(['can_use_rpc'])
|
||||
|
||||
# When
|
||||
result = check(request)
|
||||
|
||||
# Then
|
||||
assert result is False
|
||||
|
||||
|
||||
def test_has_perms_ok(rf, user):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = user
|
||||
|
||||
check = auth_checks.has_perms(['can_use_rpc'])
|
||||
|
||||
with mock.patch.object(user, 'has_perms') as mock_has_perms:
|
||||
mock_has_perms.return_value = True
|
||||
|
||||
# When
|
||||
result = check(request)
|
||||
|
||||
# Then
|
||||
assert result is True
|
||||
|
||||
mock_has_perms.assert_called_with(['can_use_rpc'])
|
||||
|
||||
|
||||
def test_has_perms_ok_super_user(rf, super_user):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = super_user
|
||||
|
||||
check = auth_checks.has_perms(['can_use_rpc'])
|
||||
|
||||
# When
|
||||
result = check(request)
|
||||
|
||||
# Then
|
||||
assert result is True
|
||||
@@ -0,0 +1,40 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
|
||||
from bthlabs_jsonrpc_django import auth_checks
|
||||
|
||||
|
||||
def test_is_authenticated_anonymous_user(rf):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = AnonymousUser()
|
||||
|
||||
# When
|
||||
result = auth_checks.is_authenticated(request)
|
||||
|
||||
# Then
|
||||
assert result is False
|
||||
|
||||
|
||||
def test_is_authenticated_inactive(rf, inactive_user):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = inactive_user
|
||||
|
||||
# When
|
||||
result = auth_checks.is_authenticated(request)
|
||||
|
||||
# Then
|
||||
assert result is False
|
||||
|
||||
|
||||
def test_is_authenticated_ok(rf, user):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = user
|
||||
|
||||
# When
|
||||
result = auth_checks.is_authenticated(request)
|
||||
|
||||
# Then
|
||||
assert result is True
|
||||
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from bthlabs_jsonrpc_django import auth_checks
|
||||
|
||||
|
||||
def test_is_staff_regular_user(rf, user):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = user
|
||||
|
||||
# When
|
||||
result = auth_checks.is_staff(request)
|
||||
|
||||
# Then
|
||||
assert result is False
|
||||
|
||||
|
||||
def test_is_staff_ok(rf, staff_user):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = staff_user
|
||||
|
||||
# When
|
||||
result = auth_checks.is_staff(request)
|
||||
|
||||
# Then
|
||||
assert result is True
|
||||
46
packages/bthlabs-jsonrpc-django/tests/conftest.py
Normal file
46
packages/bthlabs-jsonrpc-django/tests/conftest.py
Normal file
@@ -0,0 +1,46 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from django.contrib.auth.models import User
|
||||
import factory
|
||||
import pytest
|
||||
|
||||
|
||||
class UserFactory(factory.django.DjangoModelFactory):
|
||||
username = factory.Faker('email')
|
||||
first_name = factory.Faker('first_name')
|
||||
last_name = factory.Faker('last_name')
|
||||
email = factory.Faker('email')
|
||||
is_staff = False
|
||||
is_superuser = False
|
||||
is_active = True
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user(db):
|
||||
return UserFactory()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def inactive_user(db):
|
||||
return UserFactory(is_active=False)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def staff_user(db):
|
||||
return UserFactory(is_staff=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def super_user(db):
|
||||
return UserFactory(is_superuser=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def call():
|
||||
return {
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'system.list_methods',
|
||||
'method': 'system.list_methods',
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from unittest import mock
|
||||
|
||||
from bthlabs_jsonrpc_core import exceptions
|
||||
import pytest
|
||||
|
||||
from bthlabs_jsonrpc_django import executor
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_can_call():
|
||||
return mock.Mock()
|
||||
|
||||
|
||||
def test_init(rf, fake_can_call):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
|
||||
# When
|
||||
result = executor.DjangoExecutor(request, fake_can_call)
|
||||
|
||||
# Then
|
||||
assert result.request == request
|
||||
assert result.can_call == fake_can_call
|
||||
|
||||
|
||||
def test_enrich_args(rf, fake_can_call):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
|
||||
the_executor = executor.DjangoExecutor(request, fake_can_call)
|
||||
|
||||
# When
|
||||
result = the_executor.enrich_args(['spam'])
|
||||
|
||||
# Then
|
||||
assert result == [request, 'spam']
|
||||
|
||||
|
||||
def test_before_call(rf, fake_can_call):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
|
||||
the_executor = executor.DjangoExecutor(request, fake_can_call)
|
||||
|
||||
# When
|
||||
the_executor.before_call('test', ['spam'], {'spam': True})
|
||||
|
||||
# Then
|
||||
fake_can_call.assert_called_with(request, 'test', ['spam'], {'spam': True})
|
||||
|
||||
|
||||
def test_before_call_access_denied(rf, fake_can_call):
|
||||
# Given
|
||||
fake_can_call.return_value = False
|
||||
|
||||
request = rf.get('/')
|
||||
|
||||
the_executor = executor.DjangoExecutor(request, fake_can_call)
|
||||
|
||||
# When
|
||||
try:
|
||||
the_executor.before_call('test', ['spam'], {'spam': True})
|
||||
except Exception as exception:
|
||||
assert isinstance(exception, exceptions.JSONRPCAccessDeniedError)
|
||||
else:
|
||||
assert False, 'No exception raised?'
|
||||
@@ -0,0 +1,32 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import pytest
|
||||
|
||||
from bthlabs_jsonrpc_django import serializer
|
||||
from testing.factories import ThingFactory
|
||||
from testing.models import Thing
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_serialize_value_query_set():
|
||||
# Given
|
||||
things = [ThingFactory() for _ in range(0, 3)]
|
||||
|
||||
query_set = Thing.objects.\
|
||||
filter(pk__in=[thing.pk for thing in things]).\
|
||||
order_by('pk')
|
||||
|
||||
the_serializer = serializer.DjangoJSONRPCSerializer('spam')
|
||||
|
||||
# When
|
||||
result = the_serializer.serialize_value(query_set)
|
||||
|
||||
# Then
|
||||
assert isinstance(result, list)
|
||||
assert len(result) == 3
|
||||
|
||||
expected_serialized_thing = things[0].to_rpc()
|
||||
expected_serialized_thing.update({
|
||||
'created_at': expected_serialized_thing['created_at'].isoformat(),
|
||||
'modified_at': expected_serialized_thing['modified_at'].isoformat(),
|
||||
})
|
||||
assert result[0] == expected_serialized_thing
|
||||
@@ -0,0 +1,91 @@
|
||||
# -*- 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
|
||||
Reference in New Issue
Block a user