tests: mastodon api: add test verifying that peertube videos are correctly rendered
[akkoma] / test / web / mastodon_api / status_view_test.exs
1 defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Web.MastodonAPI.{StatusView, AccountView}
5 alias Pleroma.User
6 alias Pleroma.Web.OStatus
7 alias Pleroma.Web.CommonAPI
8 alias Pleroma.Web.ActivityPub.ActivityPub
9 alias Pleroma.Activity
10 import Pleroma.Factory
11 import Tesla.Mock
12
13 setup do
14 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
15 :ok
16 end
17
18 test "a note with null content" do
19 note = insert(:note_activity)
20
21 data =
22 note.data
23 |> put_in(["object", "content"], nil)
24
25 note =
26 note
27 |> Map.put(:data, data)
28
29 User.get_cached_by_ap_id(note.data["actor"])
30
31 status = StatusView.render("status.json", %{activity: note})
32
33 assert status.content == ""
34 end
35
36 test "a note activity" do
37 note = insert(:note_activity)
38 user = User.get_cached_by_ap_id(note.data["actor"])
39
40 status = StatusView.render("status.json", %{activity: note})
41
42 created_at =
43 (note.data["object"]["published"] || "")
44 |> String.replace(~r/\.\d+Z/, ".000Z")
45
46 expected = %{
47 id: to_string(note.id),
48 uri: note.data["object"]["id"],
49 url: note.data["object"]["id"],
50 account: AccountView.render("account.json", %{user: user}),
51 in_reply_to_id: nil,
52 in_reply_to_account_id: nil,
53 reblog: nil,
54 content: HtmlSanitizeEx.basic_html(note.data["object"]["content"]),
55 created_at: created_at,
56 reblogs_count: 0,
57 replies_count: 0,
58 favourites_count: 0,
59 reblogged: false,
60 favourited: false,
61 muted: false,
62 sensitive: false,
63 spoiler_text: note.data["object"]["summary"],
64 visibility: "public",
65 media_attachments: [],
66 mentions: [],
67 tags: [
68 %{
69 name: "#{note.data["object"]["tag"]}",
70 url: "/tag/#{note.data["object"]["tag"]}"
71 }
72 ],
73 application: %{
74 name: "Web",
75 website: nil
76 },
77 language: nil,
78 emojis: [
79 %{
80 shortcode: "2hu",
81 url: "corndog.png",
82 static_url: "corndog.png",
83 visible_in_picker: false
84 }
85 ]
86 }
87
88 assert status == expected
89 end
90
91 test "a reply" do
92 note = insert(:note_activity)
93 user = insert(:user)
94
95 {:ok, activity} =
96 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
97
98 status = StatusView.render("status.json", %{activity: activity})
99
100 assert status.in_reply_to_id == to_string(note.id)
101
102 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
103
104 assert status.in_reply_to_id == to_string(note.id)
105 end
106
107 test "contains mentions" do
108 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
109 # a user with this ap id might be in the cache.
110 recipient = "https://pleroma.soykaf.com/users/lain"
111 user = insert(:user, %{ap_id: recipient})
112
113 {:ok, [activity]} = OStatus.handle_incoming(incoming)
114
115 status = StatusView.render("status.json", %{activity: activity})
116
117 assert status.mentions == [AccountView.render("mention.json", %{user: user})]
118 end
119
120 test "attachments" do
121 object = %{
122 "type" => "Image",
123 "url" => [
124 %{
125 "mediaType" => "image/png",
126 "href" => "someurl"
127 }
128 ],
129 "uuid" => 6
130 }
131
132 expected = %{
133 id: "1638338801",
134 type: "image",
135 url: "someurl",
136 remote_url: "someurl",
137 preview_url: "someurl",
138 text_url: "someurl",
139 description: nil
140 }
141
142 assert expected == StatusView.render("attachment.json", %{attachment: object})
143
144 # If theres a "id", use that instead of the generated one
145 object = Map.put(object, "id", 2)
146 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
147 end
148
149 test "a reblog" do
150 user = insert(:user)
151 activity = insert(:note_activity)
152
153 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
154
155 represented = StatusView.render("status.json", %{for: user, activity: reblog})
156
157 assert represented[:id] == to_string(reblog.id)
158 assert represented[:reblog][:id] == to_string(activity.id)
159 assert represented[:emojis] == []
160 end
161
162 test "a peertube video" do
163 user = insert(:user)
164
165 {:ok, object} =
166 ActivityPub.fetch_object_from_id(
167 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
168 )
169
170 %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
171
172 represented = StatusView.render("status.json", %{for: user, activity: activity})
173
174 assert represented[:id] == to_string(activity.id)
175 assert length(represented[:media_attachments]) == 1
176 end
177
178 describe "build_tags/1" do
179 test "it returns a a dictionary tags" do
180 object_tags = [
181 "fediverse",
182 "mastodon",
183 "nextcloud",
184 %{
185 "href" => "https://kawen.space/users/lain",
186 "name" => "@lain@kawen.space",
187 "type" => "Mention"
188 }
189 ]
190
191 assert StatusView.build_tags(object_tags) == [
192 %{name: "fediverse", url: "/tag/fediverse"},
193 %{name: "mastodon", url: "/tag/mastodon"},
194 %{name: "nextcloud", url: "/tag/nextcloud"}
195 ]
196 end
197 end
198 end