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.

37 lines
1.2 KiB

3 months ago
  1. """Test URL patterns."""
  2. import inspect
  3. from keycloak import urls_patterns
  4. def test_correctness_of_patterns():
  5. """Test that there are no duplicate url patterns."""
  6. # Test that the patterns are present
  7. urls = [x for x in dir(urls_patterns) if not x.startswith("__")]
  8. assert len(urls) >= 0
  9. # Test that all patterns start with URL_
  10. for url in urls:
  11. assert url.startswith("URL_"), f"The url pattern {url} does not begin with URL_"
  12. # Test that the patterns have unique names
  13. seen_urls = list()
  14. urls_from_src = [
  15. x.split("=")[0].strip()
  16. for x in inspect.getsource(urls_patterns).splitlines()
  17. if x.startswith("URL_")
  18. ]
  19. for url in urls_from_src:
  20. assert url not in seen_urls, f"The url pattern {url} is present twice."
  21. seen_urls.append(url)
  22. # Test that the pattern values are unique
  23. seen_url_values = list()
  24. for url in urls:
  25. url_value = urls_patterns.__dict__[url]
  26. assert url_value not in seen_url_values, f"The url {url} has a duplicate value {url_value}"
  27. assert (
  28. url_value == url_value.strip()
  29. ), f"The url {url} with value '{url_value}' has whitespace values"
  30. seen_url_values.append(url_value)