2019-06-14 11:39:57 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-26 06:11:42 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-06-14 11:39:57 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.SearchControllerTest do
|
|
|
|
use Pleroma.Web.ConnCase
|
|
|
|
|
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.Web.CommonAPI
|
2021-05-31 20:09:11 +00:00
|
|
|
alias Pleroma.Web.Endpoint
|
2019-06-14 11:39:57 +00:00
|
|
|
import Pleroma.Factory
|
|
|
|
import ExUnit.CaptureLog
|
|
|
|
import Tesla.Mock
|
2019-07-10 08:28:03 +00:00
|
|
|
import Mock
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2023-11-12 11:54:16 +00:00
|
|
|
setup do
|
|
|
|
Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config)
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2020-05-13 06:20:25 +00:00
|
|
|
setup_all do
|
2019-07-10 08:28:03 +00:00
|
|
|
mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
2019-06-14 11:39:57 +00:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
describe ".search2" do
|
|
|
|
test "it returns empty result if user or status search return undefined error", %{conn: conn} do
|
|
|
|
with_mocks [
|
|
|
|
{Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
|
2019-07-11 13:55:31 +00:00
|
|
|
{Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
|
2019-07-10 08:28:03 +00:00
|
|
|
] do
|
2019-07-20 13:07:51 +00:00
|
|
|
capture_log(fn ->
|
|
|
|
results =
|
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v2/search?q=2hu")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-07-20 13:07:51 +00:00
|
|
|
|
|
|
|
assert results["accounts"] == []
|
|
|
|
assert results["statuses"] == []
|
|
|
|
end) =~
|
|
|
|
"[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
|
2019-07-10 08:28:03 +00:00
|
|
|
end
|
|
|
|
end
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
test "search", %{conn: conn} do
|
|
|
|
user = insert(:user)
|
|
|
|
user_two = insert(:user, %{nickname: "shp@shitposter.club"})
|
2019-10-11 10:41:44 +00:00
|
|
|
user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu private 天子"})
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
{:ok, _activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "This is about 2hu, but private",
|
|
|
|
visibility: "private"
|
2019-07-10 08:28:03 +00:00
|
|
|
})
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-10-09 14:03:45 +00:00
|
|
|
results =
|
2020-01-20 18:47:44 +00:00
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v2/search?#{URI.encode_query(%{q: "2hu #private"})}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
[account | _] = results["accounts"]
|
|
|
|
assert account["id"] == to_string(user_three.id)
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
assert results["hashtags"] == [
|
2021-05-31 20:09:11 +00:00
|
|
|
%{"name" => "private", "url" => "#{Endpoint.url()}/tag/private"}
|
2019-07-10 08:28:03 +00:00
|
|
|
]
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
[status] = results["statuses"]
|
|
|
|
assert status["id"] == to_string(activity.id)
|
2019-10-09 14:03:45 +00:00
|
|
|
|
|
|
|
results =
|
2020-05-07 10:04:48 +00:00
|
|
|
get(conn, "/api/v2/search?q=天子")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-09 14:03:45 +00:00
|
|
|
|
2020-05-30 07:29:08 +00:00
|
|
|
assert results["hashtags"] == [
|
2021-05-31 20:09:11 +00:00
|
|
|
%{"name" => "天子", "url" => "#{Endpoint.url()}/tag/天子"}
|
2020-05-30 07:29:08 +00:00
|
|
|
]
|
|
|
|
|
2019-10-11 10:41:44 +00:00
|
|
|
[status] = results["statuses"]
|
|
|
|
assert status["id"] == to_string(activity.id)
|
2019-07-10 08:28:03 +00:00
|
|
|
end
|
2020-01-20 18:47:44 +00:00
|
|
|
|
2022-05-05 14:44:34 +00:00
|
|
|
test "search local-only status as an authenticated user" do
|
|
|
|
user = insert(:user)
|
|
|
|
%{conn: conn} = oauth_access(["read:search"])
|
|
|
|
|
2022-05-05 15:18:18 +00:00
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})
|
2022-05-05 14:44:34 +00:00
|
|
|
|
|
|
|
results =
|
|
|
|
conn
|
|
|
|
|> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
[status] = results["statuses"]
|
|
|
|
assert status["id"] == to_string(activity.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "search local-only status as an unauthenticated user" do
|
|
|
|
user = insert(:user)
|
|
|
|
%{conn: conn} = oauth_access([])
|
|
|
|
|
2022-05-05 15:18:18 +00:00
|
|
|
{:ok, _activity} =
|
|
|
|
CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})
|
2022-05-05 14:44:34 +00:00
|
|
|
|
|
|
|
results =
|
|
|
|
conn
|
|
|
|
|> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert [] = results["statuses"]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "search local-only status as an anonymous user" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
2022-05-05 15:18:18 +00:00
|
|
|
{:ok, _activity} =
|
|
|
|
CommonAPI.post(user, %{status: "This is about 2hu private 天子", visibility: "local"})
|
2022-05-05 14:44:34 +00:00
|
|
|
|
|
|
|
results =
|
|
|
|
build_conn()
|
|
|
|
|> get("/api/v2/search?#{URI.encode_query(%{q: "2hu"})}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert [] = results["statuses"]
|
|
|
|
end
|
|
|
|
|
2020-07-03 14:36:55 +00:00
|
|
|
@tag capture_log: true
|
2020-05-29 19:03:14 +00:00
|
|
|
test "constructs hashtags from search query", %{conn: conn} do
|
|
|
|
results =
|
|
|
|
conn
|
|
|
|
|> get("/api/v2/search?#{URI.encode_query(%{q: "some text with #explicit #hashtags"})}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert results["hashtags"] == [
|
2021-05-31 20:09:11 +00:00
|
|
|
%{"name" => "explicit", "url" => "#{Endpoint.url()}/tag/explicit"},
|
|
|
|
%{"name" => "hashtags", "url" => "#{Endpoint.url()}/tag/hashtags"}
|
2020-05-29 19:03:14 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
results =
|
|
|
|
conn
|
|
|
|
|> get("/api/v2/search?#{URI.encode_query(%{q: "john doe JOHN DOE"})}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert results["hashtags"] == [
|
2021-05-31 20:09:11 +00:00
|
|
|
%{"name" => "john", "url" => "#{Endpoint.url()}/tag/john"},
|
|
|
|
%{"name" => "doe", "url" => "#{Endpoint.url()}/tag/doe"},
|
|
|
|
%{"name" => "JohnDoe", "url" => "#{Endpoint.url()}/tag/JohnDoe"}
|
2020-05-29 19:03:14 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
results =
|
|
|
|
conn
|
|
|
|
|> get("/api/v2/search?#{URI.encode_query(%{q: "accident-prone"})}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert results["hashtags"] == [
|
2021-05-31 20:09:11 +00:00
|
|
|
%{"name" => "accident", "url" => "#{Endpoint.url()}/tag/accident"},
|
|
|
|
%{"name" => "prone", "url" => "#{Endpoint.url()}/tag/prone"},
|
|
|
|
%{"name" => "AccidentProne", "url" => "#{Endpoint.url()}/tag/AccidentProne"}
|
2020-05-29 19:03:14 +00:00
|
|
|
]
|
2020-06-11 13:05:14 +00:00
|
|
|
|
|
|
|
results =
|
|
|
|
conn
|
|
|
|
|> get("/api/v2/search?#{URI.encode_query(%{q: "https://shpposter.club/users/shpuld"})}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert results["hashtags"] == [
|
2021-05-31 20:09:11 +00:00
|
|
|
%{"name" => "shpuld", "url" => "#{Endpoint.url()}/tag/shpuld"}
|
2020-06-11 13:05:14 +00:00
|
|
|
]
|
2020-06-12 11:25:41 +00:00
|
|
|
|
|
|
|
results =
|
|
|
|
conn
|
|
|
|
|> get(
|
2021-10-06 06:08:21 +00:00
|
|
|
"/api/v2/search?#{URI.encode_query(%{q: "https://www.washingtonpost.com/sports/2020/06/10/" <> "nascar-ban-display-confederate-flag-all-events-properties/"})}"
|
2020-06-12 11:25:41 +00:00
|
|
|
)
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert results["hashtags"] == [
|
2021-05-31 20:09:11 +00:00
|
|
|
%{"name" => "nascar", "url" => "#{Endpoint.url()}/tag/nascar"},
|
|
|
|
%{"name" => "ban", "url" => "#{Endpoint.url()}/tag/ban"},
|
|
|
|
%{"name" => "display", "url" => "#{Endpoint.url()}/tag/display"},
|
|
|
|
%{"name" => "confederate", "url" => "#{Endpoint.url()}/tag/confederate"},
|
|
|
|
%{"name" => "flag", "url" => "#{Endpoint.url()}/tag/flag"},
|
|
|
|
%{"name" => "all", "url" => "#{Endpoint.url()}/tag/all"},
|
|
|
|
%{"name" => "events", "url" => "#{Endpoint.url()}/tag/events"},
|
|
|
|
%{"name" => "properties", "url" => "#{Endpoint.url()}/tag/properties"},
|
2020-06-12 11:25:41 +00:00
|
|
|
%{
|
|
|
|
"name" => "NascarBanDisplayConfederateFlagAllEventsProperties",
|
|
|
|
"url" =>
|
2021-05-31 20:09:11 +00:00
|
|
|
"#{Endpoint.url()}/tag/NascarBanDisplayConfederateFlagAllEventsProperties"
|
2020-06-12 11:25:41 +00:00
|
|
|
}
|
|
|
|
]
|
2020-05-29 19:03:14 +00:00
|
|
|
end
|
|
|
|
|
2020-06-19 13:14:06 +00:00
|
|
|
test "supports pagination of hashtags search results", %{conn: conn} do
|
|
|
|
results =
|
|
|
|
conn
|
|
|
|
|> get(
|
2021-10-06 06:08:21 +00:00
|
|
|
"/api/v2/search?#{URI.encode_query(%{q: "#some #text #with #hashtags", limit: 2, offset: 1})}"
|
2020-06-19 13:14:06 +00:00
|
|
|
)
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert results["hashtags"] == [
|
2021-05-31 20:09:11 +00:00
|
|
|
%{"name" => "text", "url" => "#{Endpoint.url()}/tag/text"},
|
|
|
|
%{"name" => "with", "url" => "#{Endpoint.url()}/tag/with"}
|
2020-06-19 13:14:06 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2020-01-20 18:47:44 +00:00
|
|
|
test "excludes a blocked users from search results", %{conn: conn} do
|
|
|
|
user = insert(:user)
|
|
|
|
user_smith = insert(:user, %{nickname: "Agent", name: "I love 2hu"})
|
|
|
|
user_neo = insert(:user, %{nickname: "Agent Neo", name: "Agent"})
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, act1} = CommonAPI.post(user, %{status: "This is about 2hu private 天子"})
|
|
|
|
{:ok, act2} = CommonAPI.post(user_smith, %{status: "Agent Smith"})
|
|
|
|
{:ok, act3} = CommonAPI.post(user_neo, %{status: "Agent Smith"})
|
2020-01-20 18:47:44 +00:00
|
|
|
Pleroma.User.block(user, user_smith)
|
|
|
|
|
|
|
|
results =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> assign(:token, insert(:oauth_token, user: user, scopes: ["read"]))
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v2/search?q=Agent")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2020-01-20 18:47:44 +00:00
|
|
|
|
|
|
|
status_ids = Enum.map(results["statuses"], fn g -> g["id"] end)
|
|
|
|
|
|
|
|
assert act3.id in status_ids
|
|
|
|
refute act2.id in status_ids
|
|
|
|
refute act1.id in status_ids
|
|
|
|
end
|
2019-07-10 08:28:03 +00:00
|
|
|
end
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
describe ".account_search" do
|
|
|
|
test "account search", %{conn: conn} do
|
|
|
|
user_two = insert(:user, %{nickname: "shp@shitposter.club"})
|
|
|
|
user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
results =
|
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/accounts/search?q=shp")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
result_ids = for result <- results, do: result["acct"]
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
assert user_two.nickname in result_ids
|
|
|
|
assert user_three.nickname in result_ids
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
results =
|
2019-06-14 11:39:57 +00:00
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/accounts/search?q=2hu")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
result_ids = for result <- results, do: result["acct"]
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
assert user_three.nickname in result_ids
|
|
|
|
end
|
2019-08-03 18:13:20 +00:00
|
|
|
|
|
|
|
test "returns account if query contains a space", %{conn: conn} do
|
2019-12-19 14:23:27 +00:00
|
|
|
insert(:user, %{nickname: "shp@shitposter.club"})
|
2019-08-03 18:13:20 +00:00
|
|
|
|
|
|
|
results =
|
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/accounts/search?q=shp@shitposter.club xxx")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-08-03 18:13:20 +00:00
|
|
|
|
|
|
|
assert length(results) == 1
|
|
|
|
end
|
2019-06-14 11:39:57 +00:00
|
|
|
end
|
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
describe ".search" do
|
|
|
|
test "it returns empty result if user or status search return undefined error", %{conn: conn} do
|
|
|
|
with_mocks [
|
|
|
|
{Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
|
2019-07-11 13:55:31 +00:00
|
|
|
{Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
|
2019-07-10 08:28:03 +00:00
|
|
|
] do
|
2019-07-20 13:07:51 +00:00
|
|
|
capture_log(fn ->
|
|
|
|
results =
|
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/search?q=2hu")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-07-20 13:07:51 +00:00
|
|
|
|
|
|
|
assert results["accounts"] == []
|
|
|
|
assert results["statuses"] == []
|
|
|
|
end) =~
|
|
|
|
"[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
|
2019-07-10 08:28:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "search", %{conn: conn} do
|
|
|
|
user = insert(:user)
|
|
|
|
user_two = insert(:user, %{nickname: "shp@shitposter.club"})
|
|
|
|
user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity} = CommonAPI.post(user, %{status: "This is about 2hu"})
|
2019-07-10 08:28:03 +00:00
|
|
|
|
|
|
|
{:ok, _activity} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "This is about 2hu, but private",
|
|
|
|
visibility: "private"
|
2019-07-10 08:28:03 +00:00
|
|
|
})
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, _} = CommonAPI.post(user_two, %{status: "This isn't"})
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2020-01-20 18:47:44 +00:00
|
|
|
results =
|
2019-06-14 11:39:57 +00:00
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/search?q=2hu")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
[account | _] = results["accounts"]
|
|
|
|
assert account["id"] == to_string(user_three.id)
|
|
|
|
|
2020-05-29 19:03:14 +00:00
|
|
|
assert results["hashtags"] == ["2hu"]
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
[status] = results["statuses"]
|
|
|
|
assert status["id"] == to_string(activity.id)
|
|
|
|
end
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-12-09 17:45:04 +00:00
|
|
|
test "search fetches remote statuses and prefers them over other results", %{conn: conn} do
|
2020-11-23 14:29:55 +00:00
|
|
|
old_version = :persistent_term.get({Pleroma.Repo, :postgres_version})
|
|
|
|
:persistent_term.put({Pleroma.Repo, :postgres_version}, 10.0)
|
2020-12-01 16:45:25 +00:00
|
|
|
on_exit(fn -> :persistent_term.put({Pleroma.Repo, :postgres_version}, old_version) end)
|
2020-11-19 15:53:26 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
capture_log(fn ->
|
2019-12-09 17:45:04 +00:00
|
|
|
{:ok, %{id: activity_id}} =
|
|
|
|
CommonAPI.post(insert(:user), %{
|
2020-09-08 07:13:11 +00:00
|
|
|
status: "check out http://mastodon.example.org/@admin/99541947525187367"
|
2019-12-09 17:45:04 +00:00
|
|
|
})
|
|
|
|
|
2020-01-20 18:47:44 +00:00
|
|
|
results =
|
2019-07-10 08:28:03 +00:00
|
|
|
conn
|
2020-09-08 07:13:11 +00:00
|
|
|
|> get("/api/v1/search?q=http://mastodon.example.org/@admin/99541947525187367")
|
2020-05-07 10:04:48 +00:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-07-10 08:28:03 +00:00
|
|
|
|
2020-09-08 07:13:11 +00:00
|
|
|
assert [
|
|
|
|
%{"url" => "http://mastodon.example.org/@admin/99541947525187367"},
|
|
|
|
%{"id" => ^activity_id}
|
|
|
|
] = results["statuses"]
|
2019-07-10 08:28:03 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "search doesn't show statuses that it shouldn't", %{conn: conn} do
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(insert(:user), %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "This is about 2hu, but private",
|
|
|
|
visibility: "private"
|
2019-07-10 08:28:03 +00:00
|
|
|
})
|
2019-06-14 11:39:57 +00:00
|
|
|
|
2019-07-10 08:28:03 +00:00
|
|
|
capture_log(fn ->
|
2021-01-04 12:38:31 +00:00
|
|
|
q = Object.normalize(activity, fetch: false).data["id"]
|
2020-05-07 10:04:48 +00:00
|
|
|
|
2020-01-20 18:47:44 +00:00
|
|
|
results =
|
2019-07-10 08:28:03 +00:00
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/search?q=#{q}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-07-10 08:28:03 +00:00
|
|
|
|
|
|
|
[] = results["statuses"]
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "search fetches remote accounts", %{conn: conn} do
|
|
|
|
user = insert(:user)
|
|
|
|
|
2020-07-03 14:36:55 +00:00
|
|
|
query = URI.encode_query(%{q: " mike@osada.macgirvin.com ", resolve: true})
|
|
|
|
|
2020-01-20 18:47:44 +00:00
|
|
|
results =
|
2019-07-10 08:28:03 +00:00
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
2019-12-19 14:23:27 +00:00
|
|
|
|> assign(:token, insert(:oauth_token, user: user, scopes: ["read"]))
|
2020-07-03 14:36:55 +00:00
|
|
|
|> get("/api/v1/search?#{query}")
|
2020-05-07 10:04:48 +00:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-07-10 08:28:03 +00:00
|
|
|
|
|
|
|
[account] = results["accounts"]
|
2019-10-18 04:14:26 +00:00
|
|
|
assert account["acct"] == "mike@osada.macgirvin.com"
|
2019-07-10 08:28:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "search doesn't fetch remote accounts if resolve is false", %{conn: conn} do
|
2020-01-20 18:47:44 +00:00
|
|
|
results =
|
2019-07-10 08:28:03 +00:00
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/search?q=mike@osada.macgirvin.com&resolve=false")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-07-10 08:28:03 +00:00
|
|
|
|
|
|
|
assert [] == results["accounts"]
|
|
|
|
end
|
2019-07-11 13:55:31 +00:00
|
|
|
|
|
|
|
test "search with limit and offset", %{conn: conn} do
|
|
|
|
user = insert(:user)
|
|
|
|
_user_two = insert(:user, %{nickname: "shp@shitposter.club"})
|
|
|
|
_user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, _activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
|
|
|
|
{:ok, _activity2} = CommonAPI.post(user, %{status: "This is also about 2hu"})
|
2019-07-11 13:55:31 +00:00
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/search?q=2hu&limit=1")
|
2019-07-11 13:55:31 +00:00
|
|
|
|
2020-05-07 10:04:48 +00:00
|
|
|
assert results = json_response_and_validate_schema(result, 200)
|
2019-07-11 13:55:31 +00:00
|
|
|
assert [%{"id" => activity_id1}] = results["statuses"]
|
|
|
|
assert [_] = results["accounts"]
|
|
|
|
|
|
|
|
results =
|
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/search?q=2hu&limit=1&offset=1")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-07-11 13:55:31 +00:00
|
|
|
|
|
|
|
assert [%{"id" => activity_id2}] = results["statuses"]
|
|
|
|
assert [] = results["accounts"]
|
|
|
|
|
|
|
|
assert activity_id1 != activity_id2
|
|
|
|
end
|
|
|
|
|
|
|
|
test "search returns results only for the given type", %{conn: conn} do
|
|
|
|
user = insert(:user)
|
|
|
|
_user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, _activity} = CommonAPI.post(user, %{status: "This is about 2hu"})
|
2019-07-11 13:55:31 +00:00
|
|
|
|
|
|
|
assert %{"statuses" => [_activity], "accounts" => [], "hashtags" => []} =
|
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/search?q=2hu&type=statuses")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-07-11 13:55:31 +00:00
|
|
|
|
|
|
|
assert %{"statuses" => [], "accounts" => [_user_two], "hashtags" => []} =
|
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/search?q=2hu&type=accounts")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-07-11 13:55:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "search uses account_id to filter statuses by the author", %{conn: conn} do
|
|
|
|
user = insert(:user, %{nickname: "shp@shitposter.club"})
|
|
|
|
user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity1} = CommonAPI.post(user, %{status: "This is about 2hu"})
|
|
|
|
{:ok, activity2} = CommonAPI.post(user_two, %{status: "This is also about 2hu"})
|
2019-07-11 13:55:31 +00:00
|
|
|
|
|
|
|
results =
|
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/search?q=2hu&account_id=#{user.id}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-07-11 13:55:31 +00:00
|
|
|
|
|
|
|
assert [%{"id" => activity_id1}] = results["statuses"]
|
|
|
|
assert activity_id1 == activity1.id
|
|
|
|
assert [_] = results["accounts"]
|
|
|
|
|
|
|
|
results =
|
|
|
|
conn
|
2020-05-07 10:04:48 +00:00
|
|
|
|> get("/api/v1/search?q=2hu&account_id=#{user_two.id}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-07-11 13:55:31 +00:00
|
|
|
|
|
|
|
assert [%{"id" => activity_id2}] = results["statuses"]
|
|
|
|
assert activity_id2 == activity2.id
|
|
|
|
end
|
2019-06-14 11:39:57 +00:00
|
|
|
end
|
|
|
|
end
|