From 7e50d4313c4d2ead8a2843346a24e8a8a5561703 Mon Sep 17 00:00:00 2001 From: Luca Paganin Date: Mon, 18 Jul 2022 14:49:02 +0200 Subject: [PATCH] fix: applied tox linting check --- tests/conftest.py | 21 +++++++++------------ tests/test_keycloak_admin.py | 29 +++++++++++++++++------------ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 7380999..44865e2 100644 --- a/tests/conftest.py +++ b/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()) diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py index 20341af..76d0cf4 100644 --- a/tests/test_keycloak_admin.py +++ b/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])