Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.7KB

  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.DataCase do
  5. @moduledoc """
  6. This module defines the setup for tests requiring
  7. access to the application's data layer.
  8. You may define functions here to be used as helpers in
  9. your tests.
  10. Finally, if the test case interacts with the database,
  11. it cannot be async. For this reason, every test runs
  12. inside a transaction which is reset at the beginning
  13. of the test unless the test case is marked as async.
  14. """
  15. use ExUnit.CaseTemplate
  16. import Pleroma.Tests.Helpers, only: [clear_config: 2]
  17. using do
  18. quote do
  19. alias Pleroma.Repo
  20. import Ecto
  21. import Ecto.Changeset
  22. import Ecto.Query
  23. import Pleroma.DataCase
  24. use Pleroma.Tests.Helpers
  25. # Sets up OAuth access with specified scopes
  26. defp oauth_access(scopes, opts \\ []) do
  27. user =
  28. Keyword.get_lazy(opts, :user, fn ->
  29. Pleroma.Factory.insert(:user)
  30. end)
  31. token =
  32. Keyword.get_lazy(opts, :oauth_token, fn ->
  33. Pleroma.Factory.insert(:oauth_token, user: user, scopes: scopes)
  34. end)
  35. %{user: user, token: token}
  36. end
  37. end
  38. end
  39. def clear_cachex do
  40. Pleroma.Supervisor
  41. |> Supervisor.which_children()
  42. |> Enum.each(fn
  43. {name, _, _, [Cachex]} ->
  44. name
  45. |> to_string
  46. |> String.trim_leading("cachex_")
  47. |> Kernel.<>("_cache")
  48. |> String.to_existing_atom()
  49. |> Cachex.clear()
  50. _ ->
  51. nil
  52. end)
  53. end
  54. def setup_multi_process_mode(tags) do
  55. :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
  56. if tags[:async] do
  57. Mox.stub_with(Pleroma.CachexMock, Pleroma.NullCache)
  58. Mox.set_mox_private()
  59. else
  60. Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
  61. Mox.set_mox_global()
  62. Mox.stub_with(Pleroma.CachexMock, Pleroma.CachexProxy)
  63. clear_cachex()
  64. end
  65. :ok
  66. end
  67. def setup_streamer(tags) do
  68. if tags[:needs_streamer] do
  69. start_supervised(%{
  70. id: Pleroma.Web.Streamer.registry(),
  71. start:
  72. {Registry, :start_link, [[keys: :duplicate, name: Pleroma.Web.Streamer.registry()]]}
  73. })
  74. end
  75. :ok
  76. end
  77. setup tags do
  78. setup_multi_process_mode(tags)
  79. setup_streamer(tags)
  80. stub_pipeline()
  81. Mox.verify_on_exit!()
  82. :ok
  83. end
  84. def stub_pipeline do
  85. Mox.stub_with(Pleroma.Web.ActivityPub.SideEffectsMock, Pleroma.Web.ActivityPub.SideEffects)
  86. Mox.stub_with(
  87. Pleroma.Web.ActivityPub.ObjectValidatorMock,
  88. Pleroma.Web.ActivityPub.ObjectValidator
  89. )
  90. Mox.stub_with(Pleroma.Web.ActivityPub.MRFMock, Pleroma.Web.ActivityPub.MRF)
  91. Mox.stub_with(Pleroma.Web.ActivityPub.ActivityPubMock, Pleroma.Web.ActivityPub.ActivityPub)
  92. Mox.stub_with(Pleroma.Web.FederatorMock, Pleroma.Web.Federator)
  93. Mox.stub_with(Pleroma.ConfigMock, Pleroma.Config)
  94. end
  95. def ensure_local_uploader(context) do
  96. test_uploader = Map.get(context, :uploader) || Pleroma.Uploaders.Local
  97. clear_config([Pleroma.Upload, :uploader], test_uploader)
  98. clear_config([Pleroma.Upload, :filters], [])
  99. :ok
  100. end
  101. @doc """
  102. A helper that transform changeset errors to a map of messages.
  103. changeset = Accounts.create_user(%{password: "short"})
  104. assert "password is too short" in errors_on(changeset).password
  105. assert %{password: ["password is too short"]} = errors_on(changeset)
  106. """
  107. def errors_on(changeset) do
  108. Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
  109. Enum.reduce(opts, message, fn {key, value}, acc ->
  110. String.replace(acc, "%{#{key}}", to_string(value))
  111. end)
  112. end)
  113. end
  114. end