71 lines
1.7 KiB
Python
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?'
|