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.

104 lines
3.4 KiB

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