fix csp-induced HTML match error
[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(%{taskUid: 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(["private", "direct"], fn visibility ->
75 {:ok, activity} =
76 CommonAPI.post(user, %{
77 status: "guys i just don't wanna leave the swamp",
78 visibility: visibility
79 })
80
81 args = %{"op" => "add_to_index", "activity" => activity.id}
82
83 assert_enqueued(worker: SearchIndexingWorker, args: args)
84 assert :ok = perform_job(SearchIndexingWorker, args)
85
86 assert_not_called(Meilisearch.meili_put(:_))
87 end)
88
89 history = call_history(Meilisearch)
90 assert Enum.count(history) == 2
91 end
92
93 test "deletes posts from index when deleted locally" do
94 user = insert(:user)
95
96 mock_global(fn
97 %{method: :put, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
98 assert match?(
99 [%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
100 Jason.decode!(body)
101 )
102
103 json(%{taskUid: 1})
104
105 %{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
106 assert String.length(id) > 1
107 json(%{taskUid: 2})
108 end)
109
110 {:ok, activity} =
111 CommonAPI.post(user, %{
112 status: "guys i just don't wanna leave the swamp",
113 visibility: "public"
114 })
115
116 args = %{"op" => "add_to_index", "activity" => activity.id}
117 assert_enqueued(worker: SearchIndexingWorker, args: args)
118 assert :ok = perform_job(SearchIndexingWorker, args)
119
120 {:ok, _} = CommonAPI.delete(activity.id, user)
121
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)
125
126 assert_called(Meilisearch.remove_from_index(:_))
127 end
128 end
129 end