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