1
0
bthlabs-jsonrpc/packages/bthlabs-jsonrpc-django/tests/executor/test_DjangoExecutor.py

71 lines
1.7 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
2024-01-15 20:20:10 +00:00
# type: ignore
from unittest import mock
from bthlabs_jsonrpc_core import exceptions
2024-01-15 20:20:10 +00:00
from django.test import RequestFactory
import pytest
from bthlabs_jsonrpc_django import executor
@pytest.fixture
2024-01-15 20:20:10 +00:00
def fake_can_call() -> mock.Mock:
return mock.Mock()
2024-01-15 20:20:10 +00:00
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
2024-01-15 20:20:10 +00:00
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']
2024-01-15 20:20:10 +00:00
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})
2024-01-15 20:20:10 +00:00
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?'