1
0
bthlabs-jsonrpc/packages/bthlabs-jsonrpc-django/tests/auth_checks/test_has_perms.py

56 lines
1.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2024-01-15 20:20:10 +00:00
# type: ignore
from unittest import mock
2024-01-15 20:20:10 +00:00
from django.contrib.auth.models import User
from django.test import RequestFactory
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):
# 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):
# 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):
# 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