logo

pleroma

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

twitter_card_test.exs (5706B)


  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.Metadata.Providers.TwitterCardTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.User
  8. alias Pleroma.Web.CommonAPI
  9. alias Pleroma.Web.Endpoint
  10. alias Pleroma.Web.Metadata.Providers.TwitterCard
  11. alias Pleroma.Web.Metadata.Utils
  12. alias Pleroma.Web.Router
  13. setup do: clear_config([Pleroma.Web.Metadata, :unfurl_nsfw])
  14. test "it renders twitter card for user info" do
  15. user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
  16. avatar_url = Utils.attachment_url(User.avatar_url(user))
  17. res = TwitterCard.build_tags(%{user: user})
  18. assert res == [
  19. {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
  20. {:meta, [property: "twitter:description", content: "born 19 March 1994"], []},
  21. {:meta, [property: "twitter:image", content: avatar_url], []},
  22. {:meta, [property: "twitter:card", content: "summary"], []}
  23. ]
  24. end
  25. test "it uses summary twittercard if post has no attachment" do
  26. user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
  27. {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
  28. note =
  29. insert(:note, %{
  30. data: %{
  31. "actor" => user.ap_id,
  32. "tag" => [],
  33. "id" => "https://pleroma.gov/objects/whatever",
  34. "content" => "pleroma in a nutshell"
  35. }
  36. })
  37. result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})
  38. assert [
  39. {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
  40. {:meta, [property: "twitter:description", content: "“pleroma in a nutshell”"], []},
  41. {:meta, [property: "twitter:image", content: "http://localhost:4001/images/avi.png"],
  42. []},
  43. {:meta, [property: "twitter:card", content: "summary"], []}
  44. ] == result
  45. end
  46. test "it renders avatar not attachment if post is nsfw and unfurl_nsfw is disabled" do
  47. Pleroma.Config.put([Pleroma.Web.Metadata, :unfurl_nsfw], false)
  48. user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
  49. {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
  50. note =
  51. insert(:note, %{
  52. data: %{
  53. "actor" => user.ap_id,
  54. "tag" => [],
  55. "id" => "https://pleroma.gov/objects/whatever",
  56. "content" => "pleroma in a nutshell",
  57. "sensitive" => true,
  58. "attachment" => [
  59. %{
  60. "url" => [%{"mediaType" => "image/png", "href" => "https://pleroma.gov/tenshi.png"}]
  61. },
  62. %{
  63. "url" => [
  64. %{
  65. "mediaType" => "application/octet-stream",
  66. "href" => "https://pleroma.gov/fqa/badapple.sfc"
  67. }
  68. ]
  69. },
  70. %{
  71. "url" => [
  72. %{"mediaType" => "video/webm", "href" => "https://pleroma.gov/about/juche.webm"}
  73. ]
  74. }
  75. ]
  76. }
  77. })
  78. result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})
  79. assert [
  80. {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
  81. {:meta, [property: "twitter:description", content: "“pleroma in a nutshell”"], []},
  82. {:meta, [property: "twitter:image", content: "http://localhost:4001/images/avi.png"],
  83. []},
  84. {:meta, [property: "twitter:card", content: "summary"], []}
  85. ] == result
  86. end
  87. test "it renders supported types of attachments and skips unknown types" do
  88. user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
  89. {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
  90. note =
  91. insert(:note, %{
  92. data: %{
  93. "actor" => user.ap_id,
  94. "tag" => [],
  95. "id" => "https://pleroma.gov/objects/whatever",
  96. "content" => "pleroma in a nutshell",
  97. "attachment" => [
  98. %{
  99. "url" => [%{"mediaType" => "image/png", "href" => "https://pleroma.gov/tenshi.png"}]
  100. },
  101. %{
  102. "url" => [
  103. %{
  104. "mediaType" => "application/octet-stream",
  105. "href" => "https://pleroma.gov/fqa/badapple.sfc"
  106. }
  107. ]
  108. },
  109. %{
  110. "url" => [
  111. %{"mediaType" => "video/webm", "href" => "https://pleroma.gov/about/juche.webm"}
  112. ]
  113. }
  114. ]
  115. }
  116. })
  117. result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})
  118. assert [
  119. {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
  120. {:meta, [property: "twitter:description", content: "“pleroma in a nutshell”"], []},
  121. {:meta, [property: "twitter:card", content: "summary_large_image"], []},
  122. {:meta, [property: "twitter:player", content: "https://pleroma.gov/tenshi.png"], []},
  123. {:meta, [property: "twitter:card", content: "player"], []},
  124. {:meta,
  125. [
  126. property: "twitter:player",
  127. content: Router.Helpers.o_status_url(Endpoint, :notice_player, activity.id)
  128. ], []},
  129. {:meta, [property: "twitter:player:width", content: "480"], []},
  130. {:meta, [property: "twitter:player:height", content: "480"], []}
  131. ] == result
  132. end
  133. end