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.

311 lines
8.2 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 "email?" do
  68. test "identifies valid emails" do
  69. valid_emails()
  70. |> Enum.each(fn email ->
  71. assert email?(email, [])
  72. end)
  73. end
  74. test "identifies invalid emails" do
  75. invalid_emails()
  76. |> Enum.each(fn email ->
  77. refute email?(email, [])
  78. end)
  79. end
  80. test "does not validate tlds when validate_tld: false" do
  81. valid_custom_tld_emails()
  82. |> Enum.each(fn email ->
  83. assert email?(email, validate_tld: false)
  84. end)
  85. end
  86. test "validates tlds when validate_tld: true" do
  87. valid_custom_tld_emails()
  88. |> Enum.each(fn email ->
  89. refute email?(email, validate_tld: true)
  90. end)
  91. end
  92. end
  93. describe "match_phone" do
  94. test "valid" do
  95. valid_phone_nunbers()
  96. |> Enum.each(fn number ->
  97. assert number |> match_phone() |> valid_number?(number)
  98. end)
  99. end
  100. test "invalid" do
  101. invalid_phone_numbers()
  102. |> Enum.each(fn number ->
  103. assert number |> match_phone() |> is_nil
  104. end)
  105. end
  106. end
  107. describe "parse" do
  108. test "handle line breakes" do
  109. text = "google.com\r\nssss"
  110. expected =
  111. "<a href=\"http://google.com\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>\r\nssss"
  112. assert parse(text) == expected
  113. end
  114. test "does not link attributes" do
  115. text = "Check out <a href='google.com'>google</a>"
  116. assert parse(text) == text
  117. text = "Check out <img src='google.com' alt='google.com'/>"
  118. assert parse(text) == text
  119. text = "Check out <span><img src='google.com' alt='google.com'/></span>"
  120. assert parse(text) == text
  121. end
  122. test "does not link inside `<pre>` and `<code>`" do
  123. text = "<pre>google.com</pre>"
  124. assert parse(text) == text
  125. text = "<code>google.com</code>"
  126. assert parse(text) == text
  127. text = "<pre><code>google.com</code></pre>"
  128. assert parse(text) == text
  129. end
  130. test "links url inside html" do
  131. text = "<div>google.com</div>"
  132. expected = "<div><a href=\"http://google.com\">google.com</a></div>"
  133. assert parse(text, class: false, rel: false, new_window: false, phone: false) == expected
  134. text = "Check out <div class='section'>google.com</div>"
  135. expected =
  136. "Check out <div class='section'><a href=\"http://google.com\">google.com</a></div>"
  137. assert parse(text, class: false, rel: false, new_window: false) == expected
  138. end
  139. test "links url inside nested html" do
  140. text = "<p><strong>google.com</strong></p>"
  141. expected = "<p><strong><a href=\"http://google.com\">google.com</a></strong></p>"
  142. assert parse(text, class: false, rel: false, new_window: false) == expected
  143. end
  144. test "excludes html with specified class" do
  145. text = "```Check out <div class='section'>google.com</div>```"
  146. assert parse(text, exclude_patterns: ["```"]) == text
  147. end
  148. test "do not link parens" do
  149. text = " foo (https://example.com/path/folder/), bar"
  150. expected =
  151. " foo (<a href=\"https://example.com/path/folder/\">example.com/path/folder/</a>), bar"
  152. assert parse(text, class: false, rel: false, new_window: false, scheme: true) == expected
  153. text = " foo (example.com/path/folder/), bar"
  154. expected =
  155. " foo (<a href=\"http://example.com/path/folder/\">example.com/path/folder/</a>), bar"
  156. assert parse(text, class: false, rel: false, new_window: false) == expected
  157. end
  158. test "do not link urls" do
  159. text = "google.com"
  160. assert parse(text, url: false, phone: true) == text
  161. end
  162. test "do not link `:test.test`" do
  163. text = ":test.test"
  164. assert parse(text, %{
  165. scheme: true,
  166. extra: true,
  167. class: false,
  168. strip_prefix: false,
  169. new_window: false,
  170. rel: false
  171. }) == text
  172. end
  173. end
  174. def valid_number?([list], number) do
  175. assert List.last(list) == number
  176. end
  177. def valid_number?(_, _), do: false
  178. def valid_scheme_urls,
  179. do: [
  180. "https://www.example.com",
  181. "http://www2.example.com",
  182. "http://home.example-site.com",
  183. "http://blog.example.com",
  184. "http://www.example.com/product",
  185. "http://www.example.com/products?id=1&page=2",
  186. "http://www.example.com#up",
  187. "http://255.255.255.255",
  188. "http://www.site.com:8008"
  189. ]
  190. def invalid_scheme_urls,
  191. do: [
  192. "http://invalid.com/perl.cgi?key= | http://web-site.com/cgi-bin/perl.cgi?key1=value1&key2"
  193. ]
  194. def valid_non_scheme_urls,
  195. do: [
  196. "www.example.com",
  197. "www2.example.com",
  198. "www.example.com:2000",
  199. "www.example.com?abc=1",
  200. "example.example-site.com",
  201. "example.com",
  202. "example.ca",
  203. "example.tv",
  204. "example.com:999?one=one",
  205. "255.255.255.255",
  206. "255.255.255.255:3000?one=1&two=2"
  207. ]
  208. def invalid_non_scheme_urls,
  209. do: [
  210. "invalid.com/perl.cgi?key= | web-site.com/cgi-bin/perl.cgi?key1=value1&key2",
  211. "invalid.",
  212. "hi..there",
  213. "555.555.5555"
  214. ]
  215. def valid_phone_nunbers,
  216. do: [
  217. "x55",
  218. "x555",
  219. "x5555",
  220. "x12345",
  221. "+1 555 555-5555",
  222. "555 555-5555",
  223. "555.555.5555",
  224. "613-555-5555",
  225. "1 (555) 555-5555",
  226. "(555) 555-5555",
  227. "1.555.555.5555",
  228. "800 555-5555",
  229. "1.800.555.5555",
  230. "1 (800) 555-5555",
  231. "888 555-5555",
  232. "887 555-5555",
  233. "1-877-555-5555",
  234. "1 800 710-5515"
  235. ]
  236. def invalid_phone_numbers,
  237. do: [
  238. "5555",
  239. "x5",
  240. "(555) 555-55"
  241. ]
  242. def custom_tld_scheme_urls,
  243. do: [
  244. "http://whatever.null/",
  245. "https://example.o/index.html",
  246. "http://pleroma.i2p/test",
  247. "http://misskey.loki"
  248. ]
  249. def custom_tld_non_scheme_urls,
  250. do: [
  251. "whatever.null/",
  252. "example.o/index.html",
  253. "pleroma.i2p/test",
  254. "misskey.loki"
  255. ]
  256. def valid_emails, do: ["rms@ai.mit.edu", "vc@cock.li"]
  257. def invalid_emails, do: ["rms[at]ai.mit.edu", "vc@cock", "xmpp:lain@trashserver.net"]
  258. def valid_custom_tld_emails, do: ["guardian@33y6fjyhs3phzfjj.onion", "hi@company.null"]
  259. end