logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

twitter_card_test.exs (6167B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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. "app:id:googleplay" => "com.nytimes.android",
  17. "app:name:googleplay" => "NYTimes",
  18. "app:url:googleplay" => "nytimes://reader/id/100000006583622",
  19. "site" => nil,
  20. "description" =>
  21. "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
  22. "image" =>
  23. "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-facebookJumbo.jpg",
  24. "type" => "article",
  25. "url" =>
  26. "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html",
  27. "title" =>
  28. "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database."
  29. }
  30. end
  31. test "parses twitter card with only property attributes" do
  32. html =
  33. File.read!("test/fixtures/nypd-facial-recognition-children-teenagers2.html")
  34. |> Floki.parse_document!()
  35. assert TwitterCard.parse(html, %{}) ==
  36. %{
  37. "card" => "summary_large_image",
  38. "description" =>
  39. "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
  40. "image" =>
  41. "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-videoSixteenByNineJumbo1600.jpg",
  42. "image:alt" => "",
  43. "title" =>
  44. "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
  45. "url" =>
  46. "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html",
  47. "type" => "article"
  48. }
  49. end
  50. test "parses twitter card with name & property attributes" do
  51. html =
  52. File.read!("test/fixtures/nypd-facial-recognition-children-teenagers.html")
  53. |> Floki.parse_document!()
  54. assert TwitterCard.parse(html, %{}) ==
  55. %{
  56. "app:id:googleplay" => "com.nytimes.android",
  57. "app:name:googleplay" => "NYTimes",
  58. "app:url:googleplay" => "nytimes://reader/id/100000006583622",
  59. "card" => "summary_large_image",
  60. "description" =>
  61. "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
  62. "image" =>
  63. "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-videoSixteenByNineJumbo1600.jpg",
  64. "image:alt" => "",
  65. "site" => nil,
  66. "title" =>
  67. "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
  68. "url" =>
  69. "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html",
  70. "type" => "article"
  71. }
  72. end
  73. test "respect only first title tag on the page" do
  74. image_path =
  75. "https://assets.atlasobscura.com/media/W1siZiIsInVwbG9hZHMvYXNzZXRzLzkwYzgyMzI4LThlMDUtNGRiNS05MDg3LTUzMGUxZTM5N2RmMmVkOTM5ZDM4MGM4OTIx" <>
  76. "YTQ5MF9EQVIgZXhodW1hdGlvbiBvZiBNYXJnYXJldCBDb3JiaW4gZ3JhdmUgMTkyNi5qcGciXSxbInAiLCJjb252ZXJ0IiwiIl0sWyJwIiwiY29udmVydCIsIi1xdWFsaXR5IDgxIC1hdXRvLW9" <>
  77. "yaWVudCJdLFsicCIsInRodW1iIiwiNjAweD4iXV0/DAR%20exhumation%20of%20Margaret%20Corbin%20grave%201926.jpg"
  78. html =
  79. File.read!("test/fixtures/margaret-corbin-grave-west-point.html") |> Floki.parse_document!()
  80. assert TwitterCard.parse(html, %{}) ==
  81. %{
  82. "site" => "@atlasobscura",
  83. "title" => "The Missing Grave of Margaret Corbin, Revolutionary War Veteran",
  84. "card" => "summary_large_image",
  85. "image" => image_path,
  86. "description" =>
  87. "She's the only woman veteran honored with a monument at West Point. But where was she buried?",
  88. "site_name" => "Atlas Obscura",
  89. "type" => "article",
  90. "url" => "http://www.atlasobscura.com/articles/margaret-corbin-grave-west-point"
  91. }
  92. end
  93. test "takes first founded title in html head if there is html markup error" do
  94. html =
  95. File.read!("test/fixtures/nypd-facial-recognition-children-teenagers4.html")
  96. |> Floki.parse_document!()
  97. assert TwitterCard.parse(html, %{}) ==
  98. %{
  99. "site" => nil,
  100. "title" =>
  101. "She Was Arrested at 14. Then Her Photo Went to a Facial Recognition Database.",
  102. "app:id:googleplay" => "com.nytimes.android",
  103. "app:name:googleplay" => "NYTimes",
  104. "app:url:googleplay" => "nytimes://reader/id/100000006583622",
  105. "description" =>
  106. "With little oversight, the N.Y.P.D. has been using powerful surveillance technology on photos of children and teenagers.",
  107. "image" =>
  108. "https://static01.nyt.com/images/2019/08/01/nyregion/01nypd-juveniles-promo/01nypd-juveniles-promo-facebookJumbo.jpg",
  109. "type" => "article",
  110. "url" =>
  111. "https://www.nytimes.com/2019/08/01/nyregion/nypd-facial-recognition-children-teenagers.html"
  112. }
  113. end
  114. end