2022-06-04 08:41:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2024-01-15 20:20:10 +00:00
|
|
|
# type: ignore
|
2022-06-04 08:41:53 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
2024-01-15 20:20:10 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.test import RequestFactory
|
|
|
|
|
2022-06-04 08:41:53 +00:00
|
|
|
from bthlabs_jsonrpc_django import auth_checks
|
|
|
|
|
|
|
|
|
2024-01-15 20:20:10 +00:00
|
|
|
def test_has_perms_regular_user(rf: RequestFactory, user: User):
|
2022-06-04 08:41:53 +00:00
|
|
|
# Given
|
|
|
|
request = rf.get('/')
|
|
|
|
request.user = user
|
|
|
|
|
|
|
|
check = auth_checks.has_perms(['can_use_rpc'])
|
|
|
|
|
|
|
|
# When
|
|
|
|
result = check(request)
|
|
|
|
|
|
|
|
# Then
|
|
|
|
assert result is False
|
|
|
|
|
|
|
|
|
2024-01-15 20:20:10 +00:00
|
|
|
def test_has_perms_ok(rf: RequestFactory, user: User):
|
2022-06-04 08:41:53 +00:00
|
|
|
# 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'])
|
|
|
|
|
|
|
|
|
2024-01-15 20:20:10 +00:00
|
|
|
def test_has_perms_ok_super_user(rf: RequestFactory, super_user: User):
|
2022-06-04 08:41:53 +00:00
|
|
|
# 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
|