logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

twitter_card_test.exs (6315B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.RichMedia.Parsers.TwitterCardTest do
  5. use ExUnit.Case, async: true
  6. alias Pleroma.Web.RichMedia.Parsers.TwitterCard
  7. test "returns error when html not contains twitter card" do
  8. assert TwitterCard.parse([{"html", [], [{"head", [], []}, {"body", [], []}]}], %{}) == %{}
  9. end
  10. test "parses twitter card with only name attributes" do
  11. html =
  12. File.read!("test/fixtures/nypd-facial-recognition-children-teenagers3.html")
  13. |> Floki.parse_document!()
  14. assert TwitterCard.parse(html, %{}) ==
  15. %{
  16. "description" =>
  17. "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
  18. "image" =>
  19. "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-facebookJumbo.jpg",
  20. "type" => "article",
  21. "url" =>
  22. "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html",
  23. "title" =>
  24. "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database."
  25. }
  26. end
  27. test "parses twitter card with only property attributes" do
  28. html =
  29. File.read!("test/fixtures/nypd-facial-recognition-children-teenagers2.html")
  30. |> Floki.parse_document!()
  31. assert TwitterCard.parse(html, %{}) ==
  32. %{
  33. "card" => "summary_large_image",
  34. "description" =>
  35. "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
  36. "image" =>
  37. "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-facebookJumbo.jpg",
  38. "image:alt" => "",
  39. "title" =>
  40. "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
  41. "url" =>
  42. "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html",
  43. "type" => "article"
  44. }
  45. end
  46. test "parses twitter card with name & property attributes" do
  47. html =
  48. File.read!("test/fixtures/nypd-facial-recognition-children-teenagers.html")
  49. |> Floki.parse_document!()
  50. assert TwitterCard.parse(html, %{}) ==
  51. %{
  52. "card" => "summary_large_image",
  53. "description" =>
  54. "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
  55. "image" =>
  56. "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-facebookJumbo.jpg",
  57. "image:alt" => "",
  58. "title" =>
  59. "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
  60. "url" =>
  61. "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html",
  62. "type" => "article"
  63. }
  64. end
  65. test "respect only first title tag on the page" do
  66. image_path =
  67. "https://assets.atlasobscura.com/media/W1siZiIsInVwbG9hZHMvYXNzZXRzLzkwYzgyMzI4LThlMDUtNGRiNS05MDg3LTUzMGUxZTM5N2RmMmVkOTM5ZDM4MGM4OTIx" <>
  68. "YTQ5MF9EQVIgZXhodW1hdGlvbiBvZiBNYXJnYXJldCBDb3JiaW4gZ3JhdmUgMTkyNi5qcGciXSxbInAiLCJjb252ZXJ0IiwiIl0sWyJwIiwiY29udmVydCIsIi1xdWFsaXR5IDgxIC1hdXRvLW9" <>
  69. "yaWVudCJdLFsicCIsInRodW1iIiwiNjAweD4iXV0/DAR%20exhumation%20of%20Margaret%20Corbin%20grave%201926.jpg"
  70. html =
  71. File.read!("test/fixtures/margaret-corbin-grave-west-point.html") |> Floki.parse_document!()
  72. assert TwitterCard.parse(html, %{}) ==
  73. %{
  74. "title" => "The Missing Grave of Margaret Corbin, Revolutionary War Veteran",
  75. "card" => "summary_large_image",
  76. "image" => image_path,
  77. "description" =>
  78. "She's the only woman veteran honored with a monument at West Point. But where was she buried?",
  79. "type" => "article",
  80. "url" => "http://www.atlasobscura.com/articles/margaret-corbin-grave-west-point"
  81. }
  82. end
  83. test "takes first founded title in html head if there is html markup error" do
  84. html =
  85. File.read!("test/fixtures/nypd-facial-recognition-children-teenagers4.html")
  86. |> Floki.parse_document!()
  87. assert TwitterCard.parse(html, %{}) ==
  88. %{
  89. "title" =>
  90. "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
  91. "description" =>
  92. "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
  93. "image" =>
  94. "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-facebookJumbo.jpg",
  95. "type" => "article",
  96. "url" =>
  97. "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html"
  98. }
  99. end
  100. test "takes first image if multiple are specified" do
  101. html =
  102. File.read!("test/fixtures/fulmo.html")
  103. |> Floki.parse_document!()
  104. assert TwitterCard.parse(html, %{}) ==
  105. %{
  106. "description" => "Pri feoj, kiuj devis ordigi falintan arbon.",
  107. "image" => "https://tirifto.xwx.moe/r/ilustrajhoj/pinglordigado.png",
  108. "title" => "Fulmo",
  109. "type" => "website",
  110. "url" => "https://tirifto.xwx.moe/eo/rakontoj/fulmo.html",
  111. "image:alt" =>
  112. "Meze de arbaro kuŝas falinta trunko, sen pingloj kaj kun branĉoj derompitaj. Post ĝi videblas du feoj: florofeo maldekstre kaj nubofeo dekstre. La florofeo iom kaŝas sin post la trunko. La nubofeo staras kaj tenas amason da pigloj. Ili iom rigardas al si.",
  113. "image:height" => "630",
  114. "image:width" => "1200"
  115. }
  116. end
  117. end