logo

pleroma

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

helpers.ex (4484B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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. require Logger
  10. @doc "Accepts two URLs/URIs and sorts the query parameters before comparing"
  11. def uri_equal?(a, b) do
  12. a_sorted = uri_query_sort(a)
  13. b_sorted = uri_query_sort(b)
  14. match?(^a_sorted, b_sorted)
  15. end
  16. @doc "Accepts a URL/URI and sorts the query parameters"
  17. def uri_query_sort(uri) do
  18. parsed = URI.parse(uri)
  19. sorted_query =
  20. String.split(parsed.query, "&")
  21. |> Enum.sort()
  22. |> Enum.join("&")
  23. parsed
  24. |> Map.put(:query, sorted_query)
  25. |> URI.to_string()
  26. end
  27. @doc "Returns the value of the specified query parameter for the provided URL"
  28. def get_query_parameter(url, param) do
  29. url
  30. |> URI.parse()
  31. |> Map.get(:query)
  32. |> URI.query_decoder()
  33. |> Enum.to_list()
  34. |> Enum.into(%{}, fn {x, y} -> {x, y} end)
  35. |> Map.get(param)
  36. end
  37. defmacro clear_config(config_path) do
  38. quote do
  39. clear_config(unquote(config_path)) do
  40. end
  41. end
  42. end
  43. defmacro clear_config(config_path, do: yield) do
  44. quote do
  45. initial_setting = Config.fetch(unquote(config_path))
  46. unquote(yield)
  47. on_exit(fn ->
  48. case initial_setting do
  49. :error ->
  50. Config.delete(unquote(config_path))
  51. {:ok, value} ->
  52. Config.put(unquote(config_path), value)
  53. end
  54. end)
  55. :ok
  56. end
  57. end
  58. defmacro clear_config(config_path, temp_setting) do
  59. # NOTE: `clear_config([section, key], value)` != `clear_config([section], key: value)` (!)
  60. # Displaying a warning to prevent unintentional clearing of all but one keys in section
  61. if Keyword.keyword?(temp_setting) and length(temp_setting) == 1 do
  62. Logger.warning(
  63. "Please change `clear_config([section], key: value)` to `clear_config([section, key], value)`"
  64. )
  65. end
  66. quote do
  67. clear_config(unquote(config_path)) do
  68. Config.put(unquote(config_path), unquote(temp_setting))
  69. end
  70. end
  71. end
  72. def require_migration(migration_name) do
  73. [{module, _}] = Code.require_file("#{migration_name}.exs", "priv/repo/migrations")
  74. {:ok, %{migration: module}}
  75. end
  76. defmacro __using__(_opts) do
  77. quote do
  78. import Pleroma.Tests.Helpers,
  79. only: [
  80. clear_config: 1,
  81. clear_config: 2
  82. ]
  83. def time_travel(entity, seconds) do
  84. new_time = NaiveDateTime.add(entity.inserted_at, seconds)
  85. entity
  86. |> Ecto.Changeset.change(%{inserted_at: new_time, updated_at: new_time})
  87. |> Pleroma.Repo.update()
  88. end
  89. def to_datetime(%NaiveDateTime{} = naive_datetime) do
  90. naive_datetime
  91. |> DateTime.from_naive!("Etc/UTC")
  92. |> DateTime.truncate(:second)
  93. end
  94. def to_datetime(datetime) when is_binary(datetime) do
  95. datetime
  96. |> NaiveDateTime.from_iso8601!()
  97. |> to_datetime()
  98. end
  99. def collect_ids(collection) do
  100. collection
  101. |> Enum.map(& &1.id)
  102. |> Enum.sort()
  103. end
  104. def refresh_record(%{id: id, __struct__: model} = _),
  105. do: refresh_record(model, %{id: id})
  106. def refresh_record(model, %{id: id} = _) do
  107. Pleroma.Repo.get_by(model, id: id)
  108. end
  109. # Used for comparing json rendering during tests.
  110. def render_json(view, template, assigns) do
  111. assigns = Map.new(assigns)
  112. view.render(template, assigns)
  113. |> Jason.encode!()
  114. |> Jason.decode!()
  115. end
  116. def stringify_keys(nil), do: nil
  117. def stringify_keys(key) when key in [true, false], do: key
  118. def stringify_keys(key) when is_atom(key), do: Atom.to_string(key)
  119. def stringify_keys(map) when is_map(map) do
  120. map
  121. |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
  122. |> Enum.into(%{})
  123. end
  124. def stringify_keys([head | rest] = list) when is_list(list) do
  125. [stringify_keys(head) | stringify_keys(rest)]
  126. end
  127. def stringify_keys(key), do: key
  128. defmacro guards_config(config_path) do
  129. quote do
  130. initial_setting = Config.get(config_path)
  131. Config.put(config_path, true)
  132. on_exit(fn -> Config.put(config_path, initial_setting) end)
  133. end
  134. end
  135. end
  136. end
  137. end