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.

100 lines
2.7KB

  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. # Sets up OAuth access with specified scopes
  25. defp oauth_access(scopes, opts \\ []) do
  26. user =
  27. Keyword.get_lazy(opts, :user, fn ->
  28. Pleroma.Factory.insert(:user)
  29. end)
  30. token =
  31. Keyword.get_lazy(opts, :oauth_token, fn ->
  32. Pleroma.Factory.insert(:oauth_token, user: user, scopes: scopes)
  33. end)
  34. %{user: user, token: token}
  35. end
  36. end
  37. end
  38. setup tags do
  39. Cachex.clear(:user_cache)
  40. Cachex.clear(:object_cache)
  41. :ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
  42. unless tags[:async] do
  43. Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
  44. end
  45. if tags[:needs_streamer] do
  46. start_supervised(%{
  47. id: Pleroma.Web.Streamer.registry(),
  48. start:
  49. {Registry, :start_link, [[keys: :duplicate, name: Pleroma.Web.Streamer.registry()]]}
  50. })
  51. end
  52. :ok
  53. end
  54. def ensure_local_uploader(context) do
  55. test_uploader = Map.get(context, :uploader, Pleroma.Uploaders.Local)
  56. uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
  57. filters = Pleroma.Config.get([Pleroma.Upload, :filters])
  58. Pleroma.Config.put([Pleroma.Upload, :uploader], test_uploader)
  59. Pleroma.Config.put([Pleroma.Upload, :filters], [])
  60. on_exit(fn ->
  61. Pleroma.Config.put([Pleroma.Upload, :uploader], uploader)
  62. Pleroma.Config.put([Pleroma.Upload, :filters], filters)
  63. end)
  64. :ok
  65. end
  66. @doc """
  67. A helper that transform changeset errors to a map of messages.
  68. changeset = Accounts.create_user(%{password: "short"})
  69. assert "password is too short" in errors_on(changeset).password
  70. assert %{password: ["password is too short"]} = errors_on(changeset)
  71. """
  72. def errors_on(changeset) do
  73. Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
  74. Enum.reduce(opts, message, fn {key, value}, acc ->
  75. String.replace(acc, "%{#{key}}", to_string(value))
  76. end)
  77. end)
  78. end
  79. end