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