filter_controller_test.exs 3.03 KB
Newer Older
1
2
3
4
5
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

  alias Pleroma.Web.MastodonAPI.FilterView

  import Pleroma.Factory

  test "creating a filter", %{conn: conn} do
    user = insert(:user)

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

    conn =
      conn
      |> assign(:user, user)
      |> post("/api/v1/filters", %{"phrase" => filter.phrase, context: filter.context})

    assert response = json_response(conn, 200)
    assert response["phrase"] == filter.phrase
    assert response["context"] == filter.context
    assert response["irreversible"] == false
    assert response["id"] != nil
    assert response["id"] != ""
  end

  test "fetching a list of filters", %{conn: conn} do
    user = insert(:user)

    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
      |> assign(:user, user)
      |> get("/api/v1/filters")
      |> json_response(200)

    assert response ==
             render_json(
               FilterView,
               "filters.json",
               filters: [filter_two, filter_one]
             )
  end

  test "get a filter", %{conn: conn} do
    user = insert(:user)

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

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

    conn =
      conn
      |> assign(:user, user)
      |> get("/api/v1/filters/#{filter.filter_id}")

    assert _response = json_response(conn, 200)
  end

  test "update a filter", %{conn: conn} do
    user = insert(:user)

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

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

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

    conn =
      conn
      |> assign(:user, user)
      |> put("/api/v1/filters/#{query.filter_id}", %{
        phrase: new.phrase,
        context: new.context
      })

    assert response = json_response(conn, 200)
    assert response["phrase"] == new.phrase
    assert response["context"] == new.context
  end

  test "delete a filter", %{conn: conn} do
    user = insert(:user)

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

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

    conn =
      conn
      |> assign(:user, user)
      |> delete("/api/v1/filters/#{filter.filter_id}")

    assert response = json_response(conn, 200)
    assert response == %{}
  end
end