Skip to content

Commit 8eca957

Browse files
authored
fix: Fix/python version and ci (#691)
* fix: python version to 3.10 or above, wrong return type * test: updated tests for the latet keycloak version
1 parent 16487b7 commit 8eca957

6 files changed

Lines changed: 98 additions & 548 deletions

File tree

poetry.lock

Lines changed: 47 additions & 518 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Documentation = "https://python-keycloak.readthedocs.io/en/latest/"
3131
"Issue tracker" = "https://github.com/marcospereirampj/python-keycloak/issues"
3232

3333
[tool.poetry.dependencies]
34-
python = ">=3.9,<4"
34+
python = ">=3.10,<4"
3535
requests = ">=2.20.0"
3636
requests-toolbelt = ">=0.6.0"
3737
deprecation = ">=2.1.0"

src/keycloak/keycloak_admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5429,7 +5429,7 @@ def create_authentication_flow(self, payload: dict, skip_exists: bool = False) -
54295429
skip_exists=skip_exists,
54305430
)
54315431
if isinstance(res, dict) and res == {"msg": "Already exists"}:
5432-
return res
5432+
return json.dumps(res).encode()
54335433

54345434
if not isinstance(res, bytes):
54355435
msg = (

tests/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,27 @@ def oid_with_credentials_authz(
325325
"serviceAccountsEnabled": True,
326326
},
327327
)
328+
admin.create_client_authz_resource(
329+
client_id=client_id,
330+
payload={"name": "Default Resource", "uris": ["/*"], "type": "urn.resource"},
331+
skip_exists=True,
332+
)
328333
admin.create_client_authz_role_based_policy(
329334
client_id=client_id,
330335
payload={
331336
"name": "test-authz-rb-policy",
332337
"roles": [{"id": admin.get_realm_role(role_name="offline_access")["id"]}],
333338
},
334339
)
340+
admin.create_client_authz_resource_based_permission(
341+
client_id=client_id,
342+
payload={
343+
"name": "default-resource-permission",
344+
"resources": ["Default Resource"],
345+
"policies": ["test-authz-rb-policy"],
346+
"decisionStrategy": "UNANIMOUS",
347+
},
348+
)
335349
# Create user
336350
username = str(uuid.uuid4())
337351
password = str(uuid.uuid4())

tests/test_keycloak_admin.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,8 +1227,9 @@ def test_clients(admin: KeycloakAdmin, realm: str) -> None:
12271227

12281228
# Authz resources
12291229
res = admin.get_client_authz_resources(client_id=auth_client_id)
1230-
assert len(res) == 1
1231-
assert res[0]["name"] == "Default Resource"
1230+
assert len(res) in [0, 1]
1231+
if len(res) == 1:
1232+
assert res[0]["name"] == "Default Resource"
12321233

12331234
with pytest.raises(KeycloakGetError) as err:
12341235
admin.get_client_authz_resources(client_id=client_id)
@@ -1258,8 +1259,8 @@ def test_clients(admin: KeycloakAdmin, realm: str) -> None:
12581259
) == {"msg": "Already exists"}
12591260

12601261
res = admin.get_client_authz_resources(client_id=auth_client_id)
1261-
assert len(res) == 2
1262-
assert {x["name"] for x in res} == {"Default Resource", "test-resource"}
1262+
assert len(res) in [1, 2]
1263+
assert {x["name"] for x in res}.issubset({"Default Resource", "test-resource"})
12631264

12641265
res = admin.create_client_authz_resource(
12651266
client_id=auth_client_id,
@@ -1293,8 +1294,9 @@ def test_clients(admin: KeycloakAdmin, realm: str) -> None:
12931294

12941295
# Authz policies
12951296
res = admin.get_client_authz_policies(client_id=auth_client_id)
1296-
assert len(res) == 1, res
1297-
assert res[0]["name"] == "Default Policy"
1297+
assert len(res) in [0, 1], res
1298+
if len(res) == 1:
1299+
assert res[0]["name"] == "Default Policy"
12981300

12991301
with pytest.raises(KeycloakGetError) as err:
13001302
admin.get_client_authz_policies(client_id="does-not-exist")
@@ -1320,7 +1322,7 @@ def test_clients(admin: KeycloakAdmin, realm: str) -> None:
13201322
payload={"name": "test-authz-rb-policy", "roles": [{"id": role_id}]},
13211323
skip_exists=True,
13221324
) == {"msg": "Already exists"}
1323-
assert len(admin.get_client_authz_policies(client_id=auth_client_id)) == 2
1325+
assert len(admin.get_client_authz_policies(client_id=auth_client_id)) in [1, 2]
13241326

