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