39a1598a8eac7b592b52d5ccce2c821201552388
[anni] / test / pleroma / web / activity_pub / transmogrifier / answer_handling_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.Transmogrifier.AnswerHandlingTest do
6   use Pleroma.DataCase
7
8   alias Pleroma.Activity
9   alias Pleroma.Object
10   alias Pleroma.Web.ActivityPub.Transmogrifier
11   alias Pleroma.Web.CommonAPI
12
13   import Pleroma.Factory
14
15   setup_all do
16     Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17     :ok
18   end
19
20   test "incoming, rewrites Note to Answer and increments vote counters" do
21     user = insert(:user)
22
23     {:ok, activity} =
24       CommonAPI.post(user, %{
25         status: "suya...",
26         poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10}
27       })
28
29     object = Object.normalize(activity, fetch: false)
30     assert object.data["repliesCount"] == nil
31
32     data =
33       File.read!("test/fixtures/mastodon-vote.json")
34       |> Jason.decode!()
35       |> Kernel.put_in(["to"], user.ap_id)
36       |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
37       |> Kernel.put_in(["object", "to"], user.ap_id)
38
39     {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
40     answer_object = Object.normalize(activity, fetch: false)
41     assert answer_object.data["type"] == "Answer"
42     assert answer_object.data["inReplyTo"] == object.data["id"]
43
44     new_object = Object.get_by_ap_id(object.data["id"])
45     assert new_object.data["repliesCount"] == nil
46
47     assert Enum.any?(
48              new_object.data["oneOf"],
49              fn
50                %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
51                _ -> false
52              end
53            )
54   end
55
56   test "outgoing, rewrites Answer to Note" do
57     user = insert(:user)
58
59     {:ok, poll_activity} =
60       CommonAPI.post(user, %{
61         status: "suya...",
62         poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10}
63       })
64
65     poll_object = Object.normalize(poll_activity, fetch: false)
66     # TODO: Replace with CommonAPI vote creation when implemented
67     data =
68       File.read!("test/fixtures/mastodon-vote.json")
69       |> Jason.decode!()
70       |> Kernel.put_in(["to"], user.ap_id)
71       |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
72       |> Kernel.put_in(["object", "to"], user.ap_id)
73
74     {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
75     {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
76
77     assert data["object"]["type"] == "Note"
78   end
79 end