Fix digest mix task on OTP releases
[akkoma] / test / web / mastodon_api / controllers / search_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.SearchControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Object
9 alias Pleroma.Web
10 alias Pleroma.Web.CommonAPI
11 import Pleroma.Factory
12 import ExUnit.CaptureLog
13 import Tesla.Mock
14 import Mock
15
16 setup do
17 mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 :ok
19 end
20
21 describe ".search2" do
22 test "it returns empty result if user or status search return undefined error", %{conn: conn} do
23 with_mocks [
24 {Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
25 {Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
26 ] do
27 capture_log(fn ->
28 results =
29 conn
30 |> get("/api/v2/search?q=2hu")
31 |> json_response_and_validate_schema(200)
32
33 assert results["accounts"] == []
34 assert results["statuses"] == []
35 end) =~
36 "[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
37 end
38 end
39
40 test "search", %{conn: conn} do
41 user = insert(:user)
42 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
43 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
44
45 {:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu private 天子"})
46
47 {:ok, _activity} =
48 CommonAPI.post(user, %{
49 status: "This is about 2hu, but private",
50 visibility: "private"
51 })
52
53 {:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})
54
55 results =
56 conn
57 |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu #private"})}")
58 |> json_response_and_validate_schema(200)
59
60 [account | _] = results["accounts"]
61 assert account["id"] == to_string(user_three.id)
62
63 assert results["hashtags"] == [
64 %{"name" => "private", "url" => "#{Web.base_url()}/tag/private"}
65 ]
66
67 [status] = results["statuses"]
68 assert status["id"] == to_string(activity.id)
69
70 results =
71 get(conn, "/api/v2/search?q=天子")
72 |> json_response_and_validate_schema(200)
73
74 [status] = results["statuses"]
75 assert status["id"] == to_string(activity.id)
76 end
77
78 test "excludes a blocked users from search results", %{conn: conn} do
79 user = insert(:user)
80 user_smith = insert(:user, %{nickname: "Agent", name: "I love 2hu"})
81 user_neo = insert(:user, %{nickname: "Agent Neo", name: "Agent"})
82
83 {:ok, act1} = CommonAPI.post(user, %{status: "This is about 2hu private 天子"})
84 {:ok, act2} = CommonAPI.post(user_smith, %{status: "Agent Smith"})
85 {:ok, act3} = CommonAPI.post(user_neo, %{status: "Agent Smith"})
86 Pleroma.User.block(user, user_smith)
87
88 results =
89 conn
90 |> assign(:user, user)
91 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read"]))
92 |> get("/api/v2/search?q=Agent")
93 |> json_response_and_validate_schema(200)
94
95 status_ids = Enum.map(results["statuses"], fn g -> g["id"] end)
96
97 assert act3.id in status_ids
98 refute act2.id in status_ids
99 refute act1.id in status_ids
100 end
101 end
102
103 describe ".account_search" do
104 test "account search", %{conn: conn} do
105 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
106 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
107
108 results =
109 conn
110 |> get("/api/v1/accounts/search?q=shp")
111 |> json_response_and_validate_schema(200)
112
113 result_ids = for result <- results, do: result["acct"]
114
115 assert user_two.nickname in result_ids
116 assert user_three.nickname in result_ids
117
118 results =
119 conn
120 |> get("/api/v1/accounts/search?q=2hu")
121 |> json_response_and_validate_schema(200)
122
123 result_ids = for result <- results, do: result["acct"]
124
125 assert user_three.nickname in result_ids
126 end
127
128 test "returns account if query contains a space", %{conn: conn} do
129 insert(:user, %{nickname: "shp@shitposter.club"})
130
131 results =
132 conn
133 |> get("/api/v1/accounts/search?q=shp@shitposter.club xxx")
134 |> json_response_and_validate_schema(200)
135
136 assert length(results) == 1
137 end
138 end
139
140 describe ".search" do
141 test "it returns empty result if user or status search return undefined error", %{conn: conn} do
142 with_mocks [
143 {Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
144 {Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
145 ] do
146 capture_log(fn ->
147 results =
148 conn
149 |> get("/api/v1/search?q=2hu")
150 |> json_response_and_validate_schema(200)
151
152 assert results["accounts"] == []
153 assert results["statuses"] == []
154 end) =~
155 "[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
156 end
157 end
158
159 test "search", %{conn: conn} do
160 user = insert(:user)
161 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
162 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
163
164 {:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu"})
165
166 {:ok, _activity} =
167 CommonAPI.post(user, %{
168 status: "This is about 2hu, but private",
169 visibility: "private"
170 })
171
172 {:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})
173
174 results =
175 conn
176 |> get("/api/v1/search?q=2hu")
177 |> json_response_and_validate_schema(200)
178
179 [account | _] = results["accounts"]
180 assert account["id"] == to_string(user_three.id)
181
182 assert results["hashtags"] == []
183
184 [status] = results["statuses"]
185 assert status["id"] == to_string(activity.id)
186 end
187
188 test "search fetches remote statuses and prefers them over other results", %{conn: conn} do
189 capture_log(fn ->
190 {:ok, %{id: activity_id}} =
191 CommonAPI.post(insert(:user), %{
192 status: "check out https://shitposter.club/notice/2827873"
193 })
194
195 results =
196 conn
197 |> get("/api/v1/search?q=https://shitposter.club/notice/2827873")
198 |> json_response_and_validate_schema(200)
199
200 [status, %{"id" => ^activity_id}] = results["statuses"]
201
202 assert status["uri"] ==
203 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
204 end)
205 end
206
207 test "search doesn't show statuses that it shouldn't", %{conn: conn} do
208 {:ok, activity} =
209 CommonAPI.post(insert(:user), %{
210 status: "This is about 2hu, but private",
211 visibility: "private"
212 })
213
214 capture_log(fn ->
215 q = Object.normalize(activity).data["id"]
216
217 results =
218 conn
219 |> get("/api/v1/search?q=#{q}")
220 |> json_response_and_validate_schema(200)
221
222 [] = results["statuses"]
223 end)
224 end
225
226 test "search fetches remote accounts", %{conn: conn} do
227 user = insert(:user)
228
229 results =
230 conn
231 |> assign(:user, user)
232 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read"]))
233 |> get("/api/v1/search?q=mike@osada.macgirvin.com&resolve=true")
234 |> json_response_and_validate_schema(200)
235
236 [account] = results["accounts"]
237 assert account["acct"] == "mike@osada.macgirvin.com"
238 end
239
240 test "search doesn't fetch remote accounts if resolve is false", %{conn: conn} do
241 results =
242 conn
243 |> get("/api/v1/search?q=mike@osada.macgirvin.com&resolve=false")
244 |> json_response_and_validate_schema(200)
245
246 assert [] == results["accounts"]
247 end
248
249 test "search with limit and offset", %{conn: conn} do
250 user = insert(:user)
251 _user_two = insert(:user, %{nickname: "shp@shitposter.club"})
252 _user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
253
254 {:ok, _activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
255 {:ok, _activity2} = CommonAPI.post(user, %{status: "This is also about 2hu"})
256
257 result =
258 conn
259 |> get("/api/v1/search?q=2hu&limit=1")
260
261 assert results = json_response_and_validate_schema(result, 200)
262 assert [%{"id" => activity_id1}] = results["statuses"]
263 assert [_] = results["accounts"]
264
265 results =
266 conn
267 |> get("/api/v1/search?q=2hu&limit=1&offset=1")
268 |> json_response_and_validate_schema(200)
269
270 assert [%{"id" => activity_id2}] = results["statuses"]
271 assert [] = results["accounts"]
272
273 assert activity_id1 != activity_id2
274 end
275
276 test "search returns results only for the given type", %{conn: conn} do
277 user = insert(:user)
278 _user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
279
280 {:ok, _activity} = CommonAPI.post(user, %{status: "This is about 2hu"})
281
282 assert %{"statuses" => [_activity], "accounts" => [], "hashtags" => []} =
283 conn
284 |> get("/api/v1/search?q=2hu&type=statuses")
285 |> json_response_and_validate_schema(200)
286
287 assert %{"statuses" => [], "accounts" => [_user_two], "hashtags" => []} =
288 conn
289 |> get("/api/v1/search?q=2hu&type=accounts")
290 |> json_response_and_validate_schema(200)
291 end
292
293 test "search uses account_id to filter statuses by the author", %{conn: conn} do
294 user = insert(:user, %{nickname: "shp@shitposter.club"})
295 user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
296
297 {:ok, activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
298 {:ok, activity2} = CommonAPI.post(user_two, %{status: "This is also about 2hu"})
299
300 results =
301 conn
302 |> get("/api/v1/search?q=2hu&account_id=#{user.id}")
303 |> json_response_and_validate_schema(200)
304
305 assert [%{"id" => activity_id1}] = results["statuses"]
306 assert activity_id1 == activity1.id
307 assert [_] = results["accounts"]
308
309 results =
310 conn
311 |> get("/api/v1/search?q=2hu&account_id=#{user_two.id}")
312 |> json_response_and_validate_schema(200)
313
314 assert [%{"id" => activity_id2}] = results["statuses"]
315 assert activity_id2 == activity2.id
316 end
317 end
318 end