d94878e31dfedd65414f73c9f453b84f77af946f
[anni] / test / pleroma / web / activity_pub / views / object_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectViewTest do
6   use Pleroma.DataCase
7   import Pleroma.Factory
8
9   alias Pleroma.Object
10   alias Pleroma.Web.ActivityPub.ObjectView
11   alias Pleroma.Web.CommonAPI
12
13   test "renders a note object" do
14     note = insert(:note)
15
16     result = ObjectView.render("object.json", %{object: note})
17
18     assert result["id"] == note.data["id"]
19     assert result["to"] == note.data["to"]
20     assert result["content"] == note.data["content"]
21     assert result["type"] == "Note"
22     assert result["@context"]
23   end
24
25   test "renders a note activity" do
26     note = insert(:note_activity)
27     object = Object.normalize(note, fetch: false)
28
29     result = ObjectView.render("object.json", %{object: note})
30
31     assert result["id"] == note.data["id"]
32     assert result["to"] == note.data["to"]
33     assert result["object"]["type"] == "Note"
34     assert result["object"]["content"] == object.data["content"]
35     assert result["type"] == "Create"
36     assert result["@context"]
37   end
38
39   describe "note activity's `replies` collection rendering" do
40     setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
41
42     test "renders `replies` collection for a note activity" do
43       user = insert(:user)
44       activity = insert(:note_activity, user: user)
45
46       {:ok, self_reply1} =
47         CommonAPI.post(user, %{status: "self-reply 1", in_reply_to_status_id: activity.id})
48
49       replies_uris = [self_reply1.object.data["id"]]
50       result = ObjectView.render("object.json", %{object: refresh_record(activity)})
51
52       assert %{"type" => "Collection", "items" => ^replies_uris} =
53                get_in(result, ["object", "replies"])
54     end
55   end
56
57   test "renders a like activity" do
58     note = insert(:note_activity)
59     object = Object.normalize(note, fetch: false)
60     user = insert(:user)
61
62     {:ok, like_activity} = CommonAPI.favorite(user, note.id)
63
64     result = ObjectView.render("object.json", %{object: like_activity})
65
66     assert result["id"] == like_activity.data["id"]
67     assert result["object"] == object.data["id"]
68     assert result["type"] == "Like"
69   end
70
71   test "renders an announce activity" do
72     note = insert(:note_activity)
73     object = Object.normalize(note, fetch: false)
74     user = insert(:user)
75
76     {:ok, announce_activity} = CommonAPI.repeat(note.id, user)
77
78     result = ObjectView.render("object.json", %{object: announce_activity})
79
80     assert result["id"] == announce_activity.data["id"]
81     assert result["object"] == object.data["id"]
82     assert result["type"] == "Announce"
83   end
84
85   test "renders an undo announce activity" do
86     note = insert(:note_activity)
87     user = insert(:user)
88
89     {:ok, announce} = CommonAPI.repeat(note.id, user)
90     {:ok, undo} = CommonAPI.unrepeat(note.id, user)
91
92     result = ObjectView.render("object.json", %{object: undo})
93
94     assert result["id"] == undo.data["id"]
95     assert result["object"] == announce.data["id"]
96     assert result["type"] == "Undo"
97   end
98 end