56 lines
1.1 KiB
Python
56 lines
1.1 KiB
Python
# -*- 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: RequestFactory, user: 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: RequestFactory, user: 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: RequestFactory, super_user: 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
|