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