1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Search.MeilisearchTest do
6 require Pleroma.Constants
9 use Oban.Testing, repo: Pleroma.Repo
11 import Pleroma.Factory
15 alias Pleroma.Search.Meilisearch
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Workers.SearchIndexingWorker
20 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
24 describe "meilisearch" do
25 setup do: clear_config([Pleroma.Search, :module], Meilisearch)
29 {Meilisearch, [:passthrough],
31 add_to_index: fn a -> passthrough([a]) end,
32 remove_from_index: fn a -> passthrough([a]) end,
33 meili_put: fn u, a -> passthrough([u, a]) end
40 test "indexes a local post on creation" do
44 %{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
46 [%{"content" => "guys i just don't wanna leave the swamp"}],
54 CommonAPI.post(user, %{
55 status: "guys i just don't wanna leave the swamp",
59 args = %{"op" => "add_to_index", "activity" => activity.id}
62 worker: SearchIndexingWorker,
66 assert :ok = perform_job(SearchIndexingWorker, args)
68 assert_called(Meilisearch.add_to_index(activity))
71 test "doesn't index posts that are not public" do
74 Enum.each(["private", "direct"], fn visibility ->
76 CommonAPI.post(user, %{
77 status: "guys i just don't wanna leave the swamp",
78 visibility: visibility
81 args = %{"op" => "add_to_index", "activity" => activity.id}
83 assert_enqueued(worker: SearchIndexingWorker, args: args)
84 assert :ok = perform_job(SearchIndexingWorker, args)
86 assert_not_called(Meilisearch.meili_put(:_))
89 history = call_history(Meilisearch)
90 assert Enum.count(history) == 2
93 test "deletes posts from index when deleted locally" do
97 %{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
99 [%{"content" => "guys i just don't wanna leave the swamp"}],
105 %{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
106 assert String.length(id) > 1
111 CommonAPI.post(user, %{
112 status: "guys i just don't wanna leave the swamp",
116 args = %{"op" => "add_to_index", "activity" => activity.id}
117 assert_enqueued(worker: SearchIndexingWorker, args: args)
118 assert :ok = perform_job(SearchIndexingWorker, args)
120 {:ok, _} = CommonAPI.delete(activity.id, user)
122 delete_args = %{"op" => "remove_from_index", "object" => activity.object.id}
123 assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
124 assert :ok = perform_job(SearchIndexingWorker, delete_args)
126 assert_called(Meilisearch.remove_from_index(:_))