1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do
6 use Pleroma.Web.ConnCase
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.Endpoint
11 import Pleroma.Factory
12 import ExUnit.CaptureLog
17 mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 describe ".search2" do
22 test "it returns empty result if user or status search return undefined error", %{conn: conn} do
24 {Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
25 {Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
30 |> get("/api/v2/search?q=2hu")
31 |> json_response_and_validate_schema(200)
33 assert results["accounts"] == []
34 assert results["statuses"] == []
36 "[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
41 test "search", %{conn: conn} do
43 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
44 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
46 {:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu private 天子"})
49 CommonAPI.post(user, %{
50 status: "This is about 2hu, but private",
54 {:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})
58 |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu #private"})}")
59 |> json_response_and_validate_schema(200)
61 [account | _] = results["accounts"]
62 assert account["id"] == to_string(user_three.id)
64 assert results["hashtags"] == [
65 %{"name" => "private", "url" => "#{Endpoint.url()}/tag/private"}
68 [status] = results["statuses"]
69 assert status["id"] == to_string(activity.id)
72 get(conn, "/api/v2/search?q=天子")
73 |> json_response_and_validate_schema(200)
75 assert results["hashtags"] == [
76 %{"name" => "天子", "url" => "#{Endpoint.url()}/tag/天子"}
79 [status] = results["statuses"]
80 assert status["id"] == to_string(activity.id)
83 test "search local-only status as an authenticated user" do
85 %{conn: conn} = oauth_access(["read:search"])
88 CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})
92 |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
93 |> json_response_and_validate_schema(200)
95 [status] = results["statuses"]
96 assert status["id"] == to_string(activity.id)
99 test "search local-only status as an unauthenticated user" do
101 %{conn: conn} = oauth_access([])
104 CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})
108 |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
109 |> json_response_and_validate_schema(200)
111 assert [] = results["statuses"]
114 test "search local-only status as an anonymous user" do
118 CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})
122 |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
123 |> json_response_and_validate_schema(200)
125 assert [] = results["statuses"]
128 @tag capture_log: true
129 test "constructs hashtags from search query", %{conn: conn} do
132 |> get("/api/v2/search?#{URI.encode_query(%{q: "some text with #explicit #hashtags"})}")
133 |> json_response_and_validate_schema(200)
135 assert results["hashtags"] == [
136 %{"name" => "explicit", "url" => "#{Endpoint.url()}/tag/explicit"},
137 %{"name" => "hashtags", "url" => "#{Endpoint.url()}/tag/hashtags"}
142 |> get("/api/v2/search?#{URI.encode_query(%{q: "john doe JOHN DOE"})}")
143 |> json_response_and_validate_schema(200)
145 assert results["hashtags"] == [
146 %{"name" => "john", "url" => "#{Endpoint.url()}/tag/john"},
147 %{"name" => "doe", "url" => "#{Endpoint.url()}/tag/doe"},
148 %{"name" => "JohnDoe", "url" => "#{Endpoint.url()}/tag/JohnDoe"}
153 |> get("/api/v2/search?#{URI.encode_query(%{q: "accident-prone"})}")
154 |> json_response_and_validate_schema(200)
156 assert results["hashtags"] == [
157 %{"name" => "accident", "url" => "#{Endpoint.url()}/tag/accident"},
158 %{"name" => "prone", "url" => "#{Endpoint.url()}/tag/prone"},
159 %{"name" => "AccidentProne", "url" => "#{Endpoint.url()}/tag/AccidentProne"}
164 |> get("/api/v2/search?#{URI.encode_query(%{q: "https://shpposter.club/users/shpuld"})}")
165 |> json_response_and_validate_schema(200)
167 assert results["hashtags"] == [
168 %{"name" => "shpuld", "url" => "#{Endpoint.url()}/tag/shpuld"}
174 "/api/v2/search?#{URI.encode_query(%{q: "https://www.washingtonpost.com/sports/2020/06/10/" <> "nascar-ban-display-confederate-flag-all-events-properties/"})}"
176 |> json_response_and_validate_schema(200)
178 assert results["hashtags"] == [
179 %{"name" => "nascar", "url" => "#{Endpoint.url()}/tag/nascar"},
180 %{"name" => "ban", "url" => "#{Endpoint.url()}/tag/ban"},
181 %{"name" => "display", "url" => "#{Endpoint.url()}/tag/display"},
182 %{"name" => "confederate", "url" => "#{Endpoint.url()}/tag/confederate"},
183 %{"name" => "flag", "url" => "#{Endpoint.url()}/tag/flag"},
184 %{"name" => "all", "url" => "#{Endpoint.url()}/tag/all"},
185 %{"name" => "events", "url" => "#{Endpoint.url()}/tag/events"},
186 %{"name" => "properties", "url" => "#{Endpoint.url()}/tag/properties"},
188 "name" => "NascarBanDisplayConfederateFlagAllEventsProperties",
190 "#{Endpoint.url()}/tag/NascarBanDisplayConfederateFlagAllEventsProperties"
195 test "supports pagination of hashtags search results", %{conn: conn} do
199 "/api/v2/search?#{URI.encode_query(%{q: "#some #text #with #hashtags", limit: 2, offset: 1})}"
201 |> json_response_and_validate_schema(200)
203 assert results["hashtags"] == [
204 %{"name" => "text", "url" => "#{Endpoint.url()}/tag/text"},
205 %{"name" => "with", "url" => "#{Endpoint.url()}/tag/with"}
209 test "excludes a blocked users from search results", %{conn: conn} do
211 user_smith = insert(:user, %{nickname: "Agent", name: "I love 2hu"})
212 user_neo = insert(:user, %{nickname: "Agent Neo", name: "Agent"})
214 {:ok, act1} = CommonAPI.post(user, %{status: "This is about 2hu private 天子"})
215 {:ok, act2} = CommonAPI.post(user_smith, %{status: "Agent Smith"})
216 {:ok, act3} = CommonAPI.post(user_neo, %{status: "Agent Smith"})
217 Pleroma.User.block(user, user_smith)
221 |> assign(:user, user)
222 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read"]))
223 |> get("/api/v2/search?q=Agent")
224 |> json_response_and_validate_schema(200)
226 status_ids = Enum.map(results["statuses"], fn g -> g["id"] end)
228 assert act3.id in status_ids
229 refute act2.id in status_ids
230 refute act1.id in status_ids
234 describe ".account_search" do
235 test "account search", %{conn: conn} do
236 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
237 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
241 |> get("/api/v1/accounts/search?q=shp")
242 |> json_response_and_validate_schema(200)
244 result_ids = for result <- results, do: result["acct"]
246 assert user_two.nickname in result_ids
247 assert user_three.nickname in result_ids
251 |> get("/api/v1/accounts/search?q=2hu")
252 |> json_response_and_validate_schema(200)
254 result_ids = for result <- results, do: result["acct"]
256 assert user_three.nickname in result_ids
259 test "returns account if query contains a space", %{conn: conn} do
260 insert(:user, %{nickname: "shp@shitposter.club"})
264 |> get("/api/v1/accounts/search?q=shp@shitposter.club xxx")
265 |> json_response_and_validate_schema(200)
267 assert length(results) == 1
271 describe ".search" do
272 test "it returns empty result if user or status search return undefined error", %{conn: conn} do
274 {Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
275 {Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
280 |> get("/api/v1/search?q=2hu")
281 |> json_response_and_validate_schema(200)
283 assert results["accounts"] == []
284 assert results["statuses"] == []
286 "[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
290 test "search", %{conn: conn} do
292 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
293 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
295 {:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu"})
298 CommonAPI.post(user, %{
299 status: "This is about 2hu, but private",
300 visibility: "private"
303 {:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})
307 |> get("/api/v1/search?q=2hu")
308 |> json_response_and_validate_schema(200)
310 [account | _] = results["accounts"]
311 assert account["id"] == to_string(user_three.id)
313 assert results["hashtags"] == ["2hu"]
315 [status] = results["statuses"]
316 assert status["id"] == to_string(activity.id)
319 test "search fetches remote statuses and prefers them over other results", %{conn: conn} do
320 old_version = :persistent_term.get({Pleroma.Repo, :postgres_version})
321 :persistent_term.put({Pleroma.Repo, :postgres_version}, 10.0)
322 on_exit(fn -> :persistent_term.put({Pleroma.Repo, :postgres_version}, old_version) end)
325 {:ok, %{id: activity_id}} =
326 CommonAPI.post(insert(:user), %{
327 status: "check out http://mastodon.example.org/@admin/99541947525187367"
332 |> get("/api/v1/search?q=http://mastodon.example.org/@admin/99541947525187367")
333 |> json_response_and_validate_schema(200)
336 %{"url" => "http://mastodon.example.org/@admin/99541947525187367"},
337 %{"id" => ^activity_id}
338 ] = results["statuses"]
342 test "search doesn't show statuses that it shouldn't", %{conn: conn} do
344 CommonAPI.post(insert(:user), %{
345 status: "This is about 2hu, but private",
346 visibility: "private"
350 q = Object.normalize(activity, fetch: false).data["id"]
354 |> get("/api/v1/search?q=#{q}")
355 |> json_response_and_validate_schema(200)
357 [] = results["statuses"]
361 test "search fetches remote accounts", %{conn: conn} do
364 query = URI.encode_query(%{q: " mike@osada.macgirvin.com ", resolve: true})
368 |> assign(:user, user)
369 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read"]))
370 |> get("/api/v1/search?#{query}")
371 |> json_response_and_validate_schema(200)
373 [account] = results["accounts"]
374 assert account["acct"] == "mike@osada.macgirvin.com"
377 test "search doesn't fetch remote accounts if resolve is false", %{conn: conn} do
380 |> get("/api/v1/search?q=mike@osada.macgirvin.com&resolve=false")
381 |> json_response_and_validate_schema(200)
383 assert [] == results["accounts"]
386 test "search with limit and offset", %{conn: conn} do
388 _user_two = insert(:user, %{nickname: "shp@shitposter.club"})
389 _user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
391 {:ok, _activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
392 {:ok, _activity2} = CommonAPI.post(user, %{status: "This is also about 2hu"})
396 |> get("/api/v1/search?q=2hu&limit=1")
398 assert results = json_response_and_validate_schema(result, 200)
399 assert [%{"id" => activity_id1}] = results["statuses"]
400 assert [_] = results["accounts"]
404 |> get("/api/v1/search?q=2hu&limit=1&offset=1")
405 |> json_response_and_validate_schema(200)
407 assert [%{"id" => activity_id2}] = results["statuses"]
408 assert [] = results["accounts"]
410 assert activity_id1 != activity_id2
413 test "search returns results only for the given type", %{conn: conn} do
415 _user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
417 {:ok, _activity} = CommonAPI.post(user, %{status: "This is about 2hu"})
419 assert %{"statuses" => [_activity], "accounts" => [], "hashtags" => []} =
421 |> get("/api/v1/search?q=2hu&type=statuses")
422 |> json_response_and_validate_schema(200)
424 assert %{"statuses" => [], "accounts" => [_user_two], "hashtags" => []} =
426 |> get("/api/v1/search?q=2hu&type=accounts")
427 |> json_response_and_validate_schema(200)
430 test "search uses account_id to filter statuses by the author", %{conn: conn} do
431 user = insert(:user, %{nickname: "shp@shitposter.club"})
432 user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
434 {:ok, activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
435 {:ok, activity2} = CommonAPI.post(user_two, %{status: "This is also about 2hu"})
439 |> get("/api/v1/search?q=2hu&account_id=#{user.id}")
440 |> json_response_and_validate_schema(200)
442 assert [%{"id" => activity_id1}] = results["statuses"]
443 assert activity_id1 == activity1.id
444 assert [_] = results["accounts"]
448 |> get("/api/v1/search?q=2hu&account_id=#{user_two.id}")
449 |> json_response_and_validate_schema(200)
451 assert [%{"id" => activity_id2}] = results["statuses"]
452 assert activity_id2 == activity2.id