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.

148 lines
4.7 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2017 Marcos Pereira <marcospereira.mpj@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import re
  18. import pytest
  19. from keycloak.exceptions import KeycloakPermissionFormatError, PermissionDefinitionError
  20. from keycloak.uma_permissions import Resource, Scope, build_permission_param
  21. def test_resource_with_scope_obj():
  22. r = Resource("Resource1")
  23. s = Scope("Scope1")
  24. assert r(s) == "Resource1#Scope1"
  25. def test_scope_with_resource_obj():
  26. r = Resource("Resource1")
  27. s = Scope("Scope1")
  28. assert s(r) == "Resource1#Scope1"
  29. def test_resource_scope_str():
  30. r = Resource("Resource1")
  31. s = "Scope1"
  32. assert r(scope=s) == "Resource1#Scope1"
  33. def test_scope_resource_str():
  34. r = "Resource1"
  35. s = Scope("Scope1")
  36. assert s(resource=r) == "Resource1#Scope1"
  37. def test_resource_scope_list():
  38. r = Resource("Resource1")
  39. s = ["Scope1"]
  40. with pytest.raises(PermissionDefinitionError) as err:
  41. r(s)
  42. assert err.match(re.escape("can't determine if '['Scope1']' is a resource or scope"))
  43. def test_build_permission_none():
  44. assert build_permission_param(None) == set()
  45. def test_build_permission_empty_str():
  46. assert build_permission_param("") == set()
  47. def test_build_permission_empty_list():
  48. assert build_permission_param([]) == set()
  49. def test_build_permission_empty_tuple():
  50. assert build_permission_param(()) == set()
  51. def test_build_permission_empty_set():
  52. assert build_permission_param(set()) == set()
  53. def test_build_permission_empty_dict():
  54. assert build_permission_param({}) == set()
  55. def test_build_permission_str():
  56. assert build_permission_param("resource1") == {"resource1"}
  57. def test_build_permission_list_str():
  58. assert build_permission_param(["res1#scope1", "res1#scope2"]) == {"res1#scope1", "res1#scope2"}
  59. def test_build_permission_tuple_str():
  60. assert build_permission_param(("res1#scope1", "res1#scope2")) == {"res1#scope1", "res1#scope2"}
  61. def test_build_permission_set_str():
  62. assert build_permission_param({"res1#scope1", "res1#scope2"}) == {"res1#scope1", "res1#scope2"}
  63. def test_build_permission_tuple_dict_str_str():
  64. assert build_permission_param({"res1": "scope1"}) == {"res1#scope1"}
  65. def test_build_permission_tuple_dict_str_list_str():
  66. assert build_permission_param({"res1": ["scope1", "scope2"]}) == {"res1#scope1", "res1#scope2"}
  67. def test_build_permission_tuple_dict_str_list_str2():
  68. assert build_permission_param(
  69. {"res1": ["scope1", "scope2"], "res2": ["scope2", "scope3"]}
  70. ) == {"res1#scope1", "res1#scope2", "res2#scope2", "res2#scope3"}
  71. def test_build_permission_uma():
  72. assert build_permission_param(Resource("res1")(Scope("scope1"))) == {"res1#scope1"}
  73. def test_build_permission_uma_list():
  74. assert build_permission_param(
  75. [Resource("res1")(Scope("scope1")), Resource("res1")(Scope("scope2"))]
  76. ) == {"res1#scope1", "res1#scope2"}
  77. def test_build_permission_misbuilt_dict_str_list_list_str():
  78. with pytest.raises(KeycloakPermissionFormatError) as err:
  79. build_permission_param({"res1": [["scope1", "scope2"]]})
  80. assert err.match(re.escape("misbuilt permission {'res1': [['scope1', 'scope2']]}"))
  81. def test_build_permission_misbuilt_list_list_str():
  82. with pytest.raises(KeycloakPermissionFormatError) as err:
  83. print(build_permission_param([["scope1", "scope2"]]))
  84. assert err.match(re.escape("misbuilt permission [['scope1', 'scope2']]"))
  85. def test_build_permission_misbuilt_list_set_str():
  86. with pytest.raises(KeycloakPermissionFormatError) as err:
  87. build_permission_param([{"scope1", "scope2"}])
  88. assert err.match("misbuilt permission.*")
  89. def test_build_permission_misbuilt_set_set_str():
  90. with pytest.raises(KeycloakPermissionFormatError) as err:
  91. build_permission_param([{"scope1"}])
  92. assert err.match(re.escape("misbuilt permission [{'scope1'}]"))
  93. def test_build_permission_misbuilt_dict_non_iterable():
  94. with pytest.raises(KeycloakPermissionFormatError) as err:
  95. build_permission_param({"res1": 5})
  96. assert err.match(re.escape("misbuilt permission {'res1': 5}"))