aboutsummaryrefslogtreecommitdiff
path: root/test/pleroma/search/meilisearch_test.exs
blob: eea4543232ed68bbd468051f7e9a879524147b77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Search.MeilisearchTest do
  require Pleroma.Constants

  use Pleroma.DataCase, async: true
  use Oban.Testing, repo: Pleroma.Repo

  import Pleroma.Factory
  import Tesla.Mock
  import Mox

  alias Pleroma.Search.Meilisearch
  alias Pleroma.UnstubbedConfigMock, as: Config
  alias Pleroma.Web.CommonAPI
  alias Pleroma.Workers.SearchIndexingWorker

  describe "meilisearch" do
    test "indexes a local post on creation" do
      user = insert(:user)

      Tesla.Mock.mock(fn
        %{
          method: :put,
          url: "http://127.0.0.1:7700/indexes/objects/documents",
          body: body
        } ->
          assert match?(
                   [%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
                   Jason.decode!(body)
                 )

          # To make sure that the worker is called
          send(self(), "posted_to_meilisearch")

          %{
            "enqueuedAt" => "2023-11-12T12:36:46.927517Z",
            "indexUid" => "objects",
            "status" => "enqueued",
            "taskUid" => 6,
            "type" => "documentAdditionOrUpdate"
          }
          |> json()
      end)

      Config
      |> expect(:get, 3, fn
        [Pleroma.Search, :module], nil ->
          Meilisearch

        [Pleroma.Search.Meilisearch, :url], nil ->
          "http://127.0.0.1:7700"

        [Pleroma.Search.Meilisearch, :private_key], nil ->
          "secret"
      end)

      {:ok, activity} =
        CommonAPI.post(user, %{
          status: "guys i just don't wanna leave the swamp",
          visibility: "public"
        })

      args = %{"op" => "add_to_index", "activity" => activity.id}

      assert_enqueued(
        worker: SearchIndexingWorker,
        args: args
      )

      assert :ok = perform_job(SearchIndexingWorker, args)
      assert_received("posted_to_meilisearch")
    end

    test "doesn't index posts that are not public" do
      user = insert(:user)

      Enum.each(["private", "direct"], fn visibility ->
        {:ok, activity} =
          CommonAPI.post(user, %{
            status: "guys i just don't wanna leave the swamp",
            visibility: visibility
          })

        args = %{"op" => "add_to_index", "activity" => activity.id}

        Config
        |> expect(:get, fn
          [Pleroma.Search, :module], nil ->
            Meilisearch
        end)

        assert_enqueued(worker: SearchIndexingWorker, args: args)
        assert :ok = perform_job(SearchIndexingWorker, args)
      end)
    end

    test "deletes posts from index when deleted locally" do
      user = insert(:user)

      Tesla.Mock.mock(fn
        %{
          method: :put,
          url: "http://127.0.0.1:7700/indexes/objects/documents",
          body: body
        } ->
          assert match?(
                   [%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
                   Jason.decode!(body)
                 )

          %{
            "enqueuedAt" => "2023-11-12T12:36:46.927517Z",
            "indexUid" => "objects",
            "status" => "enqueued",
            "taskUid" => 6,
            "type" => "documentAdditionOrUpdate"
          }
          |> json()

        %{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
          send(self(), "called_delete")
          assert String.length(id) > 1
          json(%{})
      end)

      Config
      |> expect(:get, 6, fn
        [Pleroma.Search, :module], nil ->
          Meilisearch

        [Pleroma.Search.Meilisearch, :url], nil ->
          "http://127.0.0.1:7700"

        [Pleroma.Search.Meilisearch, :private_key], nil ->
          "secret"
      end)

      {:ok, activity} =
        CommonAPI.post(user, %{
          status: "guys i just don't wanna leave the swamp",
          visibility: "public"
        })

      args = %{"op" => "add_to_index", "activity" => activity.id}
      assert_enqueued(worker: SearchIndexingWorker, args: args)
      assert :ok = perform_job(SearchIndexingWorker, args)

      {:ok, _} = CommonAPI.delete(activity.id, user)

      delete_args = %{"op" => "remove_from_index", "object" => activity.object.id}
      assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
      assert :ok = perform_job(SearchIndexingWorker, delete_args)

      assert_received("called_delete")
    end
  end
end