Transmogrifier Tests: Extract Accept handling
[akkoma] / test / web / activity_pub / transmogrifier / accept_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.AcceptHandlingTest do
6 use Pleroma.DataCase
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} = 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 |> Poison.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 were orphaned" do
48 follower = insert(:user)
49 followed = insert(:user, 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 |> Poison.decode!()
56 |> Map.put("actor", followed.ap_id)
57
58 accept_data =
59 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
60
61 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
62 assert activity.data["object"] == follow_activity.data["id"]
63
64 follower = User.get_cached_by_id(follower.id)
65
66 assert User.following?(follower, followed) == true
67 end
68
69 test "it works for incoming accepts which are referenced by IRI only" do
70 follower = insert(:user)
71 followed = insert(:user, locked: true)
72
73 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
74
75 accept_data =
76 File.read!("test/fixtures/mastodon-accept-activity.json")
77 |> Poison.decode!()
78 |> Map.put("actor", followed.ap_id)
79 |> Map.put("object", follow_activity.data["id"])
80
81 {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
82 assert activity.data["object"] == follow_activity.data["id"]
83
84 follower = User.get_cached_by_id(follower.id)
85
86 assert User.following?(follower, followed) == true
87
88 follower = User.get_by_id(follower.id)
89 assert follower.following_count == 1
90
91 followed = User.get_by_id(followed.id)
92 assert followed.follower_count == 1
93 end
94
95 test "it fails for incoming accepts which cannot be correlated" do
96 follower = insert(:user)
97 followed = insert(:user, locked: true)
98
99 accept_data =
100 File.read!("test/fixtures/mastodon-accept-activity.json")
101 |> Poison.decode!()
102 |> Map.put("actor", followed.ap_id)
103
104 accept_data =
105 Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
106
107 :error = Transmogrifier.handle_incoming(accept_data)
108
109 follower = User.get_cached_by_id(follower.id)
110
111 refute User.following?(follower, followed) == true
112 end
113 end