summaryrefslogtreecommitdiff
path: root/test/web/mastodon_api/controllers/domain_block_controller_test.exs
blob: 66465450003c213770b0b7ffa583eeee019b9d75 (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
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.MastodonAPI.DomainBlockControllerTest do
  use Pleroma.Web.ConnCase

  alias Pleroma.User

  import Pleroma.Factory

  test "blocking / unblocking a domain" do
    %{user: user, conn: conn} = oauth_access(["write:blocks"])
    other_user = insert(:user, %{ap_id: "https://dogwhistle.zone/@pundit"})

    ret_conn =
      conn
      |> put_req_header("content-type", "application/json")
      |> post("/api/v1/domain_blocks", %{"domain" => "dogwhistle.zone"})

    assert %{} == json_response_and_validate_schema(ret_conn, 200)
    user = User.get_cached_by_ap_id(user.ap_id)
    assert User.blocks?(user, other_user)

    ret_conn =
      conn
      |> put_req_header("content-type", "application/json")
      |> delete("/api/v1/domain_blocks", %{"domain" => "dogwhistle.zone"})

    assert %{} == json_response_and_validate_schema(ret_conn, 200)
    user = User.get_cached_by_ap_id(user.ap_id)
    refute User.blocks?(user, other_user)
  end

  test "blocking a domain via query params" do
    %{user: user, conn: conn} = oauth_access(["write:blocks"])
    other_user = insert(:user, %{ap_id: "https://dogwhistle.zone/@pundit"})

    ret_conn =
      conn
      |> put_req_header("content-type", "application/json")
      |> post("/api/v1/domain_blocks?domain=dogwhistle.zone")

    assert %{} == json_response_and_validate_schema(ret_conn, 200)
    user = User.get_cached_by_ap_id(user.ap_id)
    assert User.blocks?(user, other_user)
  end

  test "unblocking a domain via query params" do
    %{user: user, conn: conn} = oauth_access(["write:blocks"])
    other_user = insert(:user, %{ap_id: "https://dogwhistle.zone/@pundit"})

    User.block_domain(user, "dogwhistle.zone")
    user = refresh_record(user)
    assert User.blocks?(user, other_user)

    ret_conn =
      conn
      |> put_req_header("content-type", "application/json")
      |> delete("/api/v1/domain_blocks?domain=dogwhistle.zone")

    assert %{} == json_response_and_validate_schema(ret_conn, 200)
    user = User.get_cached_by_ap_id(user.ap_id)
    refute User.blocks?(user, other_user)
  end

  test "getting a list of domain blocks" do
    %{user: user, conn: conn} = oauth_access(["read:blocks"])

    {:ok, user} = User.block_domain(user, "bad.site")
    {:ok, user} = User.block_domain(user, "even.worse.site")

    assert ["even.worse.site", "bad.site"] ==
             conn
             |> assign(:user, user)
             |> get("/api/v1/domain_blocks")
             |> json_response_and_validate_schema(200)
  end
end