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