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