Merge develop to refactor/mix-tasks
[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 = 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 application: %{
67 name: "Web",
68 website: nil
69 },
70 language: nil,
71 emojis: [
72 %{
73 shortcode: "2hu",
74 url: "corndog.png",
75 static_url: "corndog.png",
76 visible_in_picker: false
77 }
78 ]
79 }
80
81 assert status == expected
82 end
83
84 test "a reply" do
85 note = insert(:note_activity)
86 user = insert(:user)
87
88 {:ok, activity} =
89 CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id})
90
91 status = StatusView.render("status.json", %{activity: activity})
92
93 assert status.in_reply_to_id == to_string(note.id)
94
95 [status] = StatusView.render("index.json", %{activities: [activity], as: :activity})
96
97 assert status.in_reply_to_id == to_string(note.id)
98 end
99
100 test "contains mentions" do
101 incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
102 # a user with this ap id might be in the cache.
103 recipient = "https://pleroma.soykaf.com/users/lain"
104 user = insert(:user, %{ap_id: recipient})
105
106 {:ok, [activity]} = OStatus.handle_incoming(incoming)
107
108 status = StatusView.render("status.json", %{activity: activity})
109
110 assert status.mentions == [AccountView.render("mention.json", %{user: user})]
111 end
112
113 test "attachments" do
114 object = %{
115 "type" => "Image",
116 "url" => [
117 %{
118 "mediaType" => "image/png",
119 "href" => "someurl"
120 }
121 ],
122 "uuid" => 6
123 }
124
125 expected = %{
126 id: "1638338801",
127 type: "image",
128 url: "someurl",
129 remote_url: "someurl",
130 preview_url: "someurl",
131 text_url: "someurl",
132 description: nil
133 }
134
135 assert expected == StatusView.render("attachment.json", %{attachment: object})
136
137 # If theres a "id", use that instead of the generated one
138 object = Map.put(object, "id", 2)
139 assert %{id: "2"} = StatusView.render("attachment.json", %{attachment: object})
140 end
141
142 test "a reblog" do
143 user = insert(:user)
144 activity = insert(:note_activity)
145
146 {:ok, reblog, _} = CommonAPI.repeat(activity.id, user)
147
148 represented = StatusView.render("status.json", %{for: user, activity: reblog})
149
150 assert represented[:id] == to_string(reblog.id)
151 assert represented[:reblog][:id] == to_string(activity.id)
152 assert represented[:emojis] == []
153 end
154 end