Browse Source

test: added all unit tests

pull/355/head
Richard Nemeth 2 years ago
parent
commit
cab018fa5a
  1. 42
      tests/test_authorization.py
  2. 41
      tests/test_connection.py
  3. 20
      tests/test_exceptions.py
  4. 4
      tests/test_uma_permissions.py

42
tests/test_authorization.py

@ -0,0 +1,42 @@
"""Test authorization module."""
import pytest
from keycloak.authorization import Permission, Policy, Role
from keycloak.exceptions import KeycloakAuthorizationConfigError
def test_authorization_objects():
"""Test authorization objects."""
# Test permission
p = Permission(name="test", type="test", logic="test", decision_strategy="test")
assert p.name == "test"
assert p.type == "test"
assert p.logic == "test"
assert p.decision_strategy == "test"
p.resources = ["test"]
assert p.resources == ["test"]
p.scopes = ["test"]
assert p.scopes == ["test"]
# Test policy
p = Policy(name="test", type="test", logic="test", decision_strategy="test")
assert p.name == "test"
assert p.type == "test"
assert p.logic == "test"
assert p.decision_strategy == "test"
p.roles = ["test"]
assert p.roles == ["test"]
p.permissions = ["test"]
assert p.permissions == ["test"]
p.add_permission(permission="test2")
assert p.permissions == ["test", "test2"]
with pytest.raises(KeycloakAuthorizationConfigError):
p.add_role(role="test2")
# Test role
r = Role(name="test")
assert r.name == "test"
assert not r.required
assert r.get_name() == "test"
assert r == r
assert r == "test"

41
tests/test_connection.py

@ -0,0 +1,41 @@
"""Connection test module."""
import pytest
from keycloak.connection import ConnectionManager
from keycloak.exceptions import KeycloakConnectionError
def test_connection_proxy():
"""Test proxies of connection manager."""
cm = ConnectionManager(
base_url="http://test.test", proxies={"http://test.test": "localhost:8080"}
)
assert cm._s.proxies == {"http://test.test": "localhost:8080"}
def test_headers():
"""Test headers manipulation."""
cm = ConnectionManager(base_url="http://test.test", headers={"H": "A"})
assert cm.param_headers(key="H") == "A"
assert cm.param_headers(key="A") is None
cm.clean_headers()
assert cm.headers == dict()
cm.add_param_headers(key="H", value="B")
assert cm.exist_param_headers(key="H")
assert not cm.exist_param_headers(key="B")
cm.del_param_headers(key="H")
assert not cm.exist_param_headers(key="H")
def test_bad_connection():
"""Test bad connection."""
cm = ConnectionManager(base_url="http://not.real.domain")
with pytest.raises(KeycloakConnectionError):
cm.raw_get(path="bad")
with pytest.raises(KeycloakConnectionError):
cm.raw_delete(path="bad")
with pytest.raises(KeycloakConnectionError):
cm.raw_post(path="bad", data={})
with pytest.raises(KeycloakConnectionError):
cm.raw_put(path="bad", data={})

20
tests/test_exceptions.py

@ -0,0 +1,20 @@
"""Test the exceptions module."""
from unittest.mock import Mock
import pytest
from keycloak.exceptions import KeycloakOperationError, raise_error_from_response
def test_raise_error_from_response_from_dict():
"""Test raise error from response using a dictionary."""
response = Mock()
response.json.return_value = {"key": "value"}
response.status_code = 408
response.content = "Error"
with pytest.raises(KeycloakOperationError):
raise_error_from_response(
response=response, error=dict(), expected_codes=[200], skip_exists=False
)

4
tests/test_uma_permissions.py

@ -23,11 +23,11 @@ import pytest
from keycloak.exceptions import KeycloakPermissionFormatError, PermissionDefinitionError
from keycloak.uma_permissions import (
AuthStatus,
Resource,
Scope,
build_permission_param,
UMAPermission,
AuthStatus,
build_permission_param,
)

Loading…
Cancel
Save