twitter_card_test.exs 5.52 KB
Newer Older
Maksim's avatar
Maksim committed
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
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.Metadata.Providers.TwitterCardTest do
  use Pleroma.DataCase
  import Pleroma.Factory

  alias Pleroma.User
  alias Pleroma.Web.CommonAPI
  alias Pleroma.Web.Endpoint
  alias Pleroma.Web.Metadata.Providers.TwitterCard
  alias Pleroma.Web.Metadata.Utils
  alias Pleroma.Web.Router

  test "it renders twitter card for user info" do
    user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
    avatar_url = Utils.attachment_url(User.avatar_url(user))
    res = TwitterCard.build_tags(%{user: user})

    assert res == [
             {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
             {:meta, [property: "twitter:description", content: "born 19 March 1994"], []},
             {:meta, [property: "twitter:image", content: avatar_url], []},
             {:meta, [property: "twitter:card", content: "summary"], []}
           ]
  end

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
  test "it uses summary twittercard if post has no attachment" do
    user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
    {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})

    note =
      insert(:note, %{
        data: %{
          "actor" => user.ap_id,
          "tag" => [],
          "id" => "https://pleroma.gov/objects/whatever",
          "content" => "pleroma in a nutshell"
        }
      })

    result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})

    assert [
             {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
             {:meta, [property: "twitter:description", content: "“pleroma in a nutshell”"], []},
             {:meta, [property: "twitter:image", content: "http://localhost:4001/images/avi.png"],
              []},
             {:meta, [property: "twitter:card", content: "summary"], []}
           ] == result
  end

  test "it renders avatar not attachment if post is nsfw and unfurl_nsfw is disabled" do
Maksim's avatar
Maksim committed
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
    Pleroma.Config.put([Pleroma.Web.Metadata, :unfurl_nsfw], false)
    user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
    {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})

    note =
      insert(:note, %{
        data: %{
          "actor" => user.ap_id,
          "tag" => [],
          "id" => "https://pleroma.gov/objects/whatever",
          "content" => "pleroma in a nutshell",
          "sensitive" => true,
          "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"}
              ]
            }
          ]
        }
      })

    result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})

    assert [
             {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
             {:meta, [property: "twitter:description", content: "“pleroma in a nutshell”"], []},
             {:meta, [property: "twitter:image", content: "http://localhost:4001/images/avi.png"],
              []},
95
             {:meta, [property: "twitter:card", content: "summary"], []}
Maksim's avatar
Maksim committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
           ] == result
  end

  test "it renders supported types of attachments and skips unknown types" do
    user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
    {:ok, activity} = CommonAPI.post(user, %{"status" => "HI"})

    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"}
              ]
            }
          ]
        }
      })

    result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})

    assert [
             {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
             {:meta, [property: "twitter:description", content: "“pleroma in a nutshell”"], []},
             {:meta, [property: "twitter:card", content: "summary_large_image"], []},
             {:meta, [property: "twitter:player", content: "https://pleroma.gov/tenshi.png"], []},
             {:meta, [property: "twitter:card", content: "player"], []},
             {:meta,
              [
                property: "twitter:player",
                content: Router.Helpers.o_status_url(Endpoint, :notice_player, activity.id)
              ], []},
             {:meta, [property: "twitter:player:width", content: "480"], []},
             {:meta, [property: "twitter:player:height", content: "480"], []}
           ] == result
  end
end