41a290eb226f982b7217cc49ae11a2098d55d516
[akkoma] / test / web / mastodon_api / controllers / filter_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Web.ApiSpec
9 alias Pleroma.Web.ApiSpec.Schemas.Filter
10 alias Pleroma.Web.ApiSpec.Schemas.FilterCreateRequest
11 alias Pleroma.Web.ApiSpec.Schemas.FiltersResponse
12 alias Pleroma.Web.ApiSpec.Schemas.FilterUpdateRequest
13 alias Pleroma.Web.MastodonAPI.FilterView
14
15 import OpenApiSpex.TestAssertions
16
17 test "creating a filter" do
18 %{conn: conn} = oauth_access(["write:filters"])
19
20 filter = %Pleroma.Filter{
21 phrase: "knights",
22 context: ["home"]
23 }
24
25 conn =
26 conn
27 |> put_req_header("content-type", "application/json")
28 |> post("/api/v1/filters", %{"phrase" => filter.phrase, context: filter.context})
29
30 assert response = json_response(conn, 200)
31 assert response["phrase"] == filter.phrase
32 assert response["context"] == filter.context
33 assert response["irreversible"] == false
34 assert response["id"] != nil
35 assert response["id"] != ""
36 assert_schema(response, "Filter", ApiSpec.spec())
37 end
38
39 test "fetching a list of filters" do
40 %{user: user, conn: conn} = oauth_access(["read:filters"])
41
42 query_one = %Pleroma.Filter{
43 user_id: user.id,
44 filter_id: 1,
45 phrase: "knights",
46 context: ["home"]
47 }
48
49 query_two = %Pleroma.Filter{
50 user_id: user.id,
51 filter_id: 2,
52 phrase: "who",
53 context: ["home"]
54 }
55
56 {:ok, filter_one} = Pleroma.Filter.create(query_one)
57 {:ok, filter_two} = Pleroma.Filter.create(query_two)
58
59 response =
60 conn
61 |> get("/api/v1/filters")
62 |> json_response(200)
63
64 assert response ==
65 render_json(
66 FilterView,
67 "index.json",
68 filters: [filter_two, filter_one]
69 )
70
71 assert_schema(response, "FiltersResponse", ApiSpec.spec())
72 end
73
74 test "get a filter" do
75 %{user: user, conn: conn} = oauth_access(["read:filters"])
76
77 query = %Pleroma.Filter{
78 user_id: user.id,
79 filter_id: 2,
80 phrase: "knight",
81 context: ["home"]
82 }
83
84 {:ok, filter} = Pleroma.Filter.create(query)
85
86 conn = get(conn, "/api/v1/filters/#{filter.filter_id}")
87
88 assert response = json_response(conn, 200)
89 assert_schema(response, "Filter", ApiSpec.spec())
90 end
91
92 test "update a filter" do
93 %{user: user, conn: conn} = oauth_access(["write:filters"])
94
95 query = %Pleroma.Filter{
96 user_id: user.id,
97 filter_id: 2,
98 phrase: "knight",
99 context: ["home"],
100 hide: true
101 }
102
103 {:ok, _filter} = Pleroma.Filter.create(query)
104
105 new = %Pleroma.Filter{
106 phrase: "nii",
107 context: ["home"]
108 }
109
110 conn =
111 conn
112 |> put_req_header("content-type", "application/json")
113 |> put("/api/v1/filters/#{query.filter_id}", %{
114 phrase: new.phrase,
115 context: new.context
116 })
117
118 assert response = json_response(conn, 200)
119 assert response["phrase"] == new.phrase
120 assert response["context"] == new.context
121 assert response["irreversible"] == true
122 assert_schema(response, "Filter", ApiSpec.spec())
123 end
124
125 test "delete a filter" do
126 %{user: user, conn: conn} = oauth_access(["write:filters"])
127
128 query = %Pleroma.Filter{
129 user_id: user.id,
130 filter_id: 2,
131 phrase: "knight",
132 context: ["home"]
133 }
134
135 {:ok, filter} = Pleroma.Filter.create(query)
136
137 conn = delete(conn, "/api/v1/filters/#{filter.filter_id}")
138
139 assert response = json_response(conn, 200)
140 assert response == %{}
141 end
142
143 describe "OpenAPI" do
144 test "Filter example matches schema" do
145 api_spec = ApiSpec.spec()
146 schema = Filter.schema()
147 assert_schema(schema.example, "Filter", api_spec)
148 end
149
150 test "FiltersResponse example matches schema" do
151 api_spec = ApiSpec.spec()
152 schema = FiltersResponse.schema()
153 assert_schema(schema.example, "FiltersResponse", api_spec)
154 end
155
156 test "FilterCreateRequest example matches schema" do
157 api_spec = ApiSpec.spec()
158 schema = FilterCreateRequest.schema()
159 assert_schema(schema.example, "FilterCreateRequest", api_spec)
160 end
161
162 test "FilterUpdateRequest example matches schema" do
163 api_spec = ApiSpec.spec()
164 schema = FilterUpdateRequest.schema()
165 assert_schema(schema.example, "FilterUpdateRequest", api_spec)
166 end
167 end
168 end