1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
10 alias Pleroma.Web.CommonAPI
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\"}"
40 test "search", %{conn: conn} do
42 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
43 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
45 {:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu private 天子"})
48 CommonAPI.post(user, %{
49 status: "This is about 2hu, but private",
53 {:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})
57 |> get("/api/v2/search?#{URI.encode_query(%{q: "2hu #private"})}")
58 |> json_response_and_validate_schema(200)
60 [account | _] = results["accounts"]
61 assert account["id"] == to_string(user_three.id)
63 assert results["hashtags"] == [
64 %{"name" => "private", "url" => "#{Web.base_url()}/tag/private"}
67 [status] = results["statuses"]
68 assert status["id"] == to_string(activity.id)
71 get(conn, "/api/v2/search?q=天子")
72 |> json_response_and_validate_schema(200)
74 [status] = results["statuses"]
75 assert status["id"] == to_string(activity.id)
78 test "excludes a blocked users from search results", %{conn: conn} do
80 user_smith = insert(:user, %{nickname: "Agent", name: "I love 2hu"})
81 user_neo = insert(:user, %{nickname: "Agent Neo", name: "Agent"})
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)
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)
95 status_ids = Enum.map(results["statuses"], fn g -> g["id"] end)
97 assert act3.id in status_ids
98 refute act2.id in status_ids
99 refute act1.id in status_ids
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"})
110 |> get("/api/v1/accounts/search?q=shp")
111 |> json_response_and_validate_schema(200)
113 result_ids = for result <- results, do: result["acct"]
115 assert user_two.nickname in result_ids
116 assert user_three.nickname in result_ids
120 |> get("/api/v1/accounts/search?q=2hu")
121 |> json_response_and_validate_schema(200)
123 result_ids = for result <- results, do: result["acct"]
125 assert user_three.nickname in result_ids
128 test "returns account if query contains a space", %{conn: conn} do
129 insert(:user, %{nickname: "shp@shitposter.club"})
133 |> get("/api/v1/accounts/search?q=shp@shitposter.club xxx")
134 |> json_response_and_validate_schema(200)
136 assert length(results) == 1
140 describe ".search" do
141 test "it returns empty result if user or status search return undefined error", %{conn: conn} do
143 {Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
144 {Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
149 |> get("/api/v1/search?q=2hu")
150 |> json_response_and_validate_schema(200)
152 assert results["accounts"] == []
153 assert results["statuses"] == []
155 "[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
159 test "search", %{conn: conn} do
161 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
162 user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
164 {:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu"})
167 CommonAPI.post(user, %{
168 status: "This is about 2hu, but private",
169 visibility: "private"
172 {:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})
176 |> get("/api/v1/search?q=2hu")
177 |> json_response_and_validate_schema(200)
179 [account | _] = results["accounts"]
180 assert account["id"] == to_string(user_three.id)
182 assert results["hashtags"] == []
184 [status] = results["statuses"]
185 assert status["id"] == to_string(activity.id)
188 test "search fetches remote statuses and prefers them over other results", %{conn: conn} do
190 {:ok, %{id: activity_id}} =
191 CommonAPI.post(insert(:user), %{
192 status: "check out https://shitposter.club/notice/2827873"
197 |> get("/api/v1/search?q=https://shitposter.club/notice/2827873")
198 |> json_response_and_validate_schema(200)
200 [status, %{"id" => ^activity_id}] = results["statuses"]
202 assert status["uri"] ==
203 "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
207 test "search doesn't show statuses that it shouldn't", %{conn: conn} do
209 CommonAPI.post(insert(:user), %{
210 status: "This is about 2hu, but private",
211 visibility: "private"
215 q = Object.normalize(activity).data["id"]
219 |> get("/api/v1/search?q=#{q}")
220 |> json_response_and_validate_schema(200)
222 [] = results["statuses"]
226 test "search fetches remote accounts", %{conn: conn} do
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)
236 [account] = results["accounts"]
237 assert account["acct"] == "mike@osada.macgirvin.com"
240 test "search doesn't fetch remote accounts if resolve is false", %{conn: conn} do
243 |> get("/api/v1/search?q=mike@osada.macgirvin.com&resolve=false")
244 |> json_response_and_validate_schema(200)
246 assert [] == results["accounts"]
249 test "search with limit and offset", %{conn: conn} do
251 _user_two = insert(:user, %{nickname: "shp@shitposter.club"})
252 _user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
254 {:ok, _activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
255 {:ok, _activity2} = CommonAPI.post(user, %{status: "This is also about 2hu"})
259 |> get("/api/v1/search?q=2hu&limit=1")
261 assert results = json_response_and_validate_schema(result, 200)
262 assert [%{"id" => activity_id1}] = results["statuses"]
263 assert [_] = results["accounts"]
267 |> get("/api/v1/search?q=2hu&limit=1&offset=1")
268 |> json_response_and_validate_schema(200)
270 assert [%{"id" => activity_id2}] = results["statuses"]
271 assert [] = results["accounts"]
273 assert activity_id1 != activity_id2
276 test "search returns results only for the given type", %{conn: conn} do
278 _user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
280 {:ok, _activity} = CommonAPI.post(user, %{status: "This is about 2hu"})
282 assert %{"statuses" => [_activity], "accounts" => [], "hashtags" => []} =
284 |> get("/api/v1/search?q=2hu&type=statuses")
285 |> json_response_and_validate_schema(200)
287 assert %{"statuses" => [], "accounts" => [_user_two], "hashtags" => []} =
289 |> get("/api/v1/search?q=2hu&type=accounts")
290 |> json_response_and_validate_schema(200)
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"})
297 {:ok, activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
298 {:ok, activity2} = CommonAPI.post(user_two, %{status: "This is also about 2hu"})
302 |> get("/api/v1/search?q=2hu&account_id=#{user.id}")
303 |> json_response_and_validate_schema(200)
305 assert [%{"id" => activity_id1}] = results["statuses"]
306 assert activity_id1 == activity1.id
307 assert [_] = results["accounts"]
311 |> get("/api/v1/search?q=2hu&account_id=#{user_two.id}")
312 |> json_response_and_validate_schema(200)
314 assert [%{"id" => activity_id2}] = results["statuses"]
315 assert activity_id2 == activity2.id