Fix meilisearch tests and jobs for oban
[akkoma] / test / pleroma / search / meilisearch_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.MeilisearchTest 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.Search.Meilisearch
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Workers.SearchIndexingWorker
18
19 setup_all do
20 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 :ok
22 end
23
24 describe "meilisearch" do
25 setup do: clear_config([Pleroma.Search, :module], Meilisearch)
26
27 setup_with_mocks(
28 [
29 {Meilisearch, [:passthrough],
30 [
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
34 ]}
35 ],
36 context,
37 do: {:ok, context}
38 )
39
40 test "indexes a local post on creation" do
41 user = insert(:user)
42
43 mock_global(fn
44 %{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
45 assert match?(
46 [%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
47 Jason.decode!(body)
48 )
49
50 json(%{updateId: 1})
51 end)
52
53 {:ok, activity} =
54 CommonAPI.post(user, %{
55 status: "guys i just don't wanna leave the swamp",
56 visibility: "public"
57 })
58
59 args = %{"op" => "add_to_index", "activity" => activity.id}
60
61 assert_enqueued(
62 worker: SearchIndexingWorker,
63 args: args
64 )
65
66 assert :ok = perform_job(SearchIndexingWorker, args)
67
68 assert_called(Meilisearch.add_to_index(activity))
69 end
70
71 test "doesn't index posts that are not public" do
72 user = insert(:user)
73
74 Enum.each(["unlisted", "private", "direct"], fn visiblity ->
75 {:ok, activity} =
76 CommonAPI.post(user, %{
77 status: "guys i just don't wanna leave the swamp",
78 visibility: visiblity
79 })
80
81 Meilisearch.add_to_index(activity)
82 assert_not_called(Meilisearch.meili_put(:_))
83 end)
84
85 history = call_history(Meilisearch)
86 assert Enum.count(history) == 3
87 end
88
89 test "deletes posts from index when deleted locally" do
90 user = insert(:user)
91
92 mock_global(fn
93 %{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
94 assert match?(
95 [%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
96 Jason.decode!(body)
97 )
98
99 json(%{updateId: 1})
100
101 %{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
102 assert String.length(id) > 1
103 json(%{updateId: 2})
104 end)
105
106 {:ok, activity} =
107 CommonAPI.post(user, %{
108 status: "guys i just don't wanna leave the swamp",
109 visibility: "public"
110 })
111
112 args = %{"op" => "add_to_index", "activity" => activity.id}
113 assert_enqueued(worker: SearchIndexingWorker, args: args)
114 assert :ok = perform_job(SearchIndexingWorker, args)
115
116 {:ok, _} = CommonAPI.delete(activity.id, user)
117
118 delete_args = %{"op" => "remove_from_index", "object" => activity.object.id}
119 assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
120 assert :ok = perform_job(SearchIndexingWorker, delete_args)
121
122 assert_called(Meilisearch.remove_from_index(:_))
123 end
124 end
125 end