Browse Source

fix: applied tox linting check

pull/358/head
Luca Paganin 2 years ago
parent
commit
7e50d4313c
  1. 21
      tests/conftest.py
  2. 29
      tests/test_keycloak_admin.py

21
tests/conftest.py

@ -284,28 +284,25 @@ def composite_client_role(admin: KeycloakAdmin, realm: str, client: str, client_
@pytest.fixture
def selfsigned_cert():
"""Generates self signed certificate for a hostname, and optional IP addresses."""
import ipaddress
from datetime import datetime, timedelta
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from datetime import datetime, timedelta
import ipaddress
from cryptography.x509.oid import NameOID
hostname = "testcert"
ip_addresses = None
key = None
# Generate our key
if key is None:
key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend(),
public_exponent=65537, key_size=2048, backend=default_backend()
)
name = x509.Name([
x509.NameAttribute(NameOID.COMMON_NAME, hostname)
])
name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)])
# best practice seem to be to include the hostname in the SAN, which *SHOULD* mean COMMON_NAME is ignored.
alt_names = [x509.DNSName(hostname)]
@ -331,7 +328,7 @@ def selfsigned_cert():
.public_key(key.public_key())
.serial_number(1000)
.not_valid_before(now)
.not_valid_after(now + timedelta(days=10*365))
.not_valid_after(now + timedelta(days=10 * 365))
.add_extension(basic_contraints, False)
.add_extension(san, False)
.sign(key, hashes.SHA256(), default_backend())

29
tests/test_keycloak_admin.py

@ -1,8 +1,9 @@
"""Test the keycloak admin object."""
import copy
import pytest
import copy
import keycloak
from keycloak import KeycloakAdmin
from keycloak.connection import ConnectionManager
@ -1840,34 +1841,38 @@ def test_get_required_action_by_alias(admin: KeycloakAdmin, realm: str):
ractions = admin.get_required_actions()
ra = admin.get_required_action_by_alias("UPDATE_PASSWORD")
assert ra in ractions
assert ra['alias'] == "UPDATE_PASSWORD"
assert ra["alias"] == "UPDATE_PASSWORD"
def test_update_required_action(admin: KeycloakAdmin, realm: str):
admin.realm_name = realm
ra = admin.get_required_action_by_alias("UPDATE_PASSWORD")
old = copy.deepcopy(ra)
ra['enabled'] = False
ra["enabled"] = False
admin.update_required_action("UPDATE_PASSWORD", ra)
newra = admin.get_required_action_by_alias("UPDATE_PASSWORD")
assert old != newra
assert newra['enabled'] is False
assert newra["enabled"] is False
def test_get_composite_client_roles_of_group(admin: KeycloakAdmin, realm: str, client: str, group: str, composite_client_role: str):
def test_get_composite_client_roles_of_group(
admin: KeycloakAdmin, realm: str, client: str, group: str, composite_client_role: str
):
admin.realm_name = realm
role = admin.get_client_role(client, composite_client_role)
admin.assign_group_client_roles(group_id=group, client_id=client, roles=[role])
result = admin.get_composite_client_roles_of_group(client, group)
assert role['id'] in [x['id'] for x in result]
assert role["id"] in [x["id"] for x in result]
def test_get_role_client_level_children(admin: KeycloakAdmin, realm: str, client: str, composite_client_role: str, client_role: str):
def test_get_role_client_level_children(
admin: KeycloakAdmin, realm: str, client: str, composite_client_role: str, client_role: str
):
admin.realm_name = realm
child = admin.get_client_role(client, client_role)
parent = admin.get_client_role(client, composite_client_role)
res = admin.get_role_client_level_children(client, parent['id'])
assert child['id'] in [x['id'] for x in res]
res = admin.get_role_client_level_children(client, parent["id"])
assert child["id"] in [x["id"] for x in res]
def test_get_user_credentials(admin: KeycloakAdmin, realm: str, user: str):
@ -1876,13 +1881,13 @@ def test_get_user_credentials(admin: KeycloakAdmin, realm: str, user: str):
res = admin.get_user_credentials(user)
assert isinstance(res, list)
assert len(res) == 1
assert res[0]['type'] == "password"
assert res[0]["type"] == "password"
def test_upload_certificate(admin: KeycloakAdmin, realm: str, client: str, selfsigned_cert: tuple):
admin.realm_name = realm
cert, _ = selfsigned_cert
cert = cert.decode('utf-8').strip()
cert = cert.decode("utf-8").strip()
admin.upload_certificate(client, cert)
cl = admin.get_client(client)
assert cl['attributes']['jwt.credential.certificate'] == "".join(cert.splitlines()[1:-1])
assert cl["attributes"]["jwt.credential.certificate"] == "".join(cert.splitlines()[1:-1])
Loading…
Cancel
Save