First
[anni] / test / pleroma / web / activity_pub / transmogrifier / accept_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.AcceptHandlingTest do
6   use Pleroma.DataCase, async: true
7
8   alias Pleroma.User
9   alias Pleroma.Web.ActivityPub.Transmogrifier
10   alias Pleroma.Web.CommonAPI
11
12   import Pleroma.Factory
13
14   test "it works for incoming accepts which were pre-accepted" do
15     follower = insert(:user)
16     followed = insert(:user)
17
18     {:ok, follower, followed} = User.follow(follower, followed)
19     assert User.following?(follower, followed) == true
20
21     {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
22
23     accept_data =
24       File.read!("test/fixtures/mastodon-accept-activity.json")
25       |> Jason.decode!()
26       |> Map.put("actor", followed.ap_id)
27
28     object =
29       accept_data["object"]
30       |> Map.put("actor", follower.ap_id)
31       |> Map.put("id", follow_activity.data["id"])
32
33     accept_data = Map.put(accept_data, "object", object)
34
35     {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
36     refute activity.local
37
38     assert activity.data["object"] == follow_activity.data["id"]
39
40     assert activity.data["id"] == accept_data["id"]
41
42     follower = User.get_cached_by_id(follower.id)
43
44     assert User.following?(follower, followed) == true
45   end
46
47   test "it works for incoming accepts which are referenced by IRI only" do
48     follower = insert(:user)
49     followed = insert(:user, is_locked: true)
50
51     {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
52
53     accept_data =
54       File.read!("test/fixtures/mastodon-accept-activity.json")
55       |> Jason.decode!()
56       |> Map.put("actor", followed.ap_id)
57       |> Map.put("object", follow_activity.data["id"])
58
59     {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
60     assert activity.data["object"] == follow_activity.data["id"]
61
62     follower = User.get_cached_by_id(follower.id)
63
64     assert User.following?(follower, followed) == true
65
66     follower = User.get_by_id(follower.id)
67     assert follower.following_count == 1
68
69     followed = User.get_by_id(followed.id)
70     assert followed.follower_count == 1
71   end
72
73   test "it fails for incoming accepts which cannot be correlated" do
74     follower = insert(:user)
75     followed = insert(:user, is_locked: true)
76
77     accept_data =
78       File.read!("test/fixtures/mastodon-accept-activity.json")
79       |> Jason.decode!()
80       |> Map.put("actor", followed.ap_id)
81
82     accept_data =
83       Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
84
85     {:error, _} = Transmogrifier.handle_incoming(accept_data)
86
87     follower = User.get_cached_by_id(follower.id)
88
89     refute User.following?(follower, followed) == true
90   end
91 end