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