First
[anni] / test / pleroma / web / mastodon_api / controllers / domain_block_controller_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.MastodonAPI.DomainBlockControllerTest do
6   # TODO: Should not need Cachex
7   use Pleroma.Web.ConnCase
8
9   alias Pleroma.User
10
11   import Pleroma.Factory
12
13   test "blocking / unblocking a domain" do
14     %{user: user, conn: conn} = oauth_access(["write:blocks"])
15     other_user = insert(:user, %{ap_id: "https://dogwhistle.zone/@pundit"})
16
17     ret_conn =
18       conn
19       |> put_req_header("content-type", "application/json")
20       |> post("/api/v1/domain_blocks", %{"domain" => "dogwhistle.zone"})
21
22     assert %{} == json_response_and_validate_schema(ret_conn, 200)
23     user = User.get_cached_by_ap_id(user.ap_id)
24     assert User.blocks?(user, other_user)
25
26     ret_conn =
27       conn
28       |> put_req_header("content-type", "application/json")
29       |> delete("/api/v1/domain_blocks", %{"domain" => "dogwhistle.zone"})
30
31     assert %{} == json_response_and_validate_schema(ret_conn, 200)
32     user = User.get_cached_by_ap_id(user.ap_id)
33     refute User.blocks?(user, other_user)
34   end
35
36   test "blocking a domain via query params" do
37     %{user: user, conn: conn} = oauth_access(["write:blocks"])
38     other_user = insert(:user, %{ap_id: "https://dogwhistle.zone/@pundit"})
39
40     ret_conn =
41       conn
42       |> put_req_header("content-type", "application/json")
43       |> post("/api/v1/domain_blocks?domain=dogwhistle.zone")
44
45     assert %{} == json_response_and_validate_schema(ret_conn, 200)
46     user = User.get_cached_by_ap_id(user.ap_id)
47     assert User.blocks?(user, other_user)
48   end
49
50   test "unblocking a domain via query params" do
51     %{user: user, conn: conn} = oauth_access(["write:blocks"])
52     other_user = insert(:user, %{ap_id: "https://dogwhistle.zone/@pundit"})
53
54     User.block_domain(user, "dogwhistle.zone")
55     user = refresh_record(user)
56     assert User.blocks?(user, other_user)
57
58     ret_conn =
59       conn
60       |> put_req_header("content-type", "application/json")
61       |> delete("/api/v1/domain_blocks?domain=dogwhistle.zone")
62
63     assert %{} == json_response_and_validate_schema(ret_conn, 200)
64     user = User.get_cached_by_ap_id(user.ap_id)
65     refute User.blocks?(user, other_user)
66   end
67
68   test "getting a list of domain blocks" do
69     %{user: user, conn: conn} = oauth_access(["read:blocks"])
70
71     {:ok, user} = User.block_domain(user, "bad.site")
72     {:ok, user} = User.block_domain(user, "even.worse.site")
73
74     assert ["even.worse.site", "bad.site"] ==
75              conn
76              |> assign(:user, user)
77              |> get("/api/v1/domain_blocks")
78              |> json_response_and_validate_schema(200)
79   end
80 end