You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.0 KiB

  1. """Test module for KeycloakUMA."""
  2. import re
  3. from typing import Tuple
  4. import pytest
  5. from keycloak import KeycloakOpenID
  6. from keycloak.connection import ConnectionManager
  7. from keycloak.exceptions import (
  8. KeycloakDeleteError,
  9. KeycloakGetError,
  10. KeycloakPostError,
  11. KeycloakPutError,
  12. )
  13. from keycloak.keycloak_uma import KeycloakUMA
  14. def test_keycloak_uma_init(env):
  15. """Test KeycloakUMA's init method.
  16. :param env: Environment fixture
  17. :type env: KeycloakTestEnv
  18. """
  19. uma = KeycloakUMA(
  20. server_url=f"http://{env.KEYCLOAK_HOST}:{env.KEYCLOAK_PORT}", realm_name="master"
  21. )
  22. assert uma.realm_name == "master"
  23. assert isinstance(uma.connection, ConnectionManager)
  24. # should initially be empty
  25. assert uma._well_known is None
  26. assert uma.uma_well_known
  27. # should be cached after first reference
  28. assert uma._well_known is not None
  29. def test_uma_well_known(uma: KeycloakUMA):
  30. """Test the well_known method.
  31. :param uma: Keycloak UMA client
  32. :type uma: KeycloakUMA
  33. """
  34. res = uma.uma_well_known
  35. assert res is not None
  36. assert res != dict()
  37. for key in ["resource_registration_endpoint"]:
  38. assert key in res
  39. def test_uma_resource_sets(
  40. uma: KeycloakUMA, oid_with_credentials_authz: Tuple[KeycloakOpenID, str, str]
  41. ):
  42. """Test resource sets.
  43. :param uma: Keycloak UMA client
  44. :type uma: KeycloakUMA
  45. :param oid_with_credentials_authz: Keycloak OpenID client with pre-configured user credentials
  46. :type oid_with_credentials_authz: Tuple[KeycloakOpenID, str, str]
  47. """
  48. oid, _, _ = oid_with_credentials_authz
  49. token = oid.token(grant_type="client_credentials")
  50. access_token = token["access_token"]
  51. # Check that only the default resource is present
  52. resource_sets = uma.resource_set_list(access_token)
  53. resource_set_list = list(resource_sets)
  54. assert len(resource_set_list) == 1, resource_set_list
  55. assert resource_set_list[0]["name"] == "Default Resource", resource_set_list[0]["name"]
  56. # Test create resource set
  57. resource_to_create = {
  58. "name": "mytest",
  59. "scopes": ["test:read", "test:write"],
  60. "type": "urn:test",
  61. }
  62. created_resource = uma.resource_set_create(access_token, resource_to_create)
  63. assert created_resource
  64. assert created_resource["_id"], created_resource
  65. assert set(resource_to_create).issubset(set(created_resource)), created_resource
  66. # Test create the same resource set
  67. with pytest.raises(KeycloakPostError) as err:
  68. uma.resource_set_create(access_token, resource_to_create)
  69. assert err.match(
  70. re.escape(
  71. '409: b\'{"error":"invalid_request","error_description":'
  72. '"Resource with name [mytest] already exists."}\''
  73. )
  74. )
  75. # Test get resource set
  76. latest_resource = uma.resource_set_read(access_token, created_resource["_id"])
  77. assert latest_resource["name"] == created_resource["name"]
  78. # Test update resource set
  79. latest_resource["name"] = "New Resource Name"
  80. res = uma.resource_set_update(access_token, created_resource["_id"], latest_resource)
  81. assert res == dict(), res
  82. updated_resource = uma.resource_set_read(access_token, created_resource["_id"])
  83. assert updated_resource["name"] == "New Resource Name"
  84. # Test update resource set fail
  85. with pytest.raises(KeycloakPutError) as err:
  86. uma.resource_set_update(
  87. token=access_token, resource_id=created_resource["_id"], payload={"wrong": "payload"}
  88. )
  89. assert err.match('400: b\'{"error":"Unrecognized field')
  90. # Test delete resource set
  91. res = uma.resource_set_delete(token=access_token, resource_id=created_resource["_id"])
  92. assert res == dict(), res
  93. with pytest.raises(KeycloakGetError) as err:
  94. uma.resource_set_read(access_token, created_resource["_id"])
  95. err.match("404: b''")
  96. # Test delete fail
  97. with pytest.raises(KeycloakDeleteError) as err:
  98. uma.resource_set_delete(token=access_token, resource_id=created_resource["_id"])
  99. assert err.match("404: b''")