2021-08-23 20:52:21 +00:00
|
|
|
# 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
|
|
|
|
|
2023-11-12 10:49:50 +00:00
|
|
|
use Pleroma.DataCase, async: true
|
2022-01-22 13:31:32 +00:00
|
|
|
use Oban.Testing, repo: Pleroma.Repo
|
2021-08-23 20:52:21 +00:00
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
import Tesla.Mock
|
2023-11-12 10:49:50 +00:00
|
|
|
import Mox
|
2021-08-23 20:52:21 +00:00
|
|
|
|
|
|
|
alias Pleroma.Search.Meilisearch
|
2023-11-12 13:13:27 +00:00
|
|
|
alias Pleroma.UnstubbedConfigMock, as: Config
|
2021-11-14 18:42:18 +00:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2022-01-22 13:31:32 +00:00
|
|
|
alias Pleroma.Workers.SearchIndexingWorker
|
2021-08-23 20:52:21 +00:00
|
|
|
|
|
|
|
describe "meilisearch" do
|
|
|
|
test "indexes a local post on creation" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
2023-11-12 10:49:50 +00:00
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{
|
|
|
|
method: :put,
|
|
|
|
url: "http://127.0.0.1:7700/indexes/objects/documents",
|
|
|
|
body: body
|
|
|
|
} ->
|
2021-08-23 20:52:21 +00:00
|
|
|
assert match?(
|
|
|
|
[%{"content" => "guys i just don't wanna leave the swamp"}],
|
|
|
|
Jason.decode!(body)
|
|
|
|
)
|
|
|
|
|
2023-11-12 10:49:50 +00:00
|
|
|
# To make sure that the worker is called
|
|
|
|
send(self(), "posted_to_meilisearch")
|
2023-11-12 12:43:50 +00:00
|
|
|
|
|
|
|
%{
|
|
|
|
"enqueuedAt" => "2023-11-12T12:36:46.927517Z",
|
|
|
|
"indexUid" => "objects",
|
|
|
|
"status" => "enqueued",
|
|
|
|
"taskUid" => 6,
|
|
|
|
"type" => "documentAdditionOrUpdate"
|
|
|
|
}
|
|
|
|
|> json()
|
2021-08-23 20:52:21 +00:00
|
|
|
end)
|
|
|
|
|
2023-11-12 10:49:50 +00:00
|
|
|
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)
|
|
|
|
|
2021-08-23 20:52:21 +00:00
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
|
|
|
status: "guys i just don't wanna leave the swamp",
|
|
|
|
visibility: "public"
|
|
|
|
})
|
|
|
|
|
2022-01-22 13:31:32 +00:00
|
|
|
args = %{"op" => "add_to_index", "activity" => activity.id}
|
|
|
|
|
|
|
|
assert_enqueued(
|
|
|
|
worker: SearchIndexingWorker,
|
|
|
|
args: args
|
|
|
|
)
|
|
|
|
|
|
|
|
assert :ok = perform_job(SearchIndexingWorker, args)
|
2023-11-12 10:49:50 +00:00
|
|
|
assert_received("posted_to_meilisearch")
|
2021-08-23 20:52:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "doesn't index posts that are not public" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
2022-01-22 18:09:53 +00:00
|
|
|
Enum.each(["private", "direct"], fn visibility ->
|
2022-01-22 13:31:32 +00:00
|
|
|
{:ok, activity} =
|
2021-08-23 20:52:21 +00:00
|
|
|
CommonAPI.post(user, %{
|
|
|
|
status: "guys i just don't wanna leave the swamp",
|
2022-01-22 18:09:53 +00:00
|
|
|
visibility: visibility
|
2021-08-23 20:52:21 +00:00
|
|
|
})
|
2022-01-22 13:31:32 +00:00
|
|
|
|
2022-01-22 18:09:53 +00:00
|
|
|
args = %{"op" => "add_to_index", "activity" => activity.id}
|
|
|
|
|
2023-11-12 10:49:50 +00:00
|
|
|
Config
|
|
|
|
|> expect(:get, fn
|
|
|
|
[Pleroma.Search, :module], nil ->
|
|
|
|
Meilisearch
|
|
|
|
end)
|
|
|
|
|
2022-01-22 18:09:53 +00:00
|
|
|
assert_enqueued(worker: SearchIndexingWorker, args: args)
|
|
|
|
assert :ok = perform_job(SearchIndexingWorker, args)
|
2021-08-23 20:52:21 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "deletes posts from index when deleted locally" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
2023-11-12 10:49:50 +00:00
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{
|
|
|
|
method: :put,
|
|
|
|
url: "http://127.0.0.1:7700/indexes/objects/documents",
|
|
|
|
body: body
|
|
|
|
} ->
|
2021-08-23 20:52:21 +00:00
|
|
|
assert match?(
|
|
|
|
[%{"content" => "guys i just don't wanna leave the swamp"}],
|
|
|
|
Jason.decode!(body)
|
|
|
|
)
|
|
|
|
|
2023-11-12 12:43:50 +00:00
|
|
|
%{
|
|
|
|
"enqueuedAt" => "2023-11-12T12:36:46.927517Z",
|
|
|
|
"indexUid" => "objects",
|
|
|
|
"status" => "enqueued",
|
|
|
|
"taskUid" => 6,
|
|
|
|
"type" => "documentAdditionOrUpdate"
|
|
|
|
}
|
|
|
|
|> json()
|
2021-08-23 20:52:21 +00:00
|
|
|
|
|
|
|
%{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
|
2023-11-12 10:49:50 +00:00
|
|
|
send(self(), "called_delete")
|
2021-08-23 20:52:21 +00:00
|
|
|
assert String.length(id) > 1
|
2023-11-12 12:43:50 +00:00
|
|
|
json(%{})
|
2021-08-23 20:52:21 +00:00
|
|
|
end)
|
|
|
|
|
2023-11-12 10:49:50 +00:00
|
|
|
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)
|
|
|
|
|
2021-08-23 20:52:21 +00:00
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(user, %{
|
|
|
|
status: "guys i just don't wanna leave the swamp",
|
|
|
|
visibility: "public"
|
|
|
|
})
|
|
|
|
|
2022-01-22 13:31:32 +00:00
|
|
|
args = %{"op" => "add_to_index", "activity" => activity.id}
|
|
|
|
assert_enqueued(worker: SearchIndexingWorker, args: args)
|
|
|
|
assert :ok = perform_job(SearchIndexingWorker, args)
|
2021-08-23 20:52:21 +00:00
|
|
|
|
|
|
|
{:ok, _} = CommonAPI.delete(activity.id, user)
|
|
|
|
|
2022-01-22 13:31:32 +00:00
|
|
|
delete_args = %{"op" => "remove_from_index", "object" => activity.object.id}
|
|
|
|
assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
|
|
|
|
assert :ok = perform_job(SearchIndexingWorker, delete_args)
|
|
|
|
|
2023-11-12 10:49:50 +00:00
|
|
|
assert_received("called_delete")
|
2021-08-23 20:52:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|