test_link_handling.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from ..parser import parse
  2. def test_parser_converts_unmarked_links_to_hrefs(request_mock, user, snapshot):
  3. text = "Lorem ipsum http://test.com"
  4. result = parse(text, request_mock, user, minify=False)
  5. snapshot.assert_match(result["parsed_text"])
  6. def test_parser_skips_links_in_inline_code_markdown(request_mock, user, snapshot):
  7. text = "Lorem ipsum `http://test.com`"
  8. result = parse(text, request_mock, user, minify=False)
  9. snapshot.assert_match(result["parsed_text"])
  10. def test_parser_skips_links_in_inline_code_bbcode(request_mock, user, snapshot):
  11. text = "Lorem ipsum [code]http://test.com[/code]"
  12. result = parse(text, request_mock, user, minify=False)
  13. snapshot.assert_match(result["parsed_text"])
  14. def test_parser_skips_links_in_code_bbcode(request_mock, user, snapshot):
  15. text = """
  16. [code]
  17. http://test.com
  18. [/code]
  19. """
  20. result = parse(text, request_mock, user, minify=False)
  21. snapshot.assert_match(result["parsed_text"])
  22. def test_absolute_link_to_site_is_changed_to_relative_link(
  23. request_mock, user, snapshot
  24. ):
  25. text = "clean_links step cleans http://example.com"
  26. result = parse(text, request_mock, user, minify=False)
  27. snapshot.assert_match(result["parsed_text"])
  28. def test_absolute_link_to_site_is_added_to_internal_links_list(request_mock, user):
  29. text = "clean_links step cleans http://example.com"
  30. result = parse(text, request_mock, user, minify=False)
  31. assert result["internal_links"] == ["/"]
  32. def test_absolute_link_to_site_without_schema_is_changed_to_relative_link(
  33. request_mock, user, snapshot
  34. ):
  35. text = "clean_links step cleans example.com"
  36. result = parse(text, request_mock, user, minify=False)
  37. snapshot.assert_match(result["parsed_text"])
  38. def test_absolute_link_to_site_without_schema_is_added_to_internal_links_list(
  39. request_mock, user
  40. ):
  41. text = "clean_links step cleans example.com"
  42. result = parse(text, request_mock, user, minify=False)
  43. assert result["internal_links"] == ["/"]
  44. def test_absolute_link_with_path_to_site_is_changed_to_relative_link(
  45. request_mock, user, snapshot
  46. ):
  47. text = "clean_links step cleans http://example.com/somewhere-something/"
  48. result = parse(text, request_mock, user, minify=False)
  49. snapshot.assert_match(result["parsed_text"])
  50. def test_absolute_link_with_path_to_site_is_added_to_internal_links_list(
  51. request_mock, user
  52. ):
  53. text = "clean_links step cleans http://example.com/somewhere-something/"
  54. result = parse(text, request_mock, user, minify=False)
  55. assert result["internal_links"] == ["/somewhere-something/"]
  56. def test_full_link_with_path_text_is_set_to_domain_and_path(request_mock, user):
  57. text = "clean_links step cleans http://example.com/somewhere-something/"
  58. result = parse(text, request_mock, user, minify=False)
  59. assert ">example.com/somewhere-something/<" in result["parsed_text"]
  60. def test_outgoing_link_is_added_to_outgoing_links_list(request_mock, user):
  61. text = "clean_links step cleans https://other.com"
  62. result = parse(text, request_mock, user, minify=False)
  63. assert result["outgoing_links"] == ["other.com"]
  64. def test_outgoing_link_without_scheme_is_added_to_outgoing_links_list(
  65. request_mock, user
  66. ):
  67. text = "clean_links step cleans other.com"
  68. result = parse(text, request_mock, user, minify=False)
  69. assert result["outgoing_links"] == ["other.com"]
  70. def test_outgoing_link_with_path_is_added_to_outgoing_links_list(request_mock, user):
  71. text = "clean_links step cleans other.com/some/path/"
  72. result = parse(text, request_mock, user, minify=False)
  73. assert result["outgoing_links"] == ["other.com/some/path/"]
  74. def test_local_image_is_changed_to_relative_link(request_mock, user, snapshot):
  75. text = "clean_links step cleans !(example.com/media/img.png)"
  76. result = parse(text, request_mock, user, minify=False)
  77. snapshot.assert_match(result["parsed_text"])
  78. def test_local_image_is_added_to_images_list(request_mock, user):
  79. text = "clean_links step cleans !(example.com/media/img.png)"
  80. result = parse(text, request_mock, user, minify=False)
  81. assert result["images"] == ["/media/img.png"]
  82. def test_remote_image_is_added_to_images_list(request_mock, user):
  83. text = "clean_links step cleans !(other.com/media/img.png)"
  84. result = parse(text, request_mock, user, minify=False)
  85. assert result["images"] == ["other.com/media/img.png"]
  86. def test_local_image_link_is_added_to_images_and_links_lists(request_mock, user):
  87. text = "clean_links step cleans [!(example.com/media/img.png)](example.com/test/)"
  88. result = parse(text, request_mock, user, minify=False)
  89. assert result["internal_links"] == ["/test/"]
  90. assert result["images"] == ["/media/img.png"]
  91. def test_remote_image_link_is_added_to_images_and_links_lists(request_mock, user):
  92. text = "clean_links step cleans [!(other.com/media/img.png)](other.com/test/)"
  93. result = parse(text, request_mock, user, minify=False)
  94. assert result["outgoing_links"] == ["other.com/test/"]
  95. assert result["images"] == ["other.com/media/img.png"]
  96. def test_parser_adds_shva_to_attachment_link_querystring_if_force_option_is_enabled(
  97. request_mock, user
  98. ):
  99. text = "clean_links step cleans ![3.png](http://example.com/a/thumb/test/43/)"
  100. result = parse(text, request_mock, user, minify=False, force_shva=True)
  101. assert "/a/thumb/test/43/?shva=1" in result["parsed_text"]
  102. def test_parser_skips_shva_in_attachment_link_querystring_if_force_option_is_omitted(
  103. request_mock, user
  104. ):
  105. text = "clean_links step cleans ![3.png](http://example.com/a/thumb/test/43/)"
  106. result = parse(text, request_mock, user, minify=False)
  107. assert "?shva=1" not in result["parsed_text"]