13251327
res = admin.create_client_authz_role_based_policy(
13261328
client_id=auth_client_id,
@@ -1363,12 +1365,13 @@ def test_clients(admin: KeycloakAdmin, realm: str) -> None:
13631365
},
13641366
skip_exists=True,
13651367
) == {"msg": "Already exists"}
1366-
assert len(admin.get_client_authz_policies(client_id=auth_client_id)) == 3
1368+
assert len(admin.get_client_authz_policies(client_id=auth_client_id)) in [2, 3]
13671369

13681370
# Test authz permissions
13691371
res = admin.get_client_authz_permissions(client_id=auth_client_id)
1370-
assert len(res) == 1, res
1371-
assert res[0]["name"] == "Default Permission"
1372+
assert len(res) in [0, 1], res
1373+
if len(res) == 1:
1374+
assert res[0]["name"] == "Default Permission"
13721375

13731376
with pytest.raises(KeycloakGetError) as err:
13741377
admin.get_client_authz_permissions(client_id="does-not-exist")
@@ -1395,7 +1398,7 @@ def test_clients(admin: KeycloakAdmin, realm: str) -> None:
13951398
payload={"name": "test-permission-rb", "resources": [test_resource_id]},
13961399
skip_exists=True,
13971400
) == {"msg": "Already exists"}
1398-
assert len(admin.get_client_authz_permissions(client_id=auth_client_id)) == 2
1401+
assert len(admin.get_client_authz_permissions(client_id=auth_client_id)) in [1, 2]
13991402

14001403
# Test associating client policy with resource based permission
14011404
res = admin.update_client_authz_resource_permission(
@@ -2681,10 +2684,10 @@ def test_auth_flows(admin: KeycloakAdmin, realm: str) -> None:
26812684
with pytest.raises(KeycloakPostError) as err:
26822685
admin.create_authentication_flow(payload={"alias": "test-create", "builtIn": False})
26832686
assert err.match('409: b\'{"errorMessage":"Flow test-create already exists"}\'')
2684-
assert admin.create_authentication_flow(
2685-
payload={"alias": "test-create"},
2686-
skip_exists=True,
2687-
) == {"msg": "Already exists"}
2687+
assert (
2688+
admin.create_authentication_flow(payload={"alias": "test-create"}, skip_exists=True)
2689+
== json.dumps({"msg": "Already exists"}).encode()
2690+
)
26882691

26892692
# Update
26902693
res = admin.get_authentication_flows()
@@ -3080,7 +3083,7 @@ def test_components(admin: KeycloakAdmin, realm: str) -> None:
30803083

30813084
# Test get components
30823085
res = admin.get_components()
3083-
assert len(res) == 12
3086+
assert len(res) in [12, 14]
30843087

30853088
with pytest.raises(KeycloakGetError) as err:
30863089
admin.get_component(component_id="does-not-exist")
@@ -4920,8 +4923,9 @@ async def test_a_clients(admin: KeycloakAdmin, realm: str) -> None:
49204923

49214924
# Authz resources
49224925
res = await admin.a_get_client_authz_resources(client_id=auth_client_id)
4923-
assert len(res) == 1
4924-
assert res[0]["name"] == "Default Resource"
4926+
assert len(res) in [0, 1]
4927+
if len(res) == 1:
4928+
assert res[0]["name"] == "Default Resource"
49254929

49264930
with pytest.raises(KeycloakGetError) as err:
49274931
await admin.a_get_client_authz_resources(client_id=client_id)
@@ -4954,8 +4958,8 @@ async def test_a_clients(admin: KeycloakAdmin, realm: str) -> None:
49544958
) == {"msg": "Already exists"}
49554959

