filter_controller_test.exs 3.8 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

MK Fain's avatar
MK Fain committed
67
    # check whole_word false
68
69
70
71
    query = %Pleroma.Filter{
      user_id: user.id,
      filter_id: 2,
      phrase: "knight",
MK Fain's avatar
MK Fain committed
72
73
74
75
76
77
78
79
80
81
82
      context: ["home"],
      whole_word: false
    }

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

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

    assert response = json_response_and_validate_schema(conn, 200)
    assert response["whole_word"] == false

MK Fain's avatar
MK Fain committed
83
    # check whole_word true
MK Fain's avatar
MK Fain committed
84
85
86
87
88
89
90
91
    %{user: user, conn: conn} = oauth_access(["read:filters"])

    query = %Pleroma.Filter{
      user_id: user.id,
      filter_id: 3,
      phrase: "knight",
      context: ["home"],
      whole_word: true
92
93
94
95
    }

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

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

98
    assert response = json_response_and_validate_schema(conn, 200)
MK Fain's avatar
MK Fain committed
99
    assert response["whole_word"] == true
100
101
  end

102
103
  test "update a filter" do
    %{user: user, conn: conn} = oauth_access(["write:filters"])
104
105
106
107
108

    query = %Pleroma.Filter{
      user_id: user.id,
      filter_id: 2,
      phrase: "knight",
109
      context: ["home"],
MK Fain's avatar
MK Fain committed
110
111
      hide: true,
      whole_word: true
112
113
114
115
116
117
118
119
120
121
    }

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

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

    conn =
122
123
124
      conn
      |> put_req_header("content-type", "application/json")
      |> put("/api/v1/filters/#{query.filter_id}", %{
125
126
127
128
        phrase: new.phrase,
        context: new.context
      })

129
    assert response = json_response_and_validate_schema(conn, 200)
130
131
    assert response["phrase"] == new.phrase
    assert response["context"] == new.context
132
    assert response["irreversible"] == true
MK Fain's avatar
MK Fain committed
133
    assert response["whole_word"] == true
134
135
  end

136
137
  test "delete a filter" do
    %{user: user, conn: conn} = oauth_access(["write:filters"])
138
139
140
141
142
143
144
145
146
147

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

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

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

150
    assert json_response_and_validate_schema(conn, 200) == %{}
151
  end
152
end