2018-12-23 21:11:29 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-03-17 17:09:58 +01:00
|
|
|
defmodule Pleroma.DataCase do
|
|
|
|
@moduledoc """
|
|
|
|
This module defines the setup for tests requiring
|
|
|
|
access to the application's data layer.
|
|
|
|
|
|
|
|
You may define functions here to be used as helpers in
|
|
|
|
your tests.
|
|
|
|
|
|
|
|
Finally, if the test case interacts with the database,
|
|
|
|
it cannot be async. For this reason, every test runs
|
|
|
|
inside a transaction which is reset at the beginning
|
|
|
|
of the test unless the test case is marked as async.
|
|
|
|
"""
|
|
|
|
|
|
|
|
use ExUnit.CaseTemplate
|
|
|
|
|
|
|
|
using do
|
|
|
|
quote do
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
import Ecto
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Ecto.Query
|
|
|
|
import Pleroma.DataCase
|
2018-12-11 13:31:52 +01:00
|
|
|
use Pleroma.Tests.Helpers
|
2017-03-17 17:09:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setup tags do
|
2018-03-22 12:44:32 +01:00
|
|
|
Cachex.clear(:user_cache)
|
2019-02-03 18:54:39 +01:00
|
|
|
Cachex.clear(:object_cache)
|
2017-03-17 17:09:58 +01:00
|
|
|
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
|
|
|
|
|
|
|
|
unless tags[:async] do
|
|
|
|
Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
|
|
|
|
end
|
|
|
|
|
2019-09-16 12:03:37 +02:00
|
|
|
if tags[:needs_streamer] do
|
|
|
|
start_supervised(Pleroma.Web.Streamer.supervisor())
|
|
|
|
end
|
|
|
|
|
2017-03-17 17:09:58 +01:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2019-07-18 14:30:18 +02:00
|
|
|
def ensure_local_uploader(context) do
|
|
|
|
test_uploader = Map.get(context, :uploader, Pleroma.Uploaders.Local)
|
2018-12-05 11:37:06 +01:00
|
|
|
uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
|
|
|
|
filters = Pleroma.Config.get([Pleroma.Upload, :filters])
|
|
|
|
|
2019-07-18 14:30:18 +02:00
|
|
|
Pleroma.Config.put([Pleroma.Upload, :uploader], test_uploader)
|
|
|
|
Pleroma.Config.put([Pleroma.Upload, :filters], [])
|
2018-12-05 11:37:06 +01:00
|
|
|
|
2019-07-18 14:30:18 +02:00
|
|
|
on_exit(fn ->
|
|
|
|
Pleroma.Config.put([Pleroma.Upload, :uploader], uploader)
|
|
|
|
Pleroma.Config.put([Pleroma.Upload, :filters], filters)
|
|
|
|
end)
|
2018-12-05 11:37:06 +01:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2017-03-17 17:09:58 +01:00
|
|
|
@doc """
|
|
|
|
A helper that transform changeset errors to a map of messages.
|
|
|
|
|
|
|
|
changeset = Accounts.create_user(%{password: "short"})
|
|
|
|
assert "password is too short" in errors_on(changeset).password
|
|
|
|
assert %{password: ["password is too short"]} = errors_on(changeset)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def errors_on(changeset) do
|
|
|
|
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
|
|
|
|
Enum.reduce(opts, message, fn {key, value}, acc ->
|
|
|
|
String.replace(acc, "%{#{key}}", to_string(value))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|