251388ea2dc85b3db52ab54c0d25fe8fe6c522ef
[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
10 import Pleroma.Factory
11 import Tesla.Mock
12 import Mock
13
14 alias Pleroma.Search.Meilisearch
15 alias Pleroma.Web.CommonAPI
16
17 setup_all do
18 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 :ok
20 end
21
22 describe "meilisearch" do
23 setup do: clear_config([Pleroma.Search, :module], Meilisearch)
24
25 setup_with_mocks(
26 [
27 {Meilisearch, [:passthrough],
28 [
29 add_to_index: fn a -> passthrough([a]) end,
30 remove_from_index: fn a -> passthrough([a]) end
31 ]}
32 ],
33 context,
34 do: {:ok, context}
35 )
36
37 test "indexes a local post on creation" do
38 user = insert(:user)
39
40 mock_global(fn
41 %{method: :post, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
42 assert match?(
43 [%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
44 Jason.decode!(body)
45 )
46
47 json(%{updateId: 1})
48 end)
49
50 {:ok, activity} =
51 CommonAPI.post(user, %{
52 status: "guys i just don't wanna leave the swamp",
53 visibility: "public"
54 })
55
56 assert_called(Meilisearch.add_to_index(activity))
57 end
58
59 test "doesn't index posts that are not public" do
60 user = insert(:user)
61
62 Enum.each(["unlisted", "private", "direct"], fn visiblity ->
63 {:ok, _} =
64 CommonAPI.post(user, %{
65 status: "guys i just don't wanna leave the swamp",
66 visibility: visiblity
67 })
68 end)
69
70 history = call_history(Meilisearch)
71 assert Enum.count(history) == 3
72
73 Enum.each(history, fn {_, _, return} ->
74 assert is_nil(return)
75 end)
76 end
77
78 test "deletes posts from index when deleted locally" do
79 user = insert(:user)
80
81 mock_global(fn
82 %{method: :post, url: "http://127.0.0.1:7700/indexes/objects/documents", body: body} ->
83 assert match?(
84 [%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
85 Jason.decode!(body)
86 )
87
88 json(%{updateId: 1})
89
90 %{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
91 assert String.length(id) > 1
92 json(%{updateId: 2})
93 end)
94
95 {:ok, activity} =
96 CommonAPI.post(user, %{
97 status: "guys i just don't wanna leave the swamp",
98 visibility: "public"
99 })
100
101 assert_called(Meilisearch.add_to_index(activity))
102
103 {:ok, _} = CommonAPI.delete(activity.id, user)
104
105 assert_called(Meilisearch.remove_from_index(:_))
106 end
107 end
108 end