summaryrefslogtreecommitdiff
path: root/test/web/activity_pub/transmogrifier/follow_handling_test.exs
blob: 967389fae465407cb372548a203eb2d1de270e28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.ActivityPub.Transmogrifier.FollowHandlingTest do
  use Pleroma.DataCase
  alias Pleroma.Activity
  alias Pleroma.Repo
  alias Pleroma.User
  alias Pleroma.Web.ActivityPub.Transmogrifier
  alias Pleroma.Web.ActivityPub.Utils

  import Pleroma.Factory
  import Ecto.Query

  setup_all do
    Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
    :ok
  end

  describe "handle_incoming" do
    setup do: clear_config([:user, :deny_follow_blocked])

    test "it works for osada follow request" do
      user = insert(:user)

      data =
        File.read!("test/fixtures/osada-follow-activity.json")
        |> Poison.decode!()
        |> Map.put("object", user.ap_id)

      {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)

      assert data["actor"] == "https://apfed.club/channel/indio"
      assert data["type"] == "Follow"
      assert data["id"] == "https://apfed.club/follow/9"

      activity = Repo.get(Activity, activity.id)
      assert activity.data["state"] == "accept"
      assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
    end

    test "it works for incoming follow requests" do
      user = insert(:user)

      data =
        File.read!("test/fixtures/mastodon-follow-activity.json")
        |> Poison.decode!()
        |> Map.put("object", user.ap_id)

      {:ok, %Activity{data: data, local: false} = activity} = Transmogrifier.handle_incoming(data)

      assert data["actor"] == "http://mastodon.example.org/users/admin"
      assert data["type"] == "Follow"
      assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"

      activity = Repo.get(Activity, activity.id)
      assert activity.data["state"] == "accept"
      assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
    end

    test "with locked accounts, it does not create a follow or an accept" do
      user = insert(:user, locked: true)

      data =
        File.read!("test/fixtures/mastodon-follow-activity.json")
        |> Poison.decode!()
        |> Map.put("object", user.ap_id)

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["state"] == "pending"

      refute User.following?(User.get_cached_by_ap_id(data["actor"]), user)

      accepts =
        from(
          a in Activity,
          where: fragment("?->>'type' = ?", a.data, "Accept")
        )
        |> Repo.all()

      assert Enum.empty?(accepts)
    end

    test "it works for follow requests when you are already followed, creating a new accept activity" do
      # This is important because the remote might have the wrong idea about the
      # current follow status. This can lead to instance A thinking that x@A is
      # followed by y@B, but B thinks they are not. In this case, the follow can
      # never go through again because it will never get an Accept.
      user = insert(:user)

      data =
        File.read!("test/fixtures/mastodon-follow-activity.json")
        |> Poison.decode!()
        |> Map.put("object", user.ap_id)

      {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)

      accepts =
        from(
          a in Activity,
          where: fragment("?->>'type' = ?", a.data, "Accept")
        )
        |> Repo.all()

      assert length(accepts) == 1

      data =
        File.read!("test/fixtures/mastodon-follow-activity.json")
        |> Poison.decode!()
        |> Map.put("id", String.replace(data["id"], "2", "3"))
        |> Map.put("object", user.ap_id)

      {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)

      accepts =
        from(
          a in Activity,
          where: fragment("?->>'type' = ?", a.data, "Accept")
        )
        |> Repo.all()

      assert length(accepts) == 2
    end

    test "it rejects incoming follow requests from blocked users when deny_follow_blocked is enabled" do
      Pleroma.Config.put([:user, :deny_follow_blocked], true)

      user = insert(:user)
      {:ok, target} = User.get_or_fetch("http://mastodon.example.org/users/admin")

      {:ok, _user_relationship} = User.block(user, target)

      data =
        File.read!("test/fixtures/mastodon-follow-activity.json")
        |> Poison.decode!()
        |> Map.put("object", user.ap_id)

      {:ok, %Activity{data: %{"id" => id}}} = Transmogrifier.handle_incoming(data)

      %Activity{} = activity = Activity.get_by_ap_id(id)

      assert activity.data["state"] == "reject"
    end

    test "it works for incoming follow requests from hubzilla" do
      user = insert(:user)

      data =
        File.read!("test/fixtures/hubzilla-follow-activity.json")
        |> Poison.decode!()
        |> Map.put("object", user.ap_id)
        |> Utils.normalize_params()

      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)

      assert data["actor"] == "https://hubzilla.example.org/channel/kaniini"
      assert data["type"] == "Follow"
      assert data["id"] == "https://hubzilla.example.org/channel/kaniini#follows/2"
      assert User.following?(User.get_cached_by_ap_id(data["actor"]), user)
    end
  end
end