1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Tests.Helpers do
7 Helpers for use in tests.
13 @doc "Accepts two URLs/URIs and sorts the query parameters before comparing"
14 def uri_equal?(a, b) do
15 a_sorted = uri_query_sort(a)
16 b_sorted = uri_query_sort(b)
18 match?(^a_sorted, b_sorted)
21 @doc "Accepts a URL/URI and sorts the query parameters"
22 def uri_query_sort(uri) do
23 parsed = URI.parse(uri)
26 String.split(parsed.query, "&")
31 |> Map.put(:query, sorted_query)
35 @doc "Returns the value of the specified query parameter for the provided URL"
36 def get_query_parameter(url, param) do
40 |> URI.query_decoder()
42 |> Enum.into(%{}, fn {x, y} -> {x, y} end)
46 defmacro clear_config(config_path) do
48 clear_config(unquote(config_path)) do
53 defmacro clear_config(config_path, do: yield) do
55 initial_setting = Config.fetch(unquote(config_path))
60 case initial_setting do
62 Config.delete(unquote(config_path))
65 Config.put(unquote(config_path), value)
73 defmacro clear_config(config_path, temp_setting) do
74 # NOTE: `clear_config([section, key], value)` != `clear_config([section], key: value)` (!)
75 # Displaying a warning to prevent unintentional clearing of all but one keys in section
76 if Keyword.keyword?(temp_setting) and length(temp_setting) == 1 do
78 "Please change `clear_config([section], key: value)` to `clear_config([section, key], value)`"
83 clear_config(unquote(config_path)) do
84 Config.put(unquote(config_path), unquote(temp_setting))
89 def require_migration(migration_name) do
90 [{module, _}] = Code.require_file("#{migration_name}.exs", "priv/repo/migrations")
91 {:ok, %{migration: module}}
94 defmacro __using__(_opts) do
96 import Pleroma.Tests.Helpers,
102 def time_travel(entity, seconds) do
103 new_time = NaiveDateTime.add(entity.inserted_at, seconds)
106 |> Ecto.Changeset.change(%{inserted_at: new_time, updated_at: new_time})
107 |> Pleroma.Repo.update()
110 def to_datetime(%NaiveDateTime{} = naive_datetime) do
112 |> DateTime.from_naive!("Etc/UTC")
113 |> DateTime.truncate(:second)
116 def to_datetime(datetime) when is_binary(datetime) do
118 |> NaiveDateTime.from_iso8601!()
122 def collect_ids(collection) do
128 def refresh_record(%{id: id, __struct__: model} = _),
129 do: refresh_record(model, %{id: id})
131 def refresh_record(model, %{id: id} = _) do
132 Pleroma.Repo.get_by(model, id: id)
135 # Used for comparing json rendering during tests.
136 def render_json(view, template, assigns) do
137 assigns = Map.new(assigns)
139 view.render(template, assigns)
144 def stringify_keys(nil), do: nil
146 def stringify_keys(key) when key in [true, false], do: key
147 def stringify_keys(key) when is_atom(key), do: Atom.to_string(key)
149 def stringify_keys(map) when is_map(map) do
151 |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
155 def stringify_keys([head | rest] = list) when is_list(list) do
156 [stringify_keys(head) | stringify_keys(rest)]
159 def stringify_keys(key), do: key
161 defmacro guards_config(config_path) do
163 initial_setting = Config.get(config_path)
165 Config.put(config_path, true)
166 on_exit(fn -> Config.put(config_path, initial_setting) end)