logo

pleroma

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

twitter_card_test.exs (7559B)


  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.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.MediaProxy
  11. alias Pleroma.Web.Metadata.Providers.TwitterCard
  12. alias Pleroma.Web.Metadata.Utils
  13. alias Pleroma.Web.Router
  14. setup do: clear_config([Pleroma.Web.Metadata, :unfurl_nsfw])
  15. test "it renders twitter card for user info" do
  16. user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
  17. avatar_url = MediaProxy.preview_url(User.avatar_url(user))
  18. res = TwitterCard.build_tags(%{user: user})
  19. assert res == [
  20. {:meta, [name: "twitter:title", content: Utils.user_name_string(user)], []},
  21. {:meta, [name: "twitter:description", content: "born 19 March 1994"], []},
  22. {:meta, [name: "twitter:image", content: avatar_url], []},
  23. {:meta, [name: "twitter:card", content: "summary"], []}
  24. ]
  25. end
  26. test "it uses summary twittercard if post has no attachment" do
  27. user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
  28. {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
  29. note =
  30. insert(:note, %{
  31. data: %{
  32. "actor" => user.ap_id,
  33. "tag" => [],
  34. "id" => "https://pleroma.gov/objects/whatever",
  35. "summary" => "",
  36. "content" => "pleroma in a nutshell"
  37. }
  38. })
  39. result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})
  40. assert [
  41. {:meta, [name: "twitter:title", content: Utils.user_name_string(user)], []},
  42. {:meta, [name: "twitter:description", content: "pleroma in a nutshell"], []},
  43. {:meta, [name: "twitter:image", content: "http://localhost:4001/images/avi.png"],
  44. []},
  45. {:meta, [name: "twitter:card", content: "summary"], []}
  46. ] == result
  47. end
  48. test "it uses summary as description if post has one" do
  49. user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
  50. {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
  51. note =
  52. insert(:note, %{
  53. data: %{
  54. "actor" => user.ap_id,
  55. "tag" => [],
  56. "id" => "https://pleroma.gov/objects/whatever",
  57. "summary" => "Public service announcement on caffeine consumption",
  58. "content" => "cofe"
  59. }
  60. })
  61. result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})
  62. assert [
  63. {:meta, [name: "twitter:title", content: Utils.user_name_string(user)], []},
  64. {:meta,
  65. [
  66. name: "twitter:description",
  67. content: "Public service announcement on caffeine consumption"
  68. ], []},
  69. {:meta, [name: "twitter:image", content: "http://localhost:4001/images/avi.png"],
  70. []},
  71. {:meta, [name: "twitter:card", content: "summary"], []}
  72. ] == result
  73. end
  74. test "it renders avatar not attachment if post is nsfw and unfurl_nsfw is disabled" do
  75. clear_config([Pleroma.Web.Metadata, :unfurl_nsfw], false)
  76. user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
  77. {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
  78. note =
  79. insert(:note, %{
  80. data: %{
  81. "actor" => user.ap_id,
  82. "tag" => [],
  83. "id" => "https://pleroma.gov/objects/whatever",
  84. "summary" => "",
  85. "content" => "pleroma in a nutshell",
  86. "sensitive" => true,
  87. "attachment" => [
  88. %{
  89. "url" => [%{"mediaType" => "image/png", "href" => "https://pleroma.gov/tenshi.png"}]
  90. },
  91. %{
  92. "url" => [
  93. %{
  94. "mediaType" => "application/octet-stream",
  95. "href" => "https://pleroma.gov/fqa/badapple.sfc"
  96. }
  97. ]
  98. },
  99. %{
  100. "url" => [
  101. %{"mediaType" => "video/webm", "href" => "https://pleroma.gov/about/juche.webm"}
  102. ]
  103. }
  104. ]
  105. }
  106. })
  107. result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})
  108. assert [
  109. {:meta, [name: "twitter:title", content: Utils.user_name_string(user)], []},
  110. {:meta, [name: "twitter:description", content: "pleroma in a nutshell"], []},
  111. {:meta, [name: "twitter:image", content: "http://localhost:4001/images/avi.png"],
  112. []},
  113. {:meta, [name: "twitter:card", content: "summary"], []}
  114. ] == result
  115. end
  116. test "it renders supported types of attachments and skips unknown types" do
  117. user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
  118. {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
  119. note =
  120. insert(:note, %{
  121. data: %{
  122. "actor" => user.ap_id,
  123. "tag" => [],
  124. "id" => "https://pleroma.gov/objects/whatever",
  125. "summary" => "",
  126. "content" => "pleroma in a nutshell",
  127. "attachment" => [
  128. %{
  129. "url" => [
  130. %{
  131. "mediaType" => "image/png",
  132. "href" => "https://pleroma.gov/tenshi.png",
  133. "height" => 1024,
  134. "width" => 1280
  135. }
  136. ]
  137. },
  138. %{
  139. "url" => [
  140. %{
  141. "mediaType" => "application/octet-stream",
  142. "href" => "https://pleroma.gov/fqa/badapple.sfc"
  143. }
  144. ]
  145. },
  146. %{
  147. "url" => [
  148. %{
  149. "mediaType" => "video/webm",
  150. "href" => "https://pleroma.gov/about/juche.webm",
  151. "height" => 600,
  152. "width" => 800
  153. }
  154. ]
  155. }
  156. ]
  157. }
  158. })
  159. result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})
  160. assert [
  161. {:meta, [name: "twitter:title", content: Utils.user_name_string(user)], []},
  162. {:meta, [name: "twitter:description", content: "pleroma in a nutshell"], []},
  163. {:meta, [name: "twitter:card", content: "summary_large_image"], []},
  164. {:meta, [name: "twitter:image", content: "https://pleroma.gov/tenshi.png"], []},
  165. {:meta, [name: "twitter:image:alt", content: ""], []},
  166. {:meta, [name: "twitter:player:width", content: "1280"], []},
  167. {:meta, [name: "twitter:player:height", content: "1024"], []},
  168. {:meta, [name: "twitter:card", content: "player"], []},
  169. {:meta,
  170. [
  171. name: "twitter:player",
  172. content: Router.Helpers.o_status_url(Endpoint, :notice_player, activity.id)
  173. ], []},
  174. {:meta, [name: "twitter:player:width", content: "800"], []},
  175. {:meta, [name: "twitter:player:height", content: "600"], []},
  176. {:meta,
  177. [
  178. name: "twitter:player:stream",
  179. content: "https://pleroma.gov/about/juche.webm"
  180. ], []},
  181. {:meta, [name: "twitter:player:stream:content_type", content: "video/webm"], []}
  182. ] == result
  183. end
  184. end