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.

143 lines
4.4 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 pytest
  18. import re
  19. from keycloak.exceptions import KeycloakPermissionFormatError, PermissionDefinitionError
  20. from keycloak.uma_permissions import build_permission_param, Resource, Scope
  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_dict():
  38. r = Resource("Resource1")
  39. s = {"scope": "Scope1"}
  40. assert r(**s) == "Resource1#Scope1"
  41. def test_scope_resource_dict():
  42. r = {"resource": "Resource1"}
  43. s = Scope("Scope1")
  44. assert s(**r) == "Resource1#Scope1"
  45. def test_resource_scope_list():
  46. r = Resource("Resource1")
  47. s = ["Scope1"]
  48. with pytest.raises(PermissionDefinitionError) as err:
  49. r(*s)
  50. assert err.match("can't determine if 'Scope1' is a resource or scope")
  51. def test_build_permission_none():
  52. assert build_permission_param(None) == set()
  53. def test_build_permission_empty_str():
  54. assert build_permission_param("") == set()
  55. def test_build_permission_empty_list():
  56. assert build_permission_param([]) == set()
  57. def test_build_permission_empty_tuple():
  58. assert build_permission_param(()) == set()
  59. def test_build_permission_empty_set():
  60. assert build_permission_param(set()) == set()
  61. def test_build_permission_empty_dict():
  62. assert build_permission_param({}) == set()
  63. def test_build_permission_str():
  64. assert build_permission_param("resource1") == {"resource1"}
  65. def test_build_permission_list_str():
  66. assert build_permission_param(["res1#scope1", "res1#scope2"]) == {"res1#scope1", "res1#scope2"}
  67. def test_build_permission_tuple_str():
  68. assert build_permission_param(("res1#scope1", "res1#scope2")) == {"res1#scope1", "res1#scope2"}
  69. def test_build_permission_set_str():
  70. assert build_permission_param({"res1#scope1", "res1#scope2"}) == {"res1#scope1", "res1#scope2"}
  71. def test_build_permission_tuple_dict_str_str():
  72. assert build_permission_param({"res1": "scope1"}) == {"res1#scope1"}
  73. def test_build_permission_tuple_dict_str_list_str():
  74. assert build_permission_param({"res1": ["scope1", "scope2"]}) == {"res1#scope1", "res1#scope2"}
  75. def test_build_permission_tuple_dict_str_list_str2():
  76. assert build_permission_param(
  77. {"res1": ["scope1", "scope2"], "res2": ["scope2", "scope3"]}
  78. ) == {"res1#scope1", "res1#scope2", "res2#scope2", "res2#scope3"}
  79. def test_build_permission_misbuilt_dict_str_list_list_str():
  80. with pytest.raises(KeycloakPermissionFormatError) as err:
  81. build_permission_param({"res1": [["scope1", "scope2"]]})
  82. assert err.match(re.escape("misbuilt permission {'res1': [['scope1', 'scope2']]}"))
  83. def test_build_permission_misbuilt_list_list_str():
  84. with pytest.raises(KeycloakPermissionFormatError) as err:
  85. build_permission_param([["scope1", "scope2"]])
  86. assert err.match(re.escape("misbuilt permission [['scope1', 'scope2']]"))
  87. def test_build_permission_misbuilt_list_set_str():
  88. with pytest.raises(KeycloakPermissionFormatError) as err:
  89. build_permission_param([{"scope1", "scope2"}])
  90. assert err.match(re.escape("misbuilt permission [{'scope1', 'scope2'}]"))
  91. def test_build_permission_misbuilt_set_set_str():
  92. with pytest.raises(KeycloakPermissionFormatError) as err:
  93. build_permission_param([{"scope1"}])
  94. assert err.match(re.escape("misbuilt permission [{'scope1'}]"))