mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2024-11-05 08:31:36 +00:00
B MeiliSearch, SearchIndexingWorker: Use Config.Getting, make tests async.
This commit is contained in:
parent
c1402af293
commit
d3f8950588
3 changed files with 72 additions and 48 deletions
|
@ -3,6 +3,7 @@ defmodule Pleroma.Search.Meilisearch do
|
|||
require Pleroma.Constants
|
||||
|
||||
alias Pleroma.Activity
|
||||
alias Pleroma.Config.Getting, as: Config
|
||||
|
||||
import Pleroma.Search.DatabaseSearch
|
||||
import Ecto.Query
|
||||
|
@ -10,14 +11,14 @@ defmodule Pleroma.Search.Meilisearch do
|
|||
@behaviour Pleroma.Search.SearchBackend
|
||||
|
||||
defp meili_headers do
|
||||
private_key = Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
|
||||
private_key = Config.get([Pleroma.Search.Meilisearch, :private_key])
|
||||
|
||||
[{"Content-Type", "application/json"}] ++
|
||||
if is_nil(private_key), do: [], else: [{"Authorization", "Bearer #{private_key}"}]
|
||||
end
|
||||
|
||||
def meili_get(path) do
|
||||
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
||||
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
||||
|
||||
result =
|
||||
Pleroma.HTTP.get(
|
||||
|
@ -31,7 +32,7 @@ defmodule Pleroma.Search.Meilisearch do
|
|||
end
|
||||
|
||||
def meili_post(path, params) do
|
||||
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
||||
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
||||
|
||||
result =
|
||||
Pleroma.HTTP.post(
|
||||
|
@ -46,7 +47,7 @@ defmodule Pleroma.Search.Meilisearch do
|
|||
end
|
||||
|
||||
def meili_put(path, params) do
|
||||
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
||||
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
||||
|
||||
result =
|
||||
Pleroma.HTTP.request(
|
||||
|
@ -63,15 +64,20 @@ defmodule Pleroma.Search.Meilisearch do
|
|||
end
|
||||
|
||||
def meili_delete(path) do
|
||||
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
||||
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
||||
|
||||
with {:ok, _} <-
|
||||
Pleroma.HTTP.request(
|
||||
:delete,
|
||||
Path.join(endpoint, path),
|
||||
"",
|
||||
meili_headers(),
|
||||
[]
|
||||
)
|
||||
) do
|
||||
:ok
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
|
|
|
@ -3,10 +3,12 @@ defmodule Pleroma.Workers.SearchIndexingWorker do
|
|||
|
||||
@impl Oban.Worker
|
||||
|
||||
alias Pleroma.Config.Getting, as: Config
|
||||
|
||||
def perform(%Job{args: %{"op" => "add_to_index", "activity" => activity_id}}) do
|
||||
activity = Pleroma.Activity.get_by_id_with_object(activity_id)
|
||||
|
||||
search_module = Pleroma.Config.get([Pleroma.Search, :module])
|
||||
search_module = Config.get([Pleroma.Search, :module])
|
||||
|
||||
search_module.add_to_index(activity)
|
||||
end
|
||||
|
@ -14,7 +16,7 @@ defmodule Pleroma.Workers.SearchIndexingWorker do
|
|||
def perform(%Job{args: %{"op" => "remove_from_index", "object" => object_id}}) do
|
||||
object = Pleroma.Object.get_by_id(object_id)
|
||||
|
||||
search_module = Pleroma.Config.get([Pleroma.Search, :module])
|
||||
search_module = Config.get([Pleroma.Search, :module])
|
||||
|
||||
search_module.remove_from_index(object)
|
||||
end
|
||||
|
|
|
@ -5,51 +5,50 @@
|
|||
defmodule Pleroma.Search.MeilisearchTest do
|
||||
require Pleroma.Constants
|
||||
|
||||
use Pleroma.DataCase
|
||||
use Pleroma.DataCase, async: true
|
||||
use Oban.Testing, repo: Pleroma.Repo
|
||||
|
||||
import Pleroma.Factory
|
||||
import Tesla.Mock
|
||||
import Mock
|
||||
import Mox
|
||||
|
||||
alias Pleroma.Search.Meilisearch
|
||||
alias Pleroma.Web.CommonAPI
|
||||
alias Pleroma.Workers.SearchIndexingWorker
|
||||
|
||||
setup_all do
|
||||
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
||||
:ok
|
||||
end
|
||||
alias Pleroma.UnstubbedConfigMock, as: Config
|
||||
|
||||
describe "meilisearch" do
|
||||
setup do: clear_config([Pleroma.Search, :module], Meilisearch)
|
||||
|
||||
setup_with_mocks(
|
||||
[
|
||||
{Meilisearch, [:passthrough],
|
||||
[
|
||||
add_to_index: fn a -> passthrough([a]) end,
|
||||
remove_from_index: fn a -> passthrough([a]) end,
|
||||
meili_put: fn u, a -> passthrough([u, a]) end
|
||||
]}
|
||||
],
|
||||
context,
|
||||
do: {:ok, context}
|
||||
)
|
||||
|
||||
test "indexes a local post on creation" do
|
||||
user = insert(:user)
|
||||
|
||||
mock_global(fn
|
||||
%{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
|
||||
Tesla.Mock.mock(fn
|
||||
%{
|
||||
method: :put,
|
||||
url: "http://127.0.0.1:7700/indexes/objects/documents",
|
||||
body: body
|
||||
} ->
|
||||
assert match?(
|
||||
[%{"content" => "guys i just don't wanna leave the swamp"}],
|
||||
Jason.decode!(body)
|
||||
)
|
||||
|
||||
# To make sure that the worker is called
|
||||
send(self(), "posted_to_meilisearch")
|
||||
json(%{updateId: 1})
|
||||
end)
|
||||
|
||||
Config
|
||||
|> expect(:get, 3, fn
|
||||
[Pleroma.Search, :module], nil ->
|
||||
Meilisearch
|
||||
|
||||
[Pleroma.Search.Meilisearch, :url], nil ->
|
||||
"http://127.0.0.1:7700"
|
||||
|
||||
[Pleroma.Search.Meilisearch, :private_key], nil ->
|
||||
"secret"
|
||||
end)
|
||||
|
||||
{:ok, activity} =
|
||||
CommonAPI.post(user, %{
|
||||
status: "guys i just don't wanna leave the swamp",
|
||||
|
@ -64,8 +63,7 @@ defmodule Pleroma.Search.MeilisearchTest do
|
|||
)
|
||||
|
||||
assert :ok = perform_job(SearchIndexingWorker, args)
|
||||
|
||||
assert_called(Meilisearch.add_to_index(activity))
|
||||
assert_received("posted_to_meilisearch")
|
||||
end
|
||||
|
||||
test "doesn't index posts that are not public" do
|
||||
|
@ -80,21 +78,26 @@ defmodule Pleroma.Search.MeilisearchTest do
|
|||
|
||||
args = %{"op" => "add_to_index", "activity" => activity.id}
|
||||
|
||||
assert_enqueued(worker: SearchIndexingWorker, args: args)
|
||||
assert :ok = perform_job(SearchIndexingWorker, args)
|
||||
|
||||
assert_not_called(Meilisearch.meili_put(:_))
|
||||
Config
|
||||
|> expect(:get, fn
|
||||
[Pleroma.Search, :module], nil ->
|
||||
Meilisearch
|
||||
end)
|
||||
|
||||
history = call_history(Meilisearch)
|
||||
assert Enum.count(history) == 2
|
||||
assert_enqueued(worker: SearchIndexingWorker, args: args)
|
||||
assert :ok = perform_job(SearchIndexingWorker, args)
|
||||
end)
|
||||
end
|
||||
|
||||
test "deletes posts from index when deleted locally" do
|
||||
user = insert(:user)
|
||||
|
||||
mock_global(fn
|
||||
%{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
|
||||
Tesla.Mock.mock(fn
|
||||
%{
|
||||
method: :put,
|
||||
url: "http://127.0.0.1:7700/indexes/objects/documents",
|
||||
body: body
|
||||
} ->
|
||||
assert match?(
|
||||
[%{"content" => "guys i just don't wanna leave the swamp"}],
|
||||
Jason.decode!(body)
|
||||
|
@ -103,10 +106,23 @@ defmodule Pleroma.Search.MeilisearchTest do
|
|||
json(%{updateId: 1})
|
||||
|
||||
%{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
|
||||
send(self(), "called_delete")
|
||||
assert String.length(id) > 1
|
||||
json(%{updateId: 2})
|
||||
end)
|
||||
|
||||
Config
|
||||
|> expect(:get, 6, fn
|
||||
[Pleroma.Search, :module], nil ->
|
||||
Meilisearch
|
||||
|
||||
[Pleroma.Search.Meilisearch, :url], nil ->
|
||||
"http://127.0.0.1:7700"
|
||||
|
||||
[Pleroma.Search.Meilisearch, :private_key], nil ->
|
||||
"secret"
|
||||
end)
|
||||
|
||||
{:ok, activity} =
|
||||
CommonAPI.post(user, %{
|
||||
status: "guys i just don't wanna leave the swamp",
|
||||
|
@ -123,7 +139,7 @@ defmodule Pleroma.Search.MeilisearchTest do
|
|||
assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
|
||||
assert :ok = perform_job(SearchIndexingWorker, delete_args)
|
||||
|
||||
assert_called(Meilisearch.remove_from_index(:_))
|
||||
assert_received("called_delete")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue