Merge branch 'features/ingestion-page' into 'develop'
[akkoma] / test / pleroma / web / metadata / providers / twitter_card_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.TwitterCardTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.Endpoint
12 alias Pleroma.Web.MediaProxy
13 alias Pleroma.Web.Metadata.Providers.TwitterCard
14 alias Pleroma.Web.Metadata.Utils
15 alias Pleroma.Web.Router
16
17 setup do: clear_config([Pleroma.Web.Metadata, :unfurl_nsfw])
18
19 test "it renders twitter card for user info" do
20 user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
21 avatar_url = MediaProxy.preview_url(User.avatar_url(user))
22 res = TwitterCard.build_tags(%{user: user})
23
24 assert res == [
25 {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
26 {:meta, [property: "twitter:description", content: "born 19 March 1994"], []},
27 {:meta, [property: "twitter:image", content: avatar_url], []},
28 {:meta, [property: "twitter:card", content: "summary"], []}
29 ]
30 end
31
32 test "it uses summary twittercard if post has no attachment" do
33 user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
34 {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
35
36 note =
37 insert(:note, %{
38 data: %{
39 "actor" => user.ap_id,
40 "tag" => [],
41 "id" => "https://pleroma.gov/objects/whatever",
42 "content" => "pleroma in a nutshell"
43 }
44 })
45
46 result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})
47
48 assert [
49 {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
50 {:meta, [property: "twitter:description", content: "pleroma in a nutshell"], []},
51 {:meta, [property: "twitter:image", content: "http://localhost:4001/images/avi.png"],
52 []},
53 {:meta, [property: "twitter:card", content: "summary"], []}
54 ] == result
55 end
56
57 test "it renders avatar not attachment if post is nsfw and unfurl_nsfw is disabled" do
58 clear_config([Pleroma.Web.Metadata, :unfurl_nsfw], false)
59 user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
60 {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
61
62 note =
63 insert(:note, %{
64 data: %{
65 "actor" => user.ap_id,
66 "tag" => [],
67 "id" => "https://pleroma.gov/objects/whatever",
68 "content" => "pleroma in a nutshell",
69 "sensitive" => true,
70 "attachment" => [
71 %{
72 "url" => [%{"mediaType" => "image/png", "href" => "https://pleroma.gov/tenshi.png"}]
73 },
74 %{
75 "url" => [
76 %{
77 "mediaType" => "application/octet-stream",
78 "href" => "https://pleroma.gov/fqa/badapple.sfc"
79 }
80 ]
81 },
82 %{
83 "url" => [
84 %{"mediaType" => "video/webm", "href" => "https://pleroma.gov/about/juche.webm"}
85 ]
86 }
87 ]
88 }
89 })
90
91 result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})
92
93 assert [
94 {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
95 {:meta, [property: "twitter:description", content: "pleroma in a nutshell"], []},
96 {:meta, [property: "twitter:image", content: "http://localhost:4001/images/avi.png"],
97 []},
98 {:meta, [property: "twitter:card", content: "summary"], []}
99 ] == result
100 end
101
102 test "it renders supported types of attachments and skips unknown types" do
103 user = insert(:user, name: "Jimmy Hendriks", bio: "born 19 March 1994")
104 {:ok, activity} = CommonAPI.post(user, %{status: "HI"})
105
106 note =
107 insert(:note, %{
108 data: %{
109 "actor" => user.ap_id,
110 "tag" => [],
111 "id" => "https://pleroma.gov/objects/whatever",
112 "content" => "pleroma in a nutshell",
113 "attachment" => [
114 %{
115 "url" => [
116 %{
117 "mediaType" => "image/png",
118 "href" => "https://pleroma.gov/tenshi.png",
119 "height" => 1024,
120 "width" => 1280
121 }
122 ]
123 },
124 %{
125 "url" => [
126 %{
127 "mediaType" => "application/octet-stream",
128 "href" => "https://pleroma.gov/fqa/badapple.sfc"
129 }
130 ]
131 },
132 %{
133 "url" => [
134 %{
135 "mediaType" => "video/webm",
136 "href" => "https://pleroma.gov/about/juche.webm",
137 "height" => 600,
138 "width" => 800
139 }
140 ]
141 }
142 ]
143 }
144 })
145
146 result = TwitterCard.build_tags(%{object: note, user: user, activity_id: activity.id})
147
148 assert [
149 {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []},
150 {:meta, [property: "twitter:description", content: "pleroma in a nutshell"], []},
151 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
152 {:meta, [property: "twitter:player", content: "https://pleroma.gov/tenshi.png"], []},
153 {:meta, [property: "twitter:player:width", content: "1280"], []},
154 {:meta, [property: "twitter:player:height", content: "1024"], []},
155 {:meta, [property: "twitter:card", content: "player"], []},
156 {:meta,
157 [
158 property: "twitter:player",
159 content: Router.Helpers.o_status_url(Endpoint, :notice_player, activity.id)
160 ], []},
161 {:meta, [property: "twitter:player:width", content: "800"], []},
162 {:meta, [property: "twitter:player:height", content: "600"], []},
163 {:meta,
164 [
165 property: "twitter:player:stream",
166 content: "https://pleroma.gov/about/juche.webm"
167 ], []},
168 {:meta, [property: "twitter:player:stream:content_type", content: "video/webm"], []}
169 ] == result
170 end
171 end