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.

85 lines
2.3KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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. using do
  17. quote do
  18. alias Pleroma.Repo
  19. import Ecto
  20. import Ecto.Changeset
  21. import Ecto.Query
  22. import Pleroma.DataCase
  23. use Pleroma.Tests.Helpers
  24. end
  25. end
  26. setup tags do
  27. Cachex.clear(:user_cache)
  28. Cachex.clear(:object_cache)
  29. :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
  30. unless tags[:async] do
  31. Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
  32. end
  33. if tags[:needs_streamer] do
  34. start_supervised(%{
  35. id: Pleroma.Web.Streamer.registry(),
  36. start:
  37. {Registry, :start_link, [[keys: :duplicate, name: Pleroma.Web.Streamer.registry()]]}
  38. })
  39. end
  40. :ok
  41. end
  42. def ensure_local_uploader(context) do
  43. test_uploader = Map.get(context, :uploader, Pleroma.Uploaders.Local)
  44. uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
  45. filters = Pleroma.Config.get([Pleroma.Upload, :filters])
  46. Pleroma.Config.put([Pleroma.Upload, :uploader], test_uploader)
  47. Pleroma.Config.put([Pleroma.Upload, :filters], [])
  48. on_exit(fn ->
  49. Pleroma.Config.put([Pleroma.Upload, :uploader], uploader)
  50. Pleroma.Config.put([Pleroma.Upload, :filters], filters)
  51. end)
  52. :ok
  53. end
  54. @doc """
  55. A helper that transform changeset errors to a map of messages.
  56. changeset = Accounts.create_user(%{password: "short"})
  57. assert "password is too short" in errors_on(changeset).password
  58. assert %{password: ["password is too short"]} = errors_on(changeset)
  59. """
  60. def errors_on(changeset) do
  61. Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
  62. Enum.reduce(opts, message, fn {key, value}, acc ->
  63. String.replace(acc, "%{#{key}}", to_string(value))
  64. end)
  65. end)
  66. end
  67. end