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.

277 lines
7.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. defmodule AutoLinker.ParserTest do
  2. use ExUnit.Case, async: true
  3. doctest AutoLinker.Parser
  4. import AutoLinker.Parser
  5. describe "url?/2" do
  6. test "valid scheme true" do
  7. valid_scheme_urls()
  8. |> Enum.each(fn url ->
  9. assert url?(url, scheme: true, validate_tld: true)
  10. end)
  11. end
  12. test "invalid scheme true" do
  13. invalid_scheme_urls()
  14. |> Enum.each(fn url ->
  15. refute url?(url, scheme: true, validate_tld: true)
  16. end)
  17. end
  18. test "valid scheme false" do
  19. valid_non_scheme_urls()
  20. |> Enum.each(fn url ->
  21. assert url?(url, scheme: false, validate_tld: true)
  22. end)
  23. end
  24. test "invalid scheme false" do
  25. invalid_non_scheme_urls()
  26. |> Enum.each(fn url ->
  27. refute url?(url, scheme: false, validate_tld: true)
  28. end)
  29. end
  30. test "checks the tld for url with a scheme when validate_tld: true" do
  31. custom_tld_scheme_urls()
  32. |> Enum.each(fn url ->
  33. refute url?(url, scheme: true, validate_tld: true)
  34. end)
  35. end
  36. test "does not check the tld for url with a scheme when validate_tld: false" do
  37. custom_tld_scheme_urls()
  38. |> Enum.each(fn url ->
  39. assert url?(url, scheme: true, validate_tld: false)
  40. end)
  41. end
  42. test "does not check the tld for url with a scheme when validate_tld: :no_scheme" do
  43. custom_tld_scheme_urls()
  44. |> Enum.each(fn url ->
  45. assert url?(url, scheme: true, validate_tld: :no_scheme)
  46. end)
  47. end
  48. test "checks the tld for url without a scheme when validate_tld: true" do
  49. custom_tld_non_scheme_urls()
  50. |> Enum.each(fn url ->
  51. refute url?(url, scheme: false, validate_tld: true)
  52. end)
  53. end
  54. test "checks the tld for url without a scheme when validate_tld: :no_scheme" do
  55. custom_tld_non_scheme_urls()
  56. |> Enum.each(fn url ->
  57. refute url?(url, scheme: false, validate_tld: :no_scheme)
  58. end)
  59. end
  60. test "does not check the tld for url without a scheme when validate_tld: false" do
  61. custom_tld_non_scheme_urls()
  62. |> Enum.each(fn url ->
  63. assert url?(url, scheme: false, validate_tld: false)
  64. end)
  65. end
  66. end
  67. describe "match_phone" do
  68. test "valid" do
  69. valid_phone_nunbers()
  70. |> Enum.each(fn number ->
  71. assert number |> match_phone() |> valid_number?(number)
  72. end)
  73. end
  74. test "invalid" do
  75. invalid_phone_numbers()
  76. |> Enum.each(fn number ->
  77. assert number |> match_phone() |> is_nil
  78. end)
  79. end
  80. end
  81. describe "parse" do
  82. test "handle line breakes" do
  83. text = "google.com\r\nssss"
  84. expected =
  85. "<a href=\"http://google.com\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>\r\nssss"
  86. assert parse(text) == expected
  87. end
  88. test "does not link attributes" do
  89. text = "Check out <a href='google.com'>google</a>"
  90. assert parse(text) == text
  91. text = "Check out <img src='google.com' alt='google.com'/>"
  92. assert parse(text) == text
  93. text = "Check out <span><img src='google.com' alt='google.com'/></span>"
  94. assert parse(text) == text
  95. end
  96. test "does not link inside `<pre>` and `<code>`" do
  97. text = "<pre>google.com</pre>"
  98. assert parse(text) == text
  99. text = "<code>google.com</code>"
  100. assert parse(text) == text
  101. text = "<pre><code>google.com</code></pre>"
  102. assert parse(text) == text
  103. end
  104. test "links url inside html" do
  105. text = "<div>google.com</div>"
  106. expected = "<div><a href=\"http://google.com\">google.com</a></div>"
  107. assert parse(text, class: false, rel: false, new_window: false, phone: false) == expected
  108. text = "Check out <div class='section'>google.com</div>"
  109. expected =
  110. "Check out <div class='section'><a href=\"http://google.com\">google.com</a></div>"
  111. assert parse(text, class: false, rel: false, new_window: false) == expected
  112. end
  113. test "links url inside nested html" do
  114. text = "<p><strong>google.com</strong></p>"
  115. expected = "<p><strong><a href=\"http://google.com\">google.com</a></strong></p>"
  116. assert parse(text, class: false, rel: false, new_window: false) == expected
  117. end
  118. test "excludes html with specified class" do
  119. text = "```Check out <div class='section'>google.com</div>```"
  120. assert parse(text, exclude_patterns: ["```"]) == text
  121. end
  122. test "do not link parens" do
  123. text = " foo (https://example.com/path/folder/), bar"
  124. expected =
  125. " foo (<a href=\"https://example.com/path/folder/\">example.com/path/folder/</a>), bar"
  126. assert parse(text, class: false, rel: false, new_window: false, scheme: true) == expected
  127. text = " foo (example.com/path/folder/), bar"
  128. expected =
  129. " foo (<a href=\"http://example.com/path/folder/\">example.com/path/folder/</a>), bar"
  130. assert parse(text, class: false, rel: false, new_window: false) == expected
  131. end
  132. test "do not link urls" do
  133. text = "google.com"
  134. assert parse(text, url: false, phone: true) == text
  135. end
  136. test "do not link `:test.test`" do
  137. text = ":test.test"
  138. assert parse(text, %{
  139. scheme: true,
  140. extra: true,
  141. class: false,
  142. strip_prefix: false,
  143. new_window: false,
  144. rel: false
  145. }) == text
  146. end
  147. end
  148. def valid_number?([list], number) do
  149. assert List.last(list) == number
  150. end
  151. def valid_number?(_, _), do: false
  152. def valid_scheme_urls,
  153. do: [
  154. "https://www.example.com",
  155. "http://www2.example.com",
  156. "http://home.example-site.com",
  157. "http://blog.example.com",
  158. "http://www.example.com/product",
  159. "http://www.example.com/products?id=1&page=2",
  160. "http://www.example.com#up",
  161. "http://255.255.255.255",
  162. "http://www.site.com:8008"
  163. ]
  164. def invalid_scheme_urls,
  165. do: [
  166. "http://invalid.com/perl.cgi?key= | http://web-site.com/cgi-bin/perl.cgi?key1=value1&key2"
  167. ]
  168. def valid_non_scheme_urls,
  169. do: [
  170. "www.example.com",
  171. "www2.example.com",
  172. "www.example.com:2000",
  173. "www.example.com?abc=1",
  174. "example.example-site.com",
  175. "example.com",
  176. "example.ca",
  177. "example.tv",
  178. "example.com:999?one=one",
  179. "255.255.255.255",
  180. "255.255.255.255:3000?one=1&two=2"
  181. ]
  182. def invalid_non_scheme_urls,
  183. do: [
  184. "invalid.com/perl.cgi?key= | web-site.com/cgi-bin/perl.cgi?key1=value1&key2",
  185. "invalid.",
  186. "hi..there",
  187. "555.555.5555"
  188. ]
  189. def valid_phone_nunbers,
  190. do: [
  191. "x55",
  192. "x555",
  193. "x5555",
  194. "x12345",
  195. "+1 555 555-5555",
  196. "555 555-5555",
  197. "555.555.5555",
  198. "613-555-5555",
  199. "1 (555) 555-5555",
  200. "(555) 555-5555",
  201. "1.555.555.5555",
  202. "800 555-5555",
  203. "1.800.555.5555",
  204. "1 (800) 555-5555",
  205. "888 555-5555",
  206. "887 555-5555",
  207. "1-877-555-5555",
  208. "1 800 710-5515"
  209. ]
  210. def invalid_phone_numbers,
  211. do: [
  212. "5555",
  213. "x5",
  214. "(555) 555-55"
  215. ]
  216. def custom_tld_scheme_urls,
  217. do: [
  218. "http://whatever.null/",
  219. "https://example.o/index.html",
  220. "http://pleroma.i2p/test",
  221. "http://misskey.loki"
  222. ]
  223. def custom_tld_non_scheme_urls,
  224. do: [
  225. "whatever.null/",
  226. "example.o/index.html",
  227. "pleroma.i2p/test",
  228. "misskey.loki"
  229. ]
  230. end