test_utils.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from crau.utils import resource_matches_base_url
  2. def test_resource_matches_base_url_empty_allowed_list():
  3. urls = [
  4. "https://url.com",
  5. "https://example.com",
  6. "https://net.br/path",
  7. ]
  8. allowed = []
  9. for url in urls:
  10. assert resource_matches_base_url(url, allowed) is True
  11. def test_resource_matches_base_url_allowed_is_domain():
  12. urls = [
  13. ("https://url.com", False),
  14. ("https://example.com", True),
  15. ("https://example.com/anything", True),
  16. ("https://facebook.com/example.com", False),
  17. ("https://net.br/path", False),
  18. ]
  19. allowed = ["example.com"]
  20. for url, match_ in urls:
  21. assert resource_matches_base_url(url, allowed) is match_, url
  22. def test_resource_matches_base_url_allowed_is_domain_with_scheme():
  23. urls = [
  24. ("https://url.com", False),
  25. ("https://example.com", True),
  26. ("http://example.com/anything", True),
  27. ("https://facebook.com/example.com", False),
  28. ("https://net.br/path", False),
  29. ]
  30. allowed = ["http://example.com"]
  31. for url, match_ in urls:
  32. assert resource_matches_base_url(url, allowed) is match_, url
  33. allowed = ["https://example.com"]
  34. for url, match_ in urls:
  35. assert resource_matches_base_url(url, allowed) is match_, url
  36. def test_resource_matches_base_url_allowed_domain_with_path():
  37. urls = [
  38. ("https://url.com", False),
  39. ("https://example.com", False),
  40. ("https://example.com/path", True),
  41. ("https://example.com/path/root", True),
  42. ("https://example.com/anything", False),
  43. ("https://facebook.com/example.com", False),
  44. ("https://net.br/path", False),
  45. ]
  46. allowed = ["example.com/path"]
  47. for url, match_ in urls:
  48. assert resource_matches_base_url(url, allowed) is match_, url
  49. def test_resource_matches_base_url_miscelaneous_allowed():
  50. urls = [
  51. ("https://url.com", False),
  52. ("https://example.com", False),
  53. ("https://example.com/path", True),
  54. ("https://example.com/path/root", True),
  55. ("https://example.com/anything", False),
  56. ("https://facebook.com/example.com", False),
  57. ("https://www.twitter.com/profile/username", True),
  58. ("https://www.twitter.com/example.com", False),
  59. ("https://net.br/path", False),
  60. ]
  61. allowed = ["example.com/path", "twitter.com/profile"]
  62. for url, match_ in urls:
  63. assert resource_matches_base_url(url, allowed) is match_, url