logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

meilisearch_test.exs (4525B)


  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. defmodule Pleroma.Search.MeilisearchTest do
  5. require Pleroma.Constants
  6. use Pleroma.DataCase, async: true
  7. use Oban.Testing, repo: Pleroma.Repo
  8. import Pleroma.Factory
  9. import Tesla.Mock
  10. import Mox
  11. alias Pleroma.Search.Meilisearch
  12. alias Pleroma.UnstubbedConfigMock, as: Config
  13. alias Pleroma.Web.CommonAPI
  14. alias Pleroma.Workers.SearchIndexingWorker
  15. describe "meilisearch" do
  16. test "indexes a local post on creation" do
  17. user = insert(:user)
  18. Tesla.Mock.mock(fn
  19. %{
  20. method: :put,
  21. url: "http://127.0.0.1:7700/indexes/objects/documents",
  22. body: body
  23. } ->
  24. assert match?(
  25. [%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
  26. Jason.decode!(body)
  27. )
  28. # To make sure that the worker is called
  29. send(self(), "posted_to_meilisearch")
  30. %{
  31. "enqueuedAt" => "2023-11-12T12:36:46.927517Z",
  32. "indexUid" => "objects",
  33. "status" => "enqueued",
  34. "taskUid" => 6,
  35. "type" => "documentAdditionOrUpdate"
  36. }
  37. |> json()
  38. end)
  39. Config
  40. |> expect(:get, 3, fn
  41. [Pleroma.Search, :module], nil ->
  42. Meilisearch
  43. [Pleroma.Search.Meilisearch, :url], nil ->
  44. "http://127.0.0.1:7700"
  45. [Pleroma.Search.Meilisearch, :private_key], nil ->
  46. "secret"
  47. end)
  48. {:ok, activity} =
  49. CommonAPI.post(user, %{
  50. status: "guys i just don't wanna leave the swamp",
  51. visibility: "public"
  52. })
  53. args = %{"op" => "add_to_index", "activity" => activity.id}
  54. assert_enqueued(
  55. worker: SearchIndexingWorker,
  56. args: args
  57. )
  58. assert :ok = perform_job(SearchIndexingWorker, args)
  59. assert_received("posted_to_meilisearch")
  60. end
  61. test "doesn't index posts that are not public" do
  62. user = insert(:user)
  63. Enum.each(["private", "direct"], fn visibility ->
  64. {:ok, activity} =
  65. CommonAPI.post(user, %{
  66. status: "guys i just don't wanna leave the swamp",
  67. visibility: visibility
  68. })
  69. args = %{"op" => "add_to_index", "activity" => activity.id}
  70. Config
  71. |> expect(:get, fn
  72. [Pleroma.Search, :module], nil ->
  73. Meilisearch
  74. end)
  75. assert_enqueued(worker: SearchIndexingWorker, args: args)
  76. assert :ok = perform_job(SearchIndexingWorker, args)
  77. end)
  78. end
  79. test "deletes posts from index when deleted locally" do
  80. user = insert(:user)
  81. Tesla.Mock.mock(fn
  82. %{
  83. method: :put,
  84. url: "http://127.0.0.1:7700/indexes/objects/documents",
  85. body: body
  86. } ->
  87. assert match?(
  88. [%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
  89. Jason.decode!(body)
  90. )
  91. %{
  92. "enqueuedAt" => "2023-11-12T12:36:46.927517Z",
  93. "indexUid" => "objects",
  94. "status" => "enqueued",
  95. "taskUid" => 6,
  96. "type" => "documentAdditionOrUpdate"
  97. }
  98. |> json()
  99. %{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
  100. send(self(), "called_delete")
  101. assert String.length(id) > 1
  102. json(%{})
  103. end)
  104. Config
  105. |> expect(:get, 6, fn
  106. [Pleroma.Search, :module], nil ->
  107. Meilisearch
  108. [Pleroma.Search.Meilisearch, :url], nil ->
  109. "http://127.0.0.1:7700"
  110. [Pleroma.Search.Meilisearch, :private_key], nil ->
  111. "secret"
  112. end)
  113. {:ok, activity} =
  114. CommonAPI.post(user, %{
  115. status: "guys i just don't wanna leave the swamp",
  116. visibility: "public"
  117. })
  118. args = %{"op" => "add_to_index", "activity" => activity.id}
  119. assert_enqueued(worker: SearchIndexingWorker, args: args)
  120. assert :ok = perform_job(SearchIndexingWorker, args)
  121. {:ok, _} = CommonAPI.delete(activity.id, user)
  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. assert_received("called_delete")
  126. end
  127. end
  128. end