total rebase
[anni] / test / support / helpers.ex
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
5 defmodule Pleroma.Tests.Helpers do
6   @moduledoc """
7   Helpers for use in tests.
8   """
9   alias Pleroma.Config
10
11   require Logger
12
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)
17
18     match?(^a_sorted, b_sorted)
19   end
20
21   @doc "Accepts a URL/URI and sorts the query parameters"
22   def uri_query_sort(uri) do
23     parsed = URI.parse(uri)
24
25     sorted_query =
26       String.split(parsed.query, "&")
27       |> Enum.sort()
28       |> Enum.join("&")
29
30     parsed
31     |> Map.put(:query, sorted_query)
32     |> URI.to_string()
33   end
34
35   @doc "Returns the value of the specified query parameter for the provided URL"
36   def get_query_parameter(url, param) do
37     url
38     |> URI.parse()
39     |> Map.get(:query)
40     |> URI.query_decoder()
41     |> Enum.to_list()
42     |> Enum.into(%{}, fn {x, y} -> {x, y} end)
43     |> Map.get(param)
44   end
45
46   defmacro clear_config(config_path) do
47     quote do
48       clear_config(unquote(config_path)) do
49       end
50     end
51   end
52
53   defmacro clear_config(config_path, do: yield) do
54     quote do
55       initial_setting = Config.fetch(unquote(config_path))
56
57       unquote(yield)
58
59       on_exit(fn ->
60         case initial_setting do
61           :error ->
62             Config.delete(unquote(config_path))
63
64           {:ok, value} ->
65             Config.put(unquote(config_path), value)
66         end
67       end)
68
69       :ok
70     end
71   end
72
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
77       Logger.warning(
78         "Please change `clear_config([section], key: value)` to `clear_config([section, key], value)`"
79       )
80     end
81
82     quote do
83       clear_config(unquote(config_path)) do
84         Config.put(unquote(config_path), unquote(temp_setting))
85       end
86     end
87   end
88
89   def require_migration(migration_name) do
90     [{module, _}] = Code.require_file("#{migration_name}.exs", "priv/repo/migrations")
91     {:ok, %{migration: module}}
92   end
93
94   defmacro __using__(_opts) do
95     quote do
96       import Pleroma.Tests.Helpers,
97         only: [
98           clear_config: 1,
99           clear_config: 2
100         ]
101
102       def time_travel(entity, seconds) do
103         new_time = NaiveDateTime.add(entity.inserted_at, seconds)
104
105         entity
106         |> Ecto.Changeset.change(%{inserted_at: new_time, updated_at: new_time})
107         |> Pleroma.Repo.update()
108       end
109
110       def to_datetime(%NaiveDateTime{} = naive_datetime) do
111         naive_datetime
112         |> DateTime.from_naive!("Etc/UTC")
113         |> DateTime.truncate(:second)
114       end
115
116       def to_datetime(datetime) when is_binary(datetime) do
117         datetime
118         |> NaiveDateTime.from_iso8601!()
119         |> to_datetime()
120       end
121
122       def collect_ids(collection) do
123         collection
124         |> Enum.map(& &1.id)
125         |> Enum.sort()
126       end
127
128       def refresh_record(%{id: id, __struct__: model} = _),
129         do: refresh_record(model, %{id: id})
130
131       def refresh_record(model, %{id: id} = _) do
132         Pleroma.Repo.get_by(model, id: id)
133       end
134
135       # Used for comparing json rendering during tests.
136       def render_json(view, template, assigns) do
137         assigns = Map.new(assigns)
138
139         view.render(template, assigns)
140         |> Jason.encode!()
141         |> Jason.decode!()
142       end
143
144       def stringify_keys(nil), do: nil
145
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)
148
149       def stringify_keys(map) when is_map(map) do
150         map
151         |> Enum.map(fn {k, v} -> {stringify_keys(k), stringify_keys(v)} end)
152         |> Enum.into(%{})
153       end
154
155       def stringify_keys([head | rest] = list) when is_list(list) do
156         [stringify_keys(head) | stringify_keys(rest)]
157       end
158
159       def stringify_keys(key), do: key
160
161       defmacro guards_config(config_path) do
162         quote do
163           initial_setting = Config.get(config_path)
164
165           Config.put(config_path, true)
166           on_exit(fn -> Config.put(config_path, initial_setting) end)
167         end
168       end
169     end
170   end
171 end