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.

107 lines
2.6KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Tests.Helpers do
  5. @moduledoc """
  6. Helpers for use in tests.
  7. """
  8. alias Pleroma.Config
  9. defmacro clear_config(config_path) do
  10. quote do
  11. clear_config(unquote(config_path)) do
  12. end
  13. end
  14. end
  15. defmacro clear_config(config_path, do: yield) do
  16. quote do
  17. setup do
  18. initial_setting = Config.get(unquote(config_path))
  19. unquote(yield)
  20. on_exit(fn -> Config.put(unquote(config_path), initial_setting) end)
  21. :ok
  22. end
  23. end
  24. end
  25. defmacro clear_config_all(config_path) do
  26. quote do
  27. clear_config_all(unquote(config_path)) do
  28. end
  29. end
  30. end
  31. defmacro clear_config_all(config_path, do: yield) do
  32. quote do
  33. setup_all do
  34. initial_setting = Config.get(unquote(config_path))
  35. unquote(yield)
  36. on_exit(fn -> Config.put(unquote(config_path), initial_setting) end)
  37. :ok
  38. end
  39. end
  40. end
  41. defmacro __using__(_opts) do
  42. quote do
  43. import Pleroma.Tests.Helpers,
  44. only: [
  45. clear_config: 1,
  46. clear_config: 2,
  47. clear_config_all: 1,
  48. clear_config_all: 2
  49. ]
  50. def collect_ids(collection) do
  51. collection
  52. |> Enum.map(& &1.id)
  53. |> Enum.sort()
  54. end
  55. def refresh_record(%{id: id, __struct__: model} = _),
  56. do: refresh_record(model, %{id: id})
  57. def refresh_record(model, %{id: id} = _) do
  58. Pleroma.Repo.get_by(model, id: id)
  59. end
  60. # Used for comparing json rendering during tests.
  61. def render_json(view, template, assigns) do
  62. assigns = Map.new(assigns)
  63. view.render(template, assigns)
  64. |> Poison.encode!()
  65. |> Poison.decode!()
  66. end
  67. def stringify_keys(nil), do: nil
  68. def stringify_keys(key) when key in [true, false], do: key
  69. def stringify_keys(key) when is_atom(key), do: Atom.to_string(key)
  70. def stringify_keys(map) when is_map(map) do
  71. map
  72. |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
  73. |> Enum.into(%{})
  74. end
  75. def stringify_keys([head | rest] = list) when is_list(list) do
  76. [stringify_keys(head) | stringify_keys(rest)]
  77. end
  78. def stringify_keys(key), do: key
  79. defmacro guards_config(config_path) do
  80. quote do
  81. initial_setting = Config.get(config_path)
  82. Config.put(config_path, true)
  83. on_exit(fn -> Config.put(config_path, initial_setting) end)
  84. end
  85. end
  86. end
  87. end
  88. end