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