f5f71cee502afe97b0469c1e45fcfb08577c2d75
[akkoma] / test / pleroma / web / metadata / providers / open_graph_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 end