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 Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config)
22 mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
26 describe ".search2" do
27 test "it returns empty result if user or status search return undefined error", %{conn: conn} do
29 {Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
30 {Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
35 |> get("/api/v2/search?q=2hu")
36 |> json_response_and_validate_schema(200)
38 assert results["accounts"] == []
39 assert results["statuses"] == []
41 "[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
46 test "search", %{conn: conn} do
48 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
49 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
51 {:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu private 天子"})
54 CommonAPI.post(user, %{
55 status: "This is about 2hu, but private",
59 {:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})
63 |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu #private"})}")
64 |> json_response_and_validate_schema(200)
66 [account | _] = results["accounts"]
67 assert account["id"] == to_string(user_three.id)
69 assert results["hashtags"] == [
70 %{"name" => "private", "url" => "#{Endpoint.url()}/tag/private"}
73 [status] = results["statuses"]
74 assert status["id"] == to_string(activity.id)
77 get(conn, "/api/v2/search?q=天子")
78 |> json_response_and_validate_schema(200)
80 assert results["hashtags"] == [
81 %{"name" => "天子", "url" => "#{Endpoint.url()}/tag/天子"}
84 [status] = results["statuses"]
85 assert status["id"] == to_string(activity.id)
88 test "search local-only status as an authenticated user" do
90 %{conn: conn} = oauth_access(["read:search"])
93 CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})
97 |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
98 |> json_response_and_validate_schema(200)
100 [status] = results["statuses"]
101 assert status["id"] == to_string(activity.id)
104 test "search local-only status as an unauthenticated user" do
106 %{conn: conn} = oauth_access([])
109 CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})
113 |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
114 |> json_response_and_validate_schema(200)
116 assert [] = results["statuses"]
119 test "search local-only status as an anonymous user" do
123 CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})
127 |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
128 |> json_response_and_validate_schema(200)
130 assert [] = results["statuses"]
133 @tag capture_log: true
134 test "constructs hashtags from search query", %{conn: conn} do
137 |> get("/api/v2/search?#{URI.encode_query(%{q: "some text with #explicit #hashtags"})}")
138 |> json_response_and_validate_schema(200)
140 assert results["hashtags"] == [
141 %{"name" => "explicit", "url" => "#{Endpoint.url()}/tag/explicit"},
142 %{"name" => "hashtags", "url" => "#{Endpoint.url()}/tag/hashtags"}
147 |> get("/api/v2/search?#{URI.encode_query(%{q: "john doe JOHN DOE"})}")
148 |> json_response_and_validate_schema(200)
150 assert results["hashtags"] == [
151 %{"name" => "john", "url" => "#{Endpoint.url()}/tag/john"},
152 %{"name" => "doe", "url" => "#{Endpoint.url()}/tag/doe"},
153 %{"name" => "JohnDoe", "url" => "#{Endpoint.url()}/tag/JohnDoe"}
158 |> get("/api/v2/search?#{URI.encode_query(%{q: "accident-prone"})}")
159 |> json_response_and_validate_schema(200)
161 assert results["hashtags"] == [
162 %{"name" => "accident", "url" => "#{Endpoint.url()}/tag/accident"},
163 %{"name" => "prone", "url" => "#{Endpoint.url()}/tag/prone"},
164 %{"name" => "AccidentProne", "url" => "#{Endpoint.url()}/tag/AccidentProne"}
169 |> get("/api/v2/search?#{URI.encode_query(%{q: "https://shpposter.club/users/shpuld"})}")
170 |> json_response_and_validate_schema(200)
172 assert results["hashtags"] == [
173 %{"name" => "shpuld", "url" => "#{Endpoint.url()}/tag/shpuld"}
179 "/api/v2/search?#{URI.encode_query(%{q: "https://www.washingtonpost.com/sports/2020/06/10/" <> "nascar-ban-display-confederate-flag-all-events-properties/"})}"
181 |> json_response_and_validate_schema(200)
183 assert results["hashtags"] == [
184 %{"name" => "nascar", "url" => "#{Endpoint.url()}/tag/nascar"},
185 %{"name" => "ban", "url" => "#{Endpoint.url()}/tag/ban"},
186 %{"name" => "display", "url" => "#{Endpoint.url()}/tag/display"},
187 %{"name" => "confederate", "url" => "#{Endpoint.url()}/tag/confederate"},
188 %{"name" => "flag", "url" => "#{Endpoint.url()}/tag/flag"},
189 %{"name" => "all", "url" => "#{Endpoint.url()}/tag/all"},
190 %{"name" => "events", "url" => "#{Endpoint.url()}/tag/events"},
191 %{"name" => "properties", "url" => "#{Endpoint.url()}/tag/properties"},
193 "name" => "NascarBanDisplayConfederateFlagAllEventsProperties",
195 "#{Endpoint.url()}/tag/NascarBanDisplayConfederateFlagAllEventsProperties"
200 test "supports pagination of hashtags search results", %{conn: conn} do
204 "/api/v2/search?#{URI.encode_query(%{q: "#some #text #with #hashtags", limit: 2, offset: 1})}"
206 |> json_response_and_validate_schema(200)
208 assert results["hashtags"] == [
209 %{"name" => "text", "url" => "#{Endpoint.url()}/tag/text"},
210 %{"name" => "with", "url" => "#{Endpoint.url()}/tag/with"}
214 test "excludes a blocked users from search results", %{conn: conn} do
216 user_smith = insert(:user, %{nickname: "Agent", name: "I love 2hu"})
217 user_neo = insert(:user, %{nickname: "Agent Neo", name: "Agent"})
219 {:ok, act1} = CommonAPI.post(user, %{status: "This is about 2hu private 天子"})
220 {:ok, act2} = CommonAPI.post(user_smith, %{status: "Agent Smith"})
221 {:ok, act3} = CommonAPI.post(user_neo, %{status: "Agent Smith"})
222 Pleroma.User.block(user, user_smith)
226 |> assign(:user, user)
227 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read"]))
228 |> get("/api/v2/search?q=Agent")
229 |> json_response_and_validate_schema(200)
231 status_ids = Enum.map(results["statuses"], fn g -> g["id"] end)
233 assert act3.id in status_ids
234 refute act2.id in status_ids
235 refute act1.id in status_ids
239 describe ".account_search" do
240 test "account search", %{conn: conn} do
241 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
242 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
246 |> get("/api/v1/accounts/search?q=shp")
247 |> json_response_and_validate_schema(200)
249 result_ids = for result <- results, do: result["acct"]
251 assert user_two.nickname in result_ids
252 assert user_three.nickname in result_ids
256 |> get("/api/v1/accounts/search?q=2hu")
257 |> json_response_and_validate_schema(200)
259 result_ids = for result <- results, do: result["acct"]
261 assert user_three.nickname in result_ids
264 test "returns account if query contains a space", %{conn: conn} do
265 insert(:user, %{nickname: "shp@shitposter.club"})
269 |> get("/api/v1/accounts/search?q=shp@shitposter.club xxx")
270 |> json_response_and_validate_schema(200)
272 assert length(results) == 1
276 describe ".search" do
277 test "it returns empty result if user or status search return undefined error", %{conn: conn} do
279 {Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
280 {Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
285 |> get("/api/v1/search?q=2hu")
286 |> json_response_and_validate_schema(200)
288 assert results["accounts"] == []
289 assert results["statuses"] == []
291 "[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
295 test "search", %{conn: conn} do
297 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
298 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
300 {:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu"})
303 CommonAPI.post(user, %{
304 status: "This is about 2hu, but private",
305 visibility: "private"
308 {:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})
312 |> get("/api/v1/search?q=2hu")
313 |> json_response_and_validate_schema(200)
315 [account | _] = results["accounts"]
316 assert account["id"] == to_string(user_three.id)
318 assert results["hashtags"] == ["2hu"]
320 [status] = results["statuses"]
321 assert status["id"] == to_string(activity.id)
324 test "search fetches remote statuses and prefers them over other results", %{conn: conn} do
325 {:ok, %{id: activity_id}} =
326 CommonAPI.post(insert(:user), %{
327 status: "check out http://mastodon.example.org/@admin/99541947525187367"
330 %{"url" => result_url, "id" => result_id} =
332 |> get("/api/v1/search?q=http://mastodon.example.org/@admin/99541947525187367")
333 |> json_response_and_validate_schema(200)
334 |> Map.get("statuses")
337 refute match?(^result_id, activity_id)
338 assert match?(^result_url, "http://mastodon.example.org/@admin/99541947525187367")
341 test "search doesn't show statuses that it shouldn't", %{conn: conn} do
343 CommonAPI.post(insert(:user), %{
344 status: "This is about 2hu, but private",
345 visibility: "private"
349 q = Object.normalize(activity, fetch: false).data["id"]
353 |> get("/api/v1/search?q=#{q}")
354 |> json_response_and_validate_schema(200)
356 [] = results["statuses"]
360 test "search fetches remote accounts", %{conn: conn} do
363 query = URI.encode_query(%{q: " mike@osada.macgirvin.com ", resolve: true})
367 |> assign(:user, user)
368 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read"]))
369 |> get("/api/v1/search?#{query}")
370 |> json_response_and_validate_schema(200)
372 [account] = results["accounts"]
373 assert account["acct"] == "mike@osada.macgirvin.com"
376 test "search doesn't fetch remote accounts if resolve is false", %{conn: conn} do
379 |> get("/api/v1/search?q=mike@osada.macgirvin.com&resolve=false")
380 |> json_response_and_validate_schema(200)
382 assert [] == results["accounts"]
385 test "search with limit and offset", %{conn: conn} do
387 _user_two = insert(:user, %{nickname: "shp@shitposter.club"})
388 _user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
390 {:ok, _activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
391 {:ok, _activity2} = CommonAPI.post(user, %{status: "This is also about 2hu"})
395 |> get("/api/v1/search?q=2hu&limit=1")
397 assert results = json_response_and_validate_schema(result, 200)
398 assert [%{"id" => activity_id1}] = results["statuses"]
399 assert [_] = results["accounts"]
403 |> get("/api/v1/search?q=2hu&limit=1&offset=1")
404 |> json_response_and_validate_schema(200)
406 assert [%{"id" => activity_id2}] = results["statuses"]
407 assert [] = results["accounts"]
409 assert activity_id1 != activity_id2
412 test "search returns results only for the given type", %{conn: conn} do
414 _user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
416 {:ok, _activity} = CommonAPI.post(user, %{status: "This is about 2hu"})
418 assert %{"statuses" => [_activity], "accounts" => [], "hashtags" => []} =
420 |> get("/api/v1/search?q=2hu&type=statuses")
421 |> json_response_and_validate_schema(200)
423 assert %{"statuses" => [], "accounts" => [_user_two], "hashtags" => []} =
425 |> get("/api/v1/search?q=2hu&type=accounts")
426 |> json_response_and_validate_schema(200)
429 test "search uses account_id to filter statuses by the author", %{conn: conn} do
430 user = insert(:user, %{nickname: "shp@shitposter.club"})
431 user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
433 {:ok, activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
434 {:ok, activity2} = CommonAPI.post(user_two, %{status: "This is also about 2hu"})
438 |> get("/api/v1/search?q=2hu&account_id=#{user.id}")
439 |> json_response_and_validate_schema(200)
441 assert [%{"id" => activity_id1}] = results["statuses"]
442 assert activity_id1 == activity1.id
443 assert [_] = results["accounts"]
447 |> get("/api/v1/search?q=2hu&account_id=#{user_two.id}")
448 |> json_response_and_validate_schema(200)
450 assert [%{"id" => activity_id2}] = results["statuses"]
451 assert activity_id2 == activity2.id