0f6605c3f2e274a79ec3d629ab4d61210667dd45
[akkoma] / test / pleroma / web / activity_pub / transmogrifier / answer_handling_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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)
30
31 data =
32 File.read!("test/fixtures/mastodon-vote.json")
33 |> Poison.decode!()
34 |> Kernel.put_in(["to"], user.ap_id)
35 |> Kernel.put_in(["object", "inReplyTo"], object.data["id"])
36 |> Kernel.put_in(["object", "to"], user.ap_id)
37
38 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
39 answer_object = Object.normalize(activity)
40 assert answer_object.data["type"] == "Answer"
41 assert answer_object.data["inReplyTo"] == object.data["id"]
42
43 new_object = Object.get_by_ap_id(object.data["id"])
44 assert new_object.data["replies_count"] == object.data["replies_count"]
45
46 assert Enum.any?(
47 new_object.data["oneOf"],
48 fn
49 %{"name" => "suya..", "replies" => %{"totalItems" => 1}} -> true
50 _ -> false
51 end
52 )
53 end
54
55 test "outgoing, rewrites Answer to Note" do
56 user = insert(:user)
57
58 {:ok, poll_activity} =
59 CommonAPI.post(user, %{
60 status: "suya...",
61 poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10}
62 })
63
64 poll_object = Object.normalize(poll_activity)
65 # TODO: Replace with CommonAPI vote creation when implemented
66 data =
67 File.read!("test/fixtures/mastodon-vote.json")
68 |> Poison.decode!()
69 |> Kernel.put_in(["to"], user.ap_id)
70 |> Kernel.put_in(["object", "inReplyTo"], poll_object.data["id"])
71 |> Kernel.put_in(["object", "to"], user.ap_id)
72
73 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
74 {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
75
76 assert data["object"]["type"] == "Note"
77 end
78 end