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.ElasticsearchTest do
6 require Pleroma.Constants
9 use Oban.Testing, repo: Pleroma.Repo
11 import Pleroma.Factory
15 alias Pleroma.Web.CommonAPI
16 alias Pleroma.Workers.SearchIndexingWorker
18 describe "elasticsearch" do
20 clear_config([Pleroma.Search, :module], Pleroma.Search.Elasticsearch)
21 clear_config([Pleroma.Search.Elasticsearch.Cluster, :api], Pleroma.ElasticsearchMock)
26 {Pleroma.Search.Elasticsearch, [:passthrough],
28 add_to_index: fn a -> passthrough([a]) end,
29 remove_from_index: fn a -> passthrough([a]) end
31 {Elasticsearch, [:passthrough],
33 put_document: fn _, _, _ -> :ok end,
34 delete_document: fn _, _, _ -> :ok end
41 test "indexes a local post on creation" do
45 CommonAPI.post(user, %{
46 status: "guys i just don't wanna leave the swamp",
50 args = %{"op" => "add_to_index", "activity" => activity.id}
53 worker: SearchIndexingWorker,
57 assert :ok = perform_job(SearchIndexingWorker, args)
59 assert_called(Pleroma.Search.Elasticsearch.add_to_index(activity))
62 test "doesn't index posts that are not public" do
65 Enum.each(["private", "direct"], fn visibility ->
67 CommonAPI.post(user, %{
68 status: "guys i just don't wanna leave the swamp",
69 visibility: visibility
72 args = %{"op" => "add_to_index", "activity" => activity.id}
74 assert_enqueued(worker: SearchIndexingWorker, args: args)
75 assert :ok = perform_job(SearchIndexingWorker, args)
77 assert_not_called(Elasticsearch.put_document(:_))
80 history = call_history(Pleroma.Search.Elasticsearch)
81 assert Enum.count(history) == 2
84 test "deletes posts from index when deleted locally" do
88 %{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
90 [%{"content" => "guys i just don't wanna leave the swamp"}],
96 %{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
97 assert String.length(id) > 1
102 CommonAPI.post(user, %{
103 status: "guys i just don't wanna leave the swamp",
107 args = %{"op" => "add_to_index", "activity" => activity.id}
108 assert_enqueued(worker: SearchIndexingWorker, args: args)
109 assert :ok = perform_job(SearchIndexingWorker, args)
111 {:ok, _} = CommonAPI.delete(activity.id, user)
113 delete_args = %{"op" => "remove_from_index", "object" => activity.object.id}
114 assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
115 assert :ok = perform_job(SearchIndexingWorker, delete_args)
117 assert_called(Pleroma.Search.Elasticsearch.remove_from_index(:_))