add `pinned` property to `StatusView`
[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 pinned: false,
67 sensitive: false,
68 spoiler_text: note.data["object"]["summary"],
69 visibility: "public",
70 media_attachments: [],
71 mentions: [],
72 tags: [
73 %{
74 name: "#{note.data["object"]["tag"]}",
75 url: "/tag/#{note.data["object"]["tag"]}"
76 }
77 ],
78 application: %{
79 name: "Web",
80 website: nil
81 },
82 language: nil,
83 emojis: [
84 %{
85 shortcode: "2hu",
86 url: "corndog.png",
87 static_url: "corndog.png",
88 visible_in_picker: false
89 }
90 ]
91 }
92
93 assert status == expected
94 end
95
96 test "a reply" do
97 note = insert(:note_activity)
98 user = insert(:user)
99
100 {:ok, activity} =
101 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
102
103 status = StatusView.render("status.json", %{activity: activity})
104
105 assert status.in_reply_to_id == to_string(note.id)
106
107 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
108
109 assert status.in_reply_to_id == to_string(note.id)
110 end
111
112 test "contains mentions" do
113 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
114 # a user with this ap id might be in the cache.
115 recipient = "https://pleroma.soykaf.com/users/lain"
116 user = insert(:user, %{ap_id: recipient})
117
118 {:ok, [activity]} = OStatus.handle_incoming(incoming)
119
120 status = StatusView.render("status.json", %{activity: activity})
121
122 assert status.mentions == [AccountView.render("mention.json", %{user: user})]
123 end
124
125 test "attachments" do
126 object = %{
127 "type" => "Image",
128 "url" => [
129 %{
130 "mediaType" => "image/png",
131 "href" => "someurl"
132 }
133 ],
134 "uuid" => 6
135 }
136
137 expected = %{
138 id: "1638338801",
139 type: "image",
140 url: "someurl",
141 remote_url: "someurl",
142 preview_url: "someurl",
143 text_url: "someurl",
144 description: nil
145 }
146
147 assert expected == StatusView.render("attachment.json", %{attachment: object})
148
149 # If theres a "id", use that instead of the generated one
150 object = Map.put(object, "id", 2)
151 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
152 end
153
154 test "a reblog" do
155 user = insert(:user)
156 activity = insert(:note_activity)
157
158 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
159
160 represented = StatusView.render("status.json", %{for: user, activity: reblog})
161
162 assert represented[:id] == to_string(reblog.id)
163 assert represented[:reblog][:id] == to_string(activity.id)
164 assert represented[:emojis] == []
165 end
166
167 test "a peertube video" do
168 user = insert(:user)
169
170 {:ok, object} =
171 ActivityPub.fetch_object_from_id(
172 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
173 )
174
175 %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
176
177 represented = StatusView.render("status.json", %{for: user, activity: activity})
178
179 assert represented[:id] == to_string(activity.id)
180 assert length(represented[:media_attachments]) == 1
181 end
182
183 describe "build_tags/1" do
184 test "it returns a a dictionary tags" do
185 object_tags = [
186 "fediverse",
187 "mastodon",
188 "nextcloud",
189 %{
190 "href" => "https://kawen.space/users/lain",
191 "name" => "@lain@kawen.space",
192 "type" => "Mention"
193 }
194 ]
195
196 assert StatusView.build_tags(object_tags) == [
197 %{name: "fediverse", url: "/tag/fediverse"},
198 %{name: "mastodon", url: "/tag/mastodon"},
199 %{name: "nextcloud", url: "/tag/nextcloud"}
200 ]
201 end
202 end
203 end