1
0
bthlabs-jsonrpc/packages/bthlabs-jsonrpc-django/tests/executor/test_DjangoExecutor.py
2024-01-15 20:20:10 +00:00

71 lines
1.7 KiB
Python

# -*- 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() -> mock.Mock:
return mock.Mock()
def test_init(rf: RequestFactory, fake_can_call: mock.Mock):
# 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: RequestFactory, fake_can_call: mock.Mock):
# 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: RequestFactory, fake_can_call: mock.Mock):
# 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: RequestFactory,
fake_can_call: mock.Mock):
# 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?'