summaryrefslogtreecommitdiff
path: root/test/pleroma/web/metadata/providers/open_graph_test.exs
blob: fc44b3cbdb741f4f0d0479ec90adaafe28f7ad05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.Metadata.Providers.OpenGraphTest do
  use Pleroma.DataCase
  import Pleroma.Factory
  alias Pleroma.Web.Metadata.Providers.OpenGraph

  setup do: clear_config([Pleroma.Web.Metadata, :unfurl_nsfw])

  test "it renders all supported types of attachments and skips unknown types" do
    user = insert(:user)

    note =
      insert(:note, %{
        data: %{
          "actor" => user.ap_id,
          "tag" => [],
          "id" => "https://pleroma.gov/objects/whatever",
          "content" => "pleroma in a nutshell",
          "attachment" => [
            %{
              "url" => [
                %{"mediaType" => "image/png", "href" => "https://pleroma.gov/tenshi.png"}
              ]
            },
            %{
              "url" => [
                %{
                  "mediaType" => "application/octet-stream",
                  "href" => "https://pleroma.gov/fqa/badapple.sfc"
                }
              ]
            },
            %{
              "url" => [
                %{"mediaType" => "video/webm", "href" => "https://pleroma.gov/about/juche.webm"}
              ]
            },
            %{
              "url" => [
                %{
                  "mediaType" => "audio/basic",
                  "href" => "http://www.gnu.org/music/free-software-song.au"
                }
              ]
            }
          ]
        }
      })

    result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})

    assert Enum.all?(
             [
               {:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []},
               {:meta,
                [property: "og:audio", content: "http://www.gnu.org/music/free-software-song.au"],
                []},
               {:meta, [property: "og:video", content: "https://pleroma.gov/about/juche.webm"],
                []}
             ],
             fn element -> element in result end
           )
  end

  test "it does not render attachments if post is nsfw" do
    clear_config([Pleroma.Web.Metadata, :unfurl_nsfw], false)
    user = insert(:user, avatar: %{"url" => [%{"href" => "https://pleroma.gov/tenshi.png"}]})

    note =
      insert(:note, %{
        data: %{
          "actor" => user.ap_id,
          "id" => "https://pleroma.gov/objects/whatever",
          "content" => "#cuteposting #nsfw #hambaga",
          "tag" => ["cuteposting", "nsfw", "hambaga"],
          "sensitive" => true,
          "attachment" => [
            %{
              "url" => [
                %{"mediaType" => "image/png", "href" => "https://misskey.microsoft/corndog.png"}
              ]
            }
          ]
        }
      })

    result = OpenGraph.build_tags(%{object: note, url: note.data["id"], user: user})

    assert {:meta, [property: "og:image", content: "https://pleroma.gov/tenshi.png"], []} in result

    refute {:meta, [property: "og:image", content: "https://misskey.microsoft/corndog.png"], []} in result
  end
end