Merge pull request 'Spin off imports into n oban jobs' (#319) from spin-off-imports...
[akkoma] / test / pleroma / search / elasticsearch_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Search.ElasticsearchTest do
6 require Pleroma.Constants
7
8 use Pleroma.DataCase
9 use Oban.Testing, repo: Pleroma.Repo
10
11 import Pleroma.Factory
12 import Tesla.Mock
13 import Mock
14
15 alias Pleroma.Web.CommonAPI
16 alias Pleroma.Workers.SearchIndexingWorker
17
18 describe "elasticsearch" do
19 setup do
20 clear_config([Pleroma.Search, :module], Pleroma.Search.Elasticsearch)
21 clear_config([Pleroma.Search.Elasticsearch.Cluster, :api], Pleroma.ElasticsearchMock)
22 end
23
24 setup_with_mocks(
25 [
26 {Pleroma.Search.Elasticsearch, [:passthrough],
27 [
28 add_to_index: fn a -> passthrough([a]) end,
29 remove_from_index: fn a -> passthrough([a]) end
30 ]},
31 {Elasticsearch, [:passthrough],
32 [
33 put_document: fn _, _, _ -> :ok end,
34 delete_document: fn _, _, _ -> :ok end
35 ]}
36 ],
37 context,
38 do: {:ok, context}
39 )
40
41 test "indexes a local post on creation" do
42 user = insert(:user)
43
44 {:ok, activity} =
45 CommonAPI.post(user, %{
46 status: "guys i just don't wanna leave the swamp",
47 visibility: "public"
48 })
49
50 args = %{"op" => "add_to_index", "activity" => activity.id}
51
52 assert_enqueued(
53 worker: SearchIndexingWorker,
54 args: args
55 )
56
57 assert :ok = perform_job(SearchIndexingWorker, args)
58
59 assert_called(Pleroma.Search.Elasticsearch.add_to_index(activity))
60 end
61
62 test "doesn't index posts that are not public" do
63 user = insert(:user)
64
65 Enum.each(["private", "direct"], fn visibility ->
66 {:ok, activity} =
67 CommonAPI.post(user, %{
68 status: "guys i just don't wanna leave the swamp",
69 visibility: visibility
70 })
71
72 args = %{"op" => "add_to_index", "activity" => activity.id}
73
74 assert_enqueued(worker: SearchIndexingWorker, args: args)
75 assert :ok = perform_job(SearchIndexingWorker, args)
76
77 assert_not_called(Elasticsearch.put_document(:_))
78 end)
79
80 history = call_history(Pleroma.Search.Elasticsearch)
81 assert Enum.count(history) == 2
82 end
83
84 test "deletes posts from index when deleted locally" do
85 user = insert(:user)
86
87 mock_global(fn
88 %{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
89 assert match?(
90 [%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
91 Jason.decode!(body)
92 )
93
94 json(%{updateId: 1})
95
96 %{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
97 assert String.length(id) > 1
98 json(%{updateId: 2})
99 end)
100
101 {:ok, activity} =
102 CommonAPI.post(user, %{
103 status: "guys i just don't wanna leave the swamp",
104 visibility: "public"
105 })
106
107 args = %{"op" => "add_to_index", "activity" => activity.id}
108 assert_enqueued(worker: SearchIndexingWorker, args: args)
109 assert :ok = perform_job(SearchIndexingWorker, args)
110
111 {:ok, _} = CommonAPI.delete(activity.id, user)
112
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)
116
117 assert_called(Pleroma.Search.Elasticsearch.remove_from_index(:_))
118 end
119 end
120 end