First
[anni] / test / pleroma / web / metadata / providers / open_graph_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.OpenGraphTest do
6   use Pleroma.DataCase
7   import Pleroma.Factory
8   alias Pleroma.Web.Metadata.Providers.OpenGraph
9
10   setup do: clear_config([Pleroma.Web.Metadata, :unfurl_nsfw])
11
12   test "it renders all supported types of attachments and skips unknown types" do
13     user = insert(:user)
14
15     note =
16       insert(:note, %{
17         data: %{
18           "actor" => user.ap_id,
19           "tag" => [],
20           "id" => "https://pleroma.gov/objects/whatever",
21           "content" => "pleroma in a nutshell",
22           "attachment" => [
23             %{
24               "url" => [
25                 %{
26                   "mediaType" => "image/png",
27                   "href" => "https://pleroma.gov/tenshi.png",
28                   "height" => 1024,
29                   "width" => 1280
30                 }
31               ]
32             },
33             %{
34               "url" => [
35                 %{
36                   "mediaType" => "application/octet-stream",
37                   "href" => "https://pleroma.gov/fqa/badapple.sfc"
38                 }
39               ]
40             },
41             %{
42               "url" => [
43                 %{
44                   "mediaType" => "video/webm",
45                   "href" => "https://pleroma.gov/about/juche.webm",
46                   "height" => 600,
47                   "width" => 800
48                 }
49               ]
50             },
51             %{
52               "url" => [
53                 %{
54                   "mediaType" => "audio/basic",
55                   "href" => "http://www.gnu.org/music/free-software-song.au"
56                 }
57               ]
58             }
59           ]
60         }
61       })
62
63     result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
64
65     assert Enum.all?(
66              [
67                {:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []},
68                {:meta, [property: "og:image:width", content: "1280"], []},
69                {:meta, [property: "og:image:height", content: "1024"], []},
70                {:meta,
71                 [property: "og:audio", content: "http://www.gnu.org/music/free-software-song.au"],
72                 []},
73                {:meta, [property: "og:video", content: "https://pleroma.gov/about/juche.webm"],
74                 []},
75                {:meta, [property: "og:video:width", content: "800"], []},
76                {:meta, [property: "og:video:height", content: "600"], []}
77              ],
78              fn element -> element in result end
79            )
80   end
81
82   test "it does not render attachments if post is nsfw" do
83     clear_config([Pleroma.Web.Metadata, :unfurl_nsfw], false)
84     user = insert(:user, avatar: %{"url" => [%{"href" => "https://pleroma.gov/tenshi.png"}]})
85
86     note =
87       insert(:note, %{
88         data: %{
89           "actor" => user.ap_id,
90           "id" => "https://pleroma.gov/objects/whatever",
91           "content" => "#cuteposting #nsfw #hambaga",
92           "tag" => ["cuteposting", "nsfw", "hambaga"],
93           "sensitive" => true,
94           "attachment" => [
95             %{
96               "url" => [
97                 %{"mediaType" => "image/png", "href" => "https://misskey.microsoft/corndog.png"}
98               ]
99             }
100           ]
101         }
102       })
103
104     result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
105
106     assert {:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []} in result
107
108     refute {:meta, [property: "og:image", content: "https://misskey.microsoft/corndog.png"], []} in result
109   end
110
111   test "video attachments have image thumbnail with WxH metadata with Preview Proxy enabled" do
112     clear_config([:media_proxy, :enabled], true)
113     clear_config([:media_preview_proxy, :enabled], true)
114     user = insert(:user)
115
116     note =
117       insert(:note, %{
118         data: %{
119           "actor" => user.ap_id,
120           "id" => "https://pleroma.gov/objects/whatever",
121           "content" => "test video post",
122           "sensitive" => false,
123           "attachment" => [
124             %{
125               "url" => [
126                 %{
127                   "mediaType" => "video/webm",
128                   "href" => "https://pleroma.gov/about/juche.webm",
129                   "height" => 600,
130                   "width" => 800
131                 }
132               ]
133             }
134           ]
135         }
136       })
137
138     result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
139
140     assert {:meta, [property: "og:image:width", content: "800"], []} in result
141     assert {:meta, [property: "og:image:height", content: "600"], []} in result
142
143     assert {:meta,
144             [
145               property: "og:image",
146               content:
147                 "http://localhost:4001/proxy/preview/LzAnlke-l5oZbNzWsrHfprX1rGw/aHR0cHM6Ly9wbGVyb21hLmdvdi9hYm91dC9qdWNoZS53ZWJt/juche.webm"
148             ], []} in result
149   end
150
151   test "video attachments have no image thumbnail with Preview Proxy disabled" do
152     clear_config([:media_proxy, :enabled], true)
153     clear_config([:media_preview_proxy, :enabled], false)
154     user = insert(:user)
155
156     note =
157       insert(:note, %{
158         data: %{
159           "actor" => user.ap_id,
160           "id" => "https://pleroma.gov/objects/whatever",
161           "content" => "test video post",
162           "sensitive" => false,
163           "attachment" => [
164             %{
165               "url" => [
166                 %{
167                   "mediaType" => "video/webm",
168                   "href" => "https://pleroma.gov/about/juche.webm",
169                   "height" => 600,
170                   "width" => 800
171                 }
172               ]
173             }
174           ]
175         }
176       })
177
178     result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})
179
180     refute {:meta, [property: "og:image:width", content: "800"], []} in result
181     refute {:meta, [property: "og:image:height", content: "600"], []} in result
182
183     refute {:meta,
184             [
185               property: "og:image",
186               content:
187                 "http://localhost:4001/proxy/preview/LzAnlke-l5oZbNzWsrHfprX1rGw/aHR0cHM6Ly9wbGVyb21hLmdvdi9hYm91dC9qdWNoZS53ZWJt/juche.webm"
188             ], []} in result
189   end
190 end