forked from AkkomaGang/akkoma
Small wrapper module around Application.get_env/put_env
Same API as the old Pleroma.Config
This commit is contained in:
parent
36ca3c1b3e
commit
7d328c658d
4 changed files with 69 additions and 20 deletions
26
lib/pleroma/config.ex
Normal file
26
lib/pleroma/config.ex
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
defmodule Pleroma.Config do
|
||||||
|
def get([key]), do: get(key)
|
||||||
|
|
||||||
|
def get([parent_key | keys]) do
|
||||||
|
Application.get_env(:pleroma, parent_key)
|
||||||
|
|> get_in(keys)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(key) do
|
||||||
|
Application.get_env(:pleroma, key)
|
||||||
|
end
|
||||||
|
|
||||||
|
def put([key], value), do: put(key, value)
|
||||||
|
|
||||||
|
def put([parent_key | keys], value) do
|
||||||
|
parent =
|
||||||
|
Application.get_env(:pleroma, parent_key)
|
||||||
|
|> put_in(keys, value)
|
||||||
|
|
||||||
|
Application.put_env(:pleroma, parent_key, parent)
|
||||||
|
end
|
||||||
|
|
||||||
|
def put(key, value) do
|
||||||
|
Application.put_env(:pleroma, key, value)
|
||||||
|
end
|
||||||
|
end
|
39
test/config_test.exs
Normal file
39
test/config_test.exs
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
defmodule Pleroma.ConfigTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
|
||||||
|
test "get/1 with an atom" do
|
||||||
|
assert Pleroma.Config.get(:instance) == Application.get_env(:pleroma, :instance)
|
||||||
|
assert Pleroma.Config.get(:azertyuiop) == nil
|
||||||
|
end
|
||||||
|
|
||||||
|
test "get/1 with a list of keys" do
|
||||||
|
assert Pleroma.Config.get([:instance, :public]) ==
|
||||||
|
Keyword.get(Application.get_env(:pleroma, :instance), :public)
|
||||||
|
|
||||||
|
assert Pleroma.Config.get([Pleroma.Web.Endpoint, :render_errors, :view]) ==
|
||||||
|
get_in(
|
||||||
|
Application.get_env(
|
||||||
|
:pleroma,
|
||||||
|
Pleroma.Web.Endpoint
|
||||||
|
),
|
||||||
|
[:render_errors, :view]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert Pleroma.Config.get([:azerty, :uiop]) == nil
|
||||||
|
end
|
||||||
|
|
||||||
|
test "put/2 with a key" do
|
||||||
|
Pleroma.Config.put(:config_test, true)
|
||||||
|
|
||||||
|
assert Pleroma.Config.get(:config_test) == true
|
||||||
|
end
|
||||||
|
|
||||||
|
test "put/2 with a list of keys" do
|
||||||
|
Pleroma.Config.put([:instance, :config_test], true)
|
||||||
|
Pleroma.Config.put([:instance, :config_nested_test], [])
|
||||||
|
Pleroma.Config.put([:instance, :config_nested_test, :x], true)
|
||||||
|
|
||||||
|
assert Pleroma.Config.get([:instance, :config_test]) == true
|
||||||
|
assert Pleroma.Config.get([:instance, :config_nested_test, :x]) == true
|
||||||
|
end
|
||||||
|
end
|
|
@ -16,22 +16,14 @@ test "with the relay active, it returns the relay user", %{conn: conn} do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "with the relay disabled, it returns 404", %{conn: conn} do
|
test "with the relay disabled, it returns 404", %{conn: conn} do
|
||||||
instance =
|
Pleroma.Config.put([:instance, :allow_relay], false)
|
||||||
Application.get_env(:pleroma, :instance)
|
|
||||||
|> Keyword.put(:allow_relay, false)
|
|
||||||
|
|
||||||
Application.put_env(:pleroma, :instance, instance)
|
|
||||||
|
|
||||||
res =
|
res =
|
||||||
conn
|
conn
|
||||||
|> get(activity_pub_path(conn, :relay))
|
|> get(activity_pub_path(conn, :relay))
|
||||||
|> json_response(404)
|
|> json_response(404)
|
||||||
|
|
||||||
instance =
|
Pleroma.Config.put([:instance, :allow_relay], true)
|
||||||
Application.get_env(:pleroma, :instance)
|
|
||||||
|> Keyword.put(:allow_relay, true)
|
|
||||||
|
|
||||||
Application.put_env(:pleroma, :instance, instance)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -50,11 +50,7 @@ test "with relays deactivated, it does not publish to the relay", %{
|
||||||
activity: activity,
|
activity: activity,
|
||||||
relay_mock: relay_mock
|
relay_mock: relay_mock
|
||||||
} do
|
} do
|
||||||
instance =
|
Pleroma.Config.put([:instance, :allow_relay], false)
|
||||||
Application.get_env(:pleroma, :instance)
|
|
||||||
|> Keyword.put(:allow_relay, false)
|
|
||||||
|
|
||||||
Application.put_env(:pleroma, :instance, instance)
|
|
||||||
|
|
||||||
with_mocks([relay_mock]) do
|
with_mocks([relay_mock]) do
|
||||||
Federator.handle(:publish, activity)
|
Federator.handle(:publish, activity)
|
||||||
|
@ -62,11 +58,7 @@ test "with relays deactivated, it does not publish to the relay", %{
|
||||||
|
|
||||||
refute_received :relay_publish
|
refute_received :relay_publish
|
||||||
|
|
||||||
instance =
|
Pleroma.Config.put([:instance, :allow_relay], true)
|
||||||
Application.get_env(:pleroma, :instance)
|
|
||||||
|> Keyword.put(:allow_relay, true)
|
|
||||||
|
|
||||||
Application.put_env(:pleroma, :instance, instance)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue