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