From d3f8950588b444dfdf46d87d5631720cc14a907c Mon Sep 17 00:00:00 2001 From: Lain Soykaf Date: Sun, 12 Nov 2023 14:49:50 +0400 Subject: [PATCH] B MeiliSearch, SearchIndexingWorker: Use Config.Getting, make tests async. --- lib/pleroma/search/meilisearch.ex | 30 ++++--- lib/pleroma/workers/search_indexing_worker.ex | 6 +- test/pleroma/search/meilisearch_test.exs | 84 +++++++++++-------- 3 files changed, 72 insertions(+), 48 deletions(-) diff --git a/lib/pleroma/search/meilisearch.ex b/lib/pleroma/search/meilisearch.ex index 7af7f460a..eed9fca1c 100644 --- a/lib/pleroma/search/meilisearch.ex +++ b/lib/pleroma/search/meilisearch.ex @@ -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 @@ def meili_get(path) 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 @@ def meili_post(path, params) 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 @@ def meili_put(path, params) do end def meili_delete(path) do - endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url]) + endpoint = Config.get([Pleroma.Search.Meilisearch, :url]) - Pleroma.HTTP.request( - :delete, - Path.join(endpoint, path), - "", - meili_headers(), - [] - ) + with {:ok, _} <- + Pleroma.HTTP.request( + :delete, + Path.join(endpoint, path), + "", + meili_headers(), + [] + ) do + :ok + else + _ -> :error + end end @impl true diff --git a/lib/pleroma/workers/search_indexing_worker.ex b/lib/pleroma/workers/search_indexing_worker.ex index 43b7bad1e..8476a2be5 100644 --- a/lib/pleroma/workers/search_indexing_worker.ex +++ b/lib/pleroma/workers/search_indexing_worker.ex @@ -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 @@ def perform(%Job{args: %{"op" => "add_to_index", "activity" => activity_id}}) 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 diff --git a/test/pleroma/search/meilisearch_test.exs b/test/pleroma/search/meilisearch_test.exs index 04a2d75d9..3a267385d 100644 --- a/test/pleroma/search/meilisearch_test.exs +++ b/test/pleroma/search/meilisearch_test.exs @@ -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 @@ test "indexes a local post on creation" 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 @@ test "doesn't index posts that are not public" do args = %{"op" => "add_to_index", "activity" => activity.id} + Config + |> expect(:get, fn + [Pleroma.Search, :module], nil -> + Meilisearch + end) + assert_enqueued(worker: SearchIndexingWorker, args: args) assert :ok = perform_job(SearchIndexingWorker, args) - - assert_not_called(Meilisearch.meili_put(:_)) end) - - history = call_history(Meilisearch) - assert Enum.count(history) == 2 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 @@ test "deletes posts from index when deleted locally" 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 @@ test "deletes posts from index when deleted locally" 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