First
[anni] / test / pleroma / web / activity_pub / transmogrifier / block_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.BlockHandlingTest do
6   use Pleroma.DataCase, async: true
7
8   alias Pleroma.Activity
9   alias Pleroma.User
10   alias Pleroma.Web.ActivityPub.Transmogrifier
11
12   import Pleroma.Factory
13
14   test "it works for incoming blocks" do
15     user = insert(:user)
16
17     data =
18       File.read!("test/fixtures/mastodon-block-activity.json")
19       |> Jason.decode!()
20       |> Map.put("object", user.ap_id)
21
22     blocker = insert(:user, ap_id: data["actor"])
23
24     {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
25
26     assert data["type"] == "Block"
27     assert data["object"] == user.ap_id
28     assert data["actor"] == "http://mastodon.example.org/users/admin"
29
30     assert User.blocks?(blocker, user)
31   end
32
33   test "incoming blocks successfully tear down any follow relationship" do
34     blocker = insert(:user)
35     blocked = insert(:user)
36
37     data =
38       File.read!("test/fixtures/mastodon-block-activity.json")
39       |> Jason.decode!()
40       |> Map.put("object", blocked.ap_id)
41       |> Map.put("actor", blocker.ap_id)
42
43     {:ok, blocker, blocked} = User.follow(blocker, blocked)
44     {:ok, blocked, blocker} = User.follow(blocked, blocker)
45
46     assert User.following?(blocker, blocked)
47     assert User.following?(blocked, blocker)
48
49     {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
50
51     assert data["type"] == "Block"
52     assert data["object"] == blocked.ap_id
53     assert data["actor"] == blocker.ap_id
54
55     blocker = User.get_cached_by_ap_id(data["actor"])
56     blocked = User.get_cached_by_ap_id(data["object"])
57
58     assert User.blocks?(blocker, blocked)
59
60     refute User.following?(blocker, blocked)
61     refute User.following?(blocked, blocker)
62   end
63 end