Merge branch 'develop' into 'remove-avatar-header'
[akkoma] / test / web / activity_pub / transmogrifier / follow_handling_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier.FollowHandlingTest do
6 use Pleroma.DataCase
7 alias Pleroma.Activity
8 alias Pleroma.Repo
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 alias Pleroma.Web.ActivityPub.Utils
12
13 import Pleroma.Factory
14 import Ecto.Query
15
16 setup_all do
17 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 :ok
19 end
20
21 describe "handle_incoming" do
22 test "it works for incoming follow requests" do
23 user = insert(:user)
24
25 data =
26 File.read!("test/fixtures/mastodon-follow-activity.json")
27 |> Poison.decode!()
28 |> Map.put("object", user.ap_id)
29
30 {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)
31
32 assert data["actor"] == "http://mastodon.example.org/users/admin"
33 assert data["type"] == "Follow"
34 assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
35
36 activity = Repo.get(Activity, activity.id)
37 assert activity.data["state"] == "accept"
38 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
39 end
40
41 test "with locked accounts, it does not create a follow or an accept" do
42 user = insert(:user, info: %{locked: true})
43
44 data =
45 File.read!("test/fixtures/mastodon-follow-activity.json")
46 |> Poison.decode!()
47 |> Map.put("object", user.ap_id)
48
49 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
50
51 assert data["state"] == "pending"
52
53 refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)
54
55 accepts =
56 from(
57 a in Activity,
58 where: fragment("?->>'type' = ?", a.data, "Accept")
59 )
60 |> Repo.all()
61
62 assert length(accepts) == 0
63 end
64
65 test "it works for follow requests when you are already followed, creating a new accept activity" do
66 # This is important because the remote might have the wrong idea about the
67 # current follow status. This can lead to instance A thinking that x@A is
68 # followed by y@B, but B thinks they are not. In this case, the follow can
69 # never go through again because it will never get an Accept.
70 user = insert(:user)
71
72 data =
73 File.read!("test/fixtures/mastodon-follow-activity.json")
74 |> Poison.decode!()
75 |> Map.put("object", user.ap_id)
76
77 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
78
79 accepts =
80 from(
81 a in Activity,
82 where: fragment("?->>'type' = ?", a.data, "Accept")
83 )
84 |> Repo.all()
85
86 assert length(accepts) == 1
87
88 data =
89 File.read!("test/fixtures/mastodon-follow-activity.json")
90 |> Poison.decode!()
91 |> Map.put("id", String.replace(data["id"], "2", "3"))
92 |> Map.put("object", user.ap_id)
93
94 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
95
96 accepts =
97 from(
98 a in Activity,
99 where: fragment("?->>'type' = ?", a.data, "Accept")
100 )
101 |> Repo.all()
102
103 assert length(accepts) == 2
104 end
105
106 test "it rejects incoming follow requests from blocked users when deny_follow_blocked is enabled" do
107 Pleroma.Config.put([:user, :deny_follow_blocked], true)
108
109 user = insert(:user)
110 {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")
111
112 {:ok, user} = User.block(user, target)
113
114 data =
115 File.read!("test/fixtures/mastodon-follow-activity.json")
116 |> Poison.decode!()
117 |> Map.put("object", user.ap_id)
118
119 {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)
120
121 %Activity{} = activity = Activity.get_by_ap_id(id)
122
123 assert activity.data["state"] == "reject"
124 end
125
126 test "it works for incoming follow requests from hubzilla" do
127 user = insert(:user)
128
129 data =
130 File.read!("test/fixtures/hubzilla-follow-activity.json")
131 |> Poison.decode!()
132 |> Map.put("object", user.ap_id)
133 |> Utils.normalize_params()
134
135 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
136
137 assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
138 assert data["type"] == "Follow"
139 assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
140 assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
141 end
142 end
143 end