49564960
res = await admin.a_get_client_authz_resources(client_id=auth_client_id)
4957-
assert len(res) == 2
4958-
assert {x["name"] for x in res} == {"Default Resource", "test-resource"}
4961+
assert len(res) in [1, 2]
4962+
assert {x["name"] for x in res}.issubset({"Default Resource", "test-resource"})
49594963

49604964
res = await admin.a_create_client_authz_resource(
49614965
client_id=auth_client_id,
@@ -4996,8 +5000,9 @@ async def test_a_clients(admin: KeycloakAdmin, realm: str) -> None:
49965000

49975001
# Authz policies
49985002
res = await admin.a_get_client_authz_policies(client_id=auth_client_id)
4999-
assert len(res) == 1, res
5000-
assert res[0]["name"] == "Default Policy"
5003+
assert len(res) in [0, 1], res
5004+
if len(res) == 1:
5005+
assert res[0]["name"] == "Default Policy"
50015006

50025007
with pytest.raises(KeycloakGetError) as err:
50035008
await admin.a_get_client_authz_policies(client_id="does-not-exist")
@@ -5021,7 +5026,7 @@ async def test_a_clients(admin: KeycloakAdmin, realm: str) -> None:
50215026
payload={"name": "test-authz-rb-policy", "roles": [{"id": role_id}]},
50225027
skip_exists=True,
50235028
) == {"msg": "Already exists"}
5024-
assert len(await admin.a_get_client_authz_policies(client_id=auth_client_id)) == 2
5029+
assert len(await admin.a_get_client_authz_policies(client_id=auth_client_id)) in [1, 2]
50255030
role_based_policy_id = res["id"]
50265031
role_based_policy_name = res["name"]
50275032

@@ -5066,12 +5071,13 @@ async def test_a_clients(admin: KeycloakAdmin, realm: str) -> None:
50665071
},
50675072
skip_exists=True,
50685073
) == {"msg": "Already exists"}
5069-
assert len(await admin.a_get_client_authz_policies(client_id=auth_client_id)) == 3
5074+
assert len(await admin.a_get_client_authz_policies(client_id=auth_client_id)) in [2, 3]
50705075

50715076
# Test authz permissions
50725077
res = await admin.a_get_client_authz_permissions(client_id=auth_client_id)
5073-
assert len(res) == 1, res
5074-
assert res[0]["name"] == "Default Permission"
5078+
assert len(res) in [0, 1], res
5079+
if len(res) == 1:
5080+
assert res[0]["name"] == "Default Permission"
50755081

50765082
with pytest.raises(KeycloakGetError) as err:
50775083
await admin.a_get_client_authz_permissions(client_id="does-not-exist")
@@ -5098,7 +5104,7 @@ async def test_a_clients(admin: KeycloakAdmin, realm: str) -> None:
50985104
payload={"name": "test-permission-rb", "resources": [test_resource_id]},
50995105
skip_exists=True,
51005106
) == {"msg": "Already exists"}
5101-
assert len(await admin.a_get_client_authz_permissions(client_id=auth_client_id)) == 2
5107+
assert len(await admin.a_get_client_authz_permissions(client_id=auth_client_id)) in [1, 2]
51025108

51035109
# Test associating client policy with resource based permission
51045110
res = await admin.a_update_client_authz_resource_permission(
@@ -6933,7 +6939,7 @@ async def test_a_components(admin: KeycloakAdmin, realm: str) -> None:
69336939

69346940
# Test get components
69356941
res = await admin.a_get_components()
6936-
assert len(res) == 12
6942+
assert len(res) in [12, 14]
69376943

69386944
with pytest.raises(KeycloakGetError) as err:
69396945
await admin.a_get_component(component_id="does-not-exist")

tests/test_keycloak_openid.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ def test_entitlement(
298298
token = oid.token(username=username, password=password)
299299
client_id = admin.get_client_id(oid.client_id)
300300
assert client_id is not None
301+
assert admin.connection.realm_name == oid.realm_name
301302
resource_server_id = admin.get_client_authz_resources(client_id=client_id)[0]["_id"]
302303

303304
with pytest.raises(KeycloakDeprecationError):

0 commit comments

Comments
 (0)