You've already forked bthlabs-jsonrpc
v1.1.0b1
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
from unittest import mock
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import RequestFactory
|
||||
|
||||
from bthlabs_jsonrpc_django import auth_checks
|
||||
|
||||
|
||||
def test_has_perms_regular_user(rf, user):
|
||||
def test_has_perms_regular_user(rf: RequestFactory, user: User):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = user
|
||||
@@ -18,7 +22,7 @@ def test_has_perms_regular_user(rf, user):
|
||||
assert result is False
|
||||
|
||||
|
||||
def test_has_perms_ok(rf, user):
|
||||
def test_has_perms_ok(rf: RequestFactory, user: User):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = user
|
||||
@@ -37,7 +41,7 @@ def test_has_perms_ok(rf, user):
|
||||
mock_has_perms.assert_called_with(['can_use_rpc'])
|
||||
|
||||
|
||||
def test_has_perms_ok_super_user(rf, super_user):
|
||||
def test_has_perms_ok_super_user(rf: RequestFactory, super_user: User):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = super_user
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import RequestFactory
|
||||
|
||||
from bthlabs_jsonrpc_django import auth_checks
|
||||
|
||||
|
||||
def test_is_authenticated_anonymous_user(rf):
|
||||
def test_is_authenticated_anonymous_user(rf: RequestFactory):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = AnonymousUser()
|
||||
@@ -16,7 +19,7 @@ def test_is_authenticated_anonymous_user(rf):
|
||||
assert result is False
|
||||
|
||||
|
||||
def test_is_authenticated_inactive(rf, inactive_user):
|
||||
def test_is_authenticated_inactive(rf: RequestFactory, inactive_user: User):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = inactive_user
|
||||
@@ -28,7 +31,7 @@ def test_is_authenticated_inactive(rf, inactive_user):
|
||||
assert result is False
|
||||
|
||||
|
||||
def test_is_authenticated_ok(rf, user):
|
||||
def test_is_authenticated_ok(rf: RequestFactory, user: User):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = user
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import RequestFactory
|
||||
|
||||
from bthlabs_jsonrpc_django import auth_checks
|
||||
|
||||
|
||||
def test_is_staff_regular_user(rf, user):
|
||||
def test_is_staff_regular_user(rf: RequestFactory, user: User):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = user
|
||||
@@ -14,7 +18,7 @@ def test_is_staff_regular_user(rf, user):
|
||||
assert result is False
|
||||
|
||||
|
||||
def test_is_staff_ok(rf, staff_user):
|
||||
def test_is_staff_ok(rf: RequestFactory, staff_user: User):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
request.user = staff_user
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
import decimal
|
||||
import datetime
|
||||
from unittest import mock
|
||||
import uuid
|
||||
|
||||
from bthlabs_jsonrpc_django import codecs
|
||||
from django.core.serializers.json import DjangoJSONEncoder
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def payload() -> dict:
|
||||
return {
|
||||
'str': 'This is a string',
|
||||
'int': 42,
|
||||
'float': 3.14,
|
||||
'decimal': decimal.Decimal('2.71828'),
|
||||
'datetime': datetime.datetime(2021, 1, 19, 8, 0, 0),
|
||||
'date': datetime.date(2022, 8, 25),
|
||||
'uuid': uuid.UUID('{ab3eacec-e205-413d-b900-940e14f61518}'),
|
||||
}
|
||||
|
||||
|
||||
def test_encode(payload: dict):
|
||||
# Given
|
||||
codec = codecs.DjangoJSONCodec()
|
||||
|
||||
# When
|
||||
result = codec.encode(payload)
|
||||
|
||||
# Then
|
||||
expected_result = (
|
||||
'{'
|
||||
'"str": "This is a string", '
|
||||
'"int": 42, '
|
||||
'"float": 3.14, '
|
||||
'"decimal": "2.71828", '
|
||||
'"datetime": "2021-01-19T08:00:00", '
|
||||
'"date": "2022-08-25", '
|
||||
'"uuid": "ab3eacec-e205-413d-b900-940e14f61518"'
|
||||
'}'
|
||||
)
|
||||
assert result == expected_result
|
||||
|
||||
|
||||
def test_encode_super_encode_call(payload: dict):
|
||||
# Given
|
||||
codec = codecs.DjangoJSONCodec()
|
||||
|
||||
with mock.patch.object(codecs.JSONCodec, 'encode') as mock_super_encode:
|
||||
# When
|
||||
_ = codec.encode(payload)
|
||||
|
||||
# Then
|
||||
mock_super_encode.assert_called_once_with(
|
||||
payload, cls=DjangoJSONEncoder,
|
||||
)
|
||||
|
||||
|
||||
def test_encode_super_encode_call_encoder_kwargs(payload: dict):
|
||||
# Given
|
||||
codec = codecs.DjangoJSONCodec()
|
||||
|
||||
with mock.patch.object(codecs.JSONCodec, 'encode') as mock_super_encode:
|
||||
# When
|
||||
_ = codec.encode(payload, cls=None)
|
||||
|
||||
# Then
|
||||
mock_super_encode.assert_called_once_with(
|
||||
payload, cls=None,
|
||||
)
|
||||
@@ -1,46 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
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
|
||||
from .factories import UserFactory
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user(db):
|
||||
def user(db) -> User:
|
||||
return UserFactory()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def inactive_user(db):
|
||||
def inactive_user(db) -> User:
|
||||
return UserFactory(is_active=False)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def staff_user(db):
|
||||
def staff_user(db) -> User:
|
||||
return UserFactory(is_staff=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def super_user(db):
|
||||
def super_user(db) -> User:
|
||||
return UserFactory(is_superuser=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def call():
|
||||
return {
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'system.list_methods',
|
||||
'method': 'system.list_methods',
|
||||
}
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
from unittest import mock
|
||||
|
||||
from bthlabs_jsonrpc_core import exceptions
|
||||
from django.test import RequestFactory
|
||||
import pytest
|
||||
|
||||
from bthlabs_jsonrpc_django import executor
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_can_call():
|
||||
def fake_can_call() -> mock.Mock:
|
||||
return mock.Mock()
|
||||
|
||||
|
||||
def test_init(rf, fake_can_call):
|
||||
def test_init(rf: RequestFactory, fake_can_call: mock.Mock):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
|
||||
@@ -24,7 +26,7 @@ def test_init(rf, fake_can_call):
|
||||
assert result.can_call == fake_can_call
|
||||
|
||||
|
||||
def test_enrich_args(rf, fake_can_call):
|
||||
def test_enrich_args(rf: RequestFactory, fake_can_call: mock.Mock):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
|
||||
@@ -37,7 +39,7 @@ def test_enrich_args(rf, fake_can_call):
|
||||
assert result == [request, 'spam']
|
||||
|
||||
|
||||
def test_before_call(rf, fake_can_call):
|
||||
def test_before_call(rf: RequestFactory, fake_can_call: mock.Mock):
|
||||
# Given
|
||||
request = rf.get('/')
|
||||
|
||||
@@ -50,7 +52,8 @@ def test_before_call(rf, fake_can_call):
|
||||
fake_can_call.assert_called_with(request, 'test', ['spam'], {'spam': True})
|
||||
|
||||
|
||||
def test_before_call_access_denied(rf, fake_can_call):
|
||||
def test_before_call_access_denied(rf: RequestFactory,
|
||||
fake_can_call: mock.Mock):
|
||||
# Given
|
||||
fake_can_call.return_value = False
|
||||
|
||||
|
||||
19
packages/bthlabs-jsonrpc-django/tests/factories.py
Normal file
19
packages/bthlabs-jsonrpc-django/tests/factories.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
from __future__ import annotations
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
import factory
|
||||
|
||||
|
||||
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
|
||||
@@ -1,4 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# type: ignore
|
||||
import pytest
|
||||
|
||||
from bthlabs_jsonrpc_django import serializer
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
# -*- 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):
|
||||
def test_view(client: Client):
|
||||
# Given
|
||||
batch = [
|
||||
{
|
||||
@@ -48,25 +51,27 @@ def test_view(client):
|
||||
assert data == expected_result_data
|
||||
|
||||
|
||||
def test_view_empty_response(client, call):
|
||||
def test_view_empty_response(client: Client, single_call: dict):
|
||||
# Given
|
||||
call.pop('id')
|
||||
single_call.pop('id')
|
||||
|
||||
# When
|
||||
response = client.post('/rpc', data=call, content_type='application/json')
|
||||
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, user, call):
|
||||
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=call, content_type='application/json',
|
||||
'/rpc/private', data=single_call, content_type='application/json',
|
||||
)
|
||||
|
||||
# Then
|
||||
@@ -75,16 +80,17 @@ def test_view_with_auth_checks(client, user, call):
|
||||
data = response.json()
|
||||
expected_result_data = {
|
||||
'jsonrpc': '2.0',
|
||||
'id': 'system.list_methods',
|
||||
'id': 'test',
|
||||
'result': ['system.list_methods'],
|
||||
}
|
||||
assert data == expected_result_data
|
||||
|
||||
|
||||
def test_view_with_auth_checks_permission_denied(client, call):
|
||||
def test_view_with_auth_checks_permission_denied(client: Client,
|
||||
single_call: dict):
|
||||
# When
|
||||
response = client.post(
|
||||
'/rpc/private', data=call, content_type='application/json',
|
||||
'/rpc/private', data=single_call, content_type='application/json',
|
||||
)
|
||||
|
||||
# Then
|
||||
|
||||
Reference in New Issue
Block a user