filter_controller_test.exs 3.19 KB
Newer Older
1
# Pleroma: A lightweight social networking server
2
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3
4
5
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do
minibikini's avatar
minibikini committed
6
  use Pleroma.Web.ConnCase
7
8
9

  alias Pleroma.Web.MastodonAPI.FilterView

10
11
  test "creating a filter" do
    %{conn: conn} = oauth_access(["write:filters"])
12
13
14
15
16
17

    filter = %Pleroma.Filter{
      phrase: "knights",
      context: ["home"]
    }

18
19
20
21
    conn =
      conn
      |> put_req_header("content-type", "application/json")
      |> post("/api/v1/filters", %{"phrase" => filter.phrase, context: filter.context})
22

23
    assert response = json_response_and_validate_schema(conn, 200)
24
25
26
27
28
29
30
    assert response["phrase"] == filter.phrase
    assert response["context"] == filter.context
    assert response["irreversible"] == false
    assert response["id"] != nil
    assert response["id"] != ""
  end

31
32
  test "fetching a list of filters" do
    %{user: user, conn: conn} = oauth_access(["read:filters"])
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

    query_one = %Pleroma.Filter{
      user_id: user.id,
      filter_id: 1,
      phrase: "knights",
      context: ["home"]
    }

    query_two = %Pleroma.Filter{
      user_id: user.id,
      filter_id: 2,
      phrase: "who",
      context: ["home"]
    }

    {:ok, filter_one} = Pleroma.Filter.create(query_one)
    {:ok, filter_two} = Pleroma.Filter.create(query_two)

    response =
      conn
      |> get("/api/v1/filters")
54
      |> json_response_and_validate_schema(200)
55
56
57
58

    assert response ==
             render_json(
               FilterView,
59
               "index.json",
60
61
62
63
               filters: [filter_two, filter_one]
             )
  end

64
65
  test "get a filter" do
    %{user: user, conn: conn} = oauth_access(["read:filters"])
66
67
68
69
70
71
72
73
74
75

    query = %Pleroma.Filter{
      user_id: user.id,
      filter_id: 2,
      phrase: "knight",
      context: ["home"]
    }

    {:ok, filter} = Pleroma.Filter.create(query)

76
    conn = get(conn, "/api/v1/filters/#{filter.filter_id}")
77

78
    assert response = json_response_and_validate_schema(conn, 200)
79
80
  end

81
82
  test "update a filter" do
    %{user: user, conn: conn} = oauth_access(["write:filters"])
83
84
85
86
87

    query = %Pleroma.Filter{
      user_id: user.id,
      filter_id: 2,
      phrase: "knight",
88
89
      context: ["home"],
      hide: true
90
91
92
93
94
95
96
97
98
99
    }

    {:ok, _filter} = Pleroma.Filter.create(query)

    new = %Pleroma.Filter{
      phrase: "nii",
      context: ["home"]
    }

    conn =
100
101
102
      conn
      |> put_req_header("content-type", "application/json")
      |> put("/api/v1/filters/#{query.filter_id}", %{
103
104
105
106
        phrase: new.phrase,
        context: new.context
      })

107
    assert response = json_response_and_validate_schema(conn, 200)
108
109
    assert response["phrase"] == new.phrase
    assert response["context"] == new.context
110
    assert response["irreversible"] == true
111
112
  end

113
114
  test "delete a filter" do
    %{user: user, conn: conn} = oauth_access(["write:filters"])
115
116
117
118
119
120
121
122
123
124

    query = %Pleroma.Filter{
      user_id: user.id,
      filter_id: 2,
      phrase: "knight",
      context: ["home"]
    }

    {:ok, filter} = Pleroma.Filter.create(query)

125
    conn = delete(conn, "/api/v1/filters/#{filter.filter_id}")
126

127
    assert json_response_and_validate_schema(conn, 200) == %{}
128
  end
129
end