Refactoring of config wrapper

This commit is contained in:
miffy 2019-09-08 00:05:54 +02:00
parent 96f51d3a27
commit a32ab590ab
25 changed files with 207 additions and 160 deletions

View file

@ -30,10 +30,11 @@ defmodule Mobilizon.Actors.Actor do
import Ecto.Changeset import Ecto.Changeset
alias Mobilizon.Actors alias Mobilizon.Actors
alias Mobilizon.Users.User
alias Mobilizon.Actors.{Actor, Follower, Member} alias Mobilizon.Actors.{Actor, Follower, Member}
alias Mobilizon.Config
alias Mobilizon.Events.{Event, FeedToken} alias Mobilizon.Events.{Event, FeedToken}
alias Mobilizon.Media.File alias Mobilizon.Media.File
alias Mobilizon.Users.User
alias Mobilizon.Reports.{Report, Note} alias Mobilizon.Reports.{Report, Note}
@ -207,9 +208,8 @@ defmodule Mobilizon.Actors.Actor do
pem = [entry] |> :public_key.pem_encode() |> String.trim_trailing() pem = [entry] |> :public_key.pem_encode() |> String.trim_trailing()
vars = %{ vars = %{
"name" => Mobilizon.CommonConfig.get([:instance, :name], "Mobilizon"), "name" => Config.get([:instance, :name], "Mobilizon"),
"summary" => "summary" => Config.get(
Mobilizon.CommonConfig.get(
[:instance, :description], [:instance, :description],
"An internal service actor for this Mobilizon instance" "An internal service actor for this Mobilizon instance"
), ),

View file

@ -2,8 +2,12 @@ defmodule Mobilizon.Application do
@moduledoc """ @moduledoc """
The Mobilizon application The Mobilizon application
""" """
use Application use Application
import Cachex.Spec import Cachex.Spec
alias Mobilizon.Config
alias Mobilizon.Service.Export.{Feed, ICalendar} alias Mobilizon.Service.Export.{Feed, ICalendar}
@name Mix.Project.config()[:name] @name Mix.Project.config()[:name]
@ -90,7 +94,7 @@ defmodule Mobilizon.Application do
def user_agent do def user_agent do
info = info =
"#{MobilizonWeb.Endpoint.url()} <#{Mobilizon.CommonConfig.get([:instance, :email], "")}>" "#{MobilizonWeb.Endpoint.url()} <#{Config.get([:instance, :email], "")}>"
named_version() <> "; " <> info named_version() <> "; " <> info
end end

View file

@ -1,71 +0,0 @@
defmodule Mobilizon.CommonConfig do
@moduledoc """
Instance configuration wrapper
"""
def registrations_open?() do
instance_config()
|> get_in([:registrations_open])
|> to_bool
end
def instance_name() do
instance_config()
|> get_in([:name])
end
def instance_description() do
instance_config()
|> get_in([:description])
end
def instance_hostname() do
instance_config()
|> get_in([:hostname])
end
def instance_config(), do: Application.get_env(:mobilizon, :instance)
defp to_bool(v), do: v == true or v == "true" or v == "True"
def get(key), do: get(key, nil)
def get([key], default), do: get(key, default)
def get([parent_key | keys], default) do
case :mobilizon
|> Application.get_env(parent_key)
|> get_in(keys) do
nil -> default
any -> any
end
end
def get(key, default) do
Application.get_env(:mobilizon, key, default)
end
def get!(key) do
value = get(key, nil)
if value == nil do
raise("Missing configuration value: #{inspect(key)}")
else
value
end
end
def put([key], value), do: put(key, value)
def put([parent_key | keys], value) do
parent =
Application.get_env(:mobilizon, parent_key)
|> put_in(keys, value)
Application.put_env(:mobilizon, parent_key, parent)
end
def put(key, value) do
Application.put_env(:mobilizon, key, value)
end
end

77
lib/mobilizon/config.ex Normal file
View file

@ -0,0 +1,77 @@
defmodule Mobilizon.Config do
@moduledoc """
Configuration wrapper.
"""
@spec instance_config :: keyword
def instance_config, do: Application.get_env(:mobilizon, :instance)
@spec instance_url :: String.t()
def instance_url, do: instance_config()[:instance]
@spec instance_name :: String.t()
def instance_name, do: instance_config()[:name]
@spec instance_description :: String.t()
def instance_description, do: instance_config()[:description]
@spec instance_version :: String.t()
def instance_version, do: instance_config()[:version]
@spec instance_hostname :: String.t()
def instance_hostname, do: instance_config()[:hostname]
@spec instance_registrations_open? :: boolean
def instance_registrations_open?, do: to_boolean(instance_config()[:registrations_open])
@spec instance_repository :: String.t()
def instance_repository, do: instance_config()[:repository]
@spec instance_email_from :: String.t()
def instance_email_from, do: instance_config()[:email_from]
@spec instance_email_reply_to :: String.t()
def instance_email_reply_to, do: instance_config()[:email_reply_to]
@spec get(module | atom) :: any
def get(key), do: get(key, nil)
@spec get([module | atom]) :: any
def get([key], default), do: get(key, default)
def get([parent_key | keys], default) do
case get_in(Application.get_env(:mobilizon, parent_key), keys) do
nil -> default
any -> any
end
end
@spec get(module | atom, any) :: any
def get(key, default), do: Application.get_env(:mobilizon, key, default)
@spec get!(module | atom) :: any
def get!(key) do
value = get(key, nil)
if value == nil do
raise("Missing configuration value: #{inspect(key)}")
else
value
end
end
@spec put([module | atom], any) :: any
def put([key], value), do: put(key, value)
def put([parent_key | keys], value) do
parent = put_in(Application.get_env(:mobilizon, parent_key), keys, value)
Application.put_env(:mobilizon, parent_key, parent)
end
@spec put(module | atom, any) :: any
def put(key, value), do: Application.put_env(:mobilizon, key, value)
@spec to_boolean(boolean | String.t()) :: boolean
defp to_boolean(boolean), do: "true" == String.downcase("#{boolean}")
end

View file

@ -2,7 +2,9 @@ defmodule MobilizonWeb.API.Utils do
@moduledoc """ @moduledoc """
Utils for API Utils for API
""" """
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Config
alias Mobilizon.Service.Formatter alias Mobilizon.Service.Formatter
@doc """ @doc """
@ -125,7 +127,7 @@ defmodule MobilizonWeb.API.Utils do
def make_report_content_html(nil), do: {:ok, {nil, [], []}} def make_report_content_html(nil), do: {:ok, {nil, [], []}}
def make_report_content_html(comment) do def make_report_content_html(comment) do
max_size = Mobilizon.CommonConfig.get([:instance, :max_report_comment_size], 1000) max_size = Config.get([:instance, :max_report_comment_size], 1000)
if String.length(comment) <= max_size do if String.length(comment) <= max_size do
{:ok, Formatter.html_escape(comment, "text/plain")} {:ok, Formatter.html_escape(comment, "text/plain")}

View file

@ -5,11 +5,13 @@
defmodule MobilizonWeb.ActivityPubController do defmodule MobilizonWeb.ActivityPubController do
use MobilizonWeb, :controller use MobilizonWeb, :controller
alias Mobilizon.{Actors, Actors.Actor}
alias MobilizonWeb.ActivityPub.ActorView alias Mobilizon.{Actors, Actors.Actor, Config}
alias Mobilizon.Service.ActivityPub alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.Federator alias Mobilizon.Service.Federator
alias MobilizonWeb.ActivityPub.ActorView
require Logger require Logger
action_fallback(:errors) action_fallback(:errors)
@ -17,7 +19,7 @@ defmodule MobilizonWeb.ActivityPubController do
plug(:relay_active? when action in [:relay]) plug(:relay_active? when action in [:relay])
def relay_active?(conn, _) do def relay_active?(conn, _) do
if Mobilizon.CommonConfig.get([:instance, :allow_relay]) do if Config.get([:instance, :allow_relay]) do
conn conn
else else
conn conn

View file

@ -5,13 +5,16 @@
defmodule MobilizonWeb.MediaProxyController do defmodule MobilizonWeb.MediaProxyController do
use MobilizonWeb, :controller use MobilizonWeb, :controller
alias Mobilizon.Config
alias MobilizonWeb.ReverseProxy alias MobilizonWeb.ReverseProxy
alias MobilizonWeb.MediaProxy alias MobilizonWeb.MediaProxy
@default_proxy_opts [max_body_length: 25 * 1_048_576, http: [follow_redirect: true]] @default_proxy_opts [max_body_length: 25 * 1_048_576, http: [follow_redirect: true]]
def remote(conn, %{"sig" => sig64, "url" => url64} = params) do def remote(conn, %{"sig" => sig64, "url" => url64} = params) do
with config <- Mobilizon.CommonConfig.get([:media_proxy], []), with config <- Config.get([:media_proxy], []),
true <- Keyword.get(config, :enabled, false), true <- Keyword.get(config, :enabled, false),
{:ok, url} <- MediaProxy.decode_url(sig64, url64), {:ok, url} <- MediaProxy.decode_url(sig64, url64),
:ok <- filename_matches(Map.has_key?(params, "filename"), conn.request_path, url) do :ok <- filename_matches(Map.has_key?(params, "filename"), conn.request_path, url) do

View file

@ -6,10 +6,8 @@
defmodule MobilizonWeb.NodeInfoController do defmodule MobilizonWeb.NodeInfoController do
use MobilizonWeb, :controller use MobilizonWeb, :controller
alias Mobilizon.{Events, Users} alias Mobilizon.{Config, Events, Users}
alias Mobilizon.CommonConfig
@instance Application.get_env(:mobilizon, :instance)
@node_info_supported_versions ["2.0", "2.1"] @node_info_supported_versions ["2.0", "2.1"]
@node_info_schema_uri "http://nodeinfo.diaspora.software/ns/schema/" @node_info_schema_uri "http://nodeinfo.diaspora.software/ns/schema/"
@ -35,14 +33,14 @@ defmodule MobilizonWeb.NodeInfoController do
version: version, version: version,
software: %{ software: %{
name: "mobilizon", name: "mobilizon",
version: Keyword.get(@instance, :version) version: Config.instance_version()
}, },
protocols: ["activitypub"], protocols: ["activitypub"],
services: %{ services: %{
inbound: [], inbound: [],
outbound: ["atom1.0"] outbound: ["atom1.0"]
}, },
openRegistrations: CommonConfig.registrations_open?(), openRegistrations: Config.instance_registrations_open?(),
usage: %{ usage: %{
users: %{ users: %{
total: Users.count_users() total: Users.count_users()
@ -51,14 +49,14 @@ defmodule MobilizonWeb.NodeInfoController do
localComments: Events.count_local_comments() localComments: Events.count_local_comments()
}, },
metadata: %{ metadata: %{
nodeName: CommonConfig.instance_name(), nodeName: Config.instance_name(),
nodeDescription: CommonConfig.instance_description() nodeDescription: Config.instance_description()
} }
} }
response = response =
if version == "2.1" do if version == "2.1" do
put_in(response, [:software, :repository], Keyword.get(@instance, :repository)) put_in(response, [:software, :repository], Config.instance_repository())
else else
response response
end end

View file

@ -7,6 +7,9 @@ defmodule MobilizonWeb.MediaProxy do
@moduledoc """ @moduledoc """
Handles proxifying media files Handles proxifying media files
""" """
alias Mobilizon.Config
@base64_opts [padding: false] @base64_opts [padding: false]
def url(nil), do: nil def url(nil), do: nil
@ -66,7 +69,7 @@ defmodule MobilizonWeb.MediaProxy do
def build_url(sig_base64, url_base64, filename \\ nil) do def build_url(sig_base64, url_base64, filename \\ nil) do
[ [
Mobilizon.CommonConfig.get([:media_proxy, :base_url], MobilizonWeb.Endpoint.url()), Config.get([:media_proxy, :base_url], MobilizonWeb.Endpoint.url()),
"proxy", "proxy",
sig_base64, sig_base64,
url_base64, url_base64,

View file

@ -8,10 +8,14 @@ defmodule MobilizonWeb.Plugs.UploadedMedia do
Serves uploaded media files Serves uploaded media files
""" """
@behaviour Plug
import Plug.Conn import Plug.Conn
alias Mobilizon.Config
require Logger require Logger
@behaviour Plug
# no slashes # no slashes
@path "media" @path "media"
@ -38,7 +42,7 @@ defmodule MobilizonWeb.Plugs.UploadedMedia do
conn conn
end end
config = Mobilizon.CommonConfig.get([MobilizonWeb.Upload]) config = Config.get([MobilizonWeb.Upload])
with uploader <- Keyword.fetch!(config, :uploader), with uploader <- Keyword.fetch!(config, :uploader),
proxy_remote = Keyword.get(config, :proxy_remote, false), proxy_remote = Keyword.get(config, :proxy_remote, false),
@ -75,7 +79,7 @@ defmodule MobilizonWeb.Plugs.UploadedMedia do
conn conn
|> MobilizonWeb.ReverseProxy.call( |> MobilizonWeb.ReverseProxy.call(
url, url,
Mobilizon.CommonConfig.get([Mobilizon.Upload, :proxy_opts], []) Config.get([Mobilizon.Upload, :proxy_opts], [])
) )
end end

View file

@ -1,19 +1,19 @@
defmodule MobilizonWeb.Resolvers.Config do defmodule MobilizonWeb.Resolvers.Config do
@moduledoc """ @moduledoc """
Handles the config-related GraphQL calls Handles the config-related GraphQL calls.
""" """
import Mobilizon.CommonConfig alias Mobilizon.Config
@doc """ @doc """
Get config Gets config.
""" """
def get_config(_parent, _params, _context) do def get_config(_parent, _params, _context) do
{:ok, {:ok,
%{ %{
name: instance_name(), name: Config.instance_name(),
registrations_open: registrations_open?(), registrations_open: Config.instance_registrations_open?(),
description: instance_description() description: Config.instance_description()
}} }}
end end
end end

View file

@ -2,12 +2,14 @@ defmodule MobilizonWeb.Resolvers.User do
@moduledoc """ @moduledoc """
Handles the user-related GraphQL calls Handles the user-related GraphQL calls
""" """
alias Mobilizon.{Actors, Config, Users}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.CommonConfig
alias Mobilizon.Users.User
alias Mobilizon.{Actors, Users}
alias Mobilizon.Service.Users.{ResetPassword, Activation} alias Mobilizon.Service.Users.{ResetPassword, Activation}
alias Mobilizon.Users.User
import Mobilizon.Users.Guards import Mobilizon.Users.Guards
require Logger require Logger
@doc """ @doc """
@ -110,7 +112,7 @@ defmodule MobilizonWeb.Resolvers.User do
""" """
@spec create_user(any(), map(), any()) :: tuple() @spec create_user(any(), map(), any()) :: tuple()
def create_user(_parent, args, _resolution) do def create_user(_parent, args, _resolution) do
with {:registrations_open, true} <- {:registrations_open, CommonConfig.registrations_open?()}, with {:registrations_open, true} <- {:registrations_open, Config.instance_registrations_open?()},
{:ok, %User{} = user} <- Users.register(args) do {:ok, %User{} = user} <- Users.register(args) do
Activation.send_confirmation_email(user) Activation.send_confirmation_email(user)
{:ok, user} {:ok, user}

View file

@ -31,7 +31,11 @@ defmodule MobilizonWeb.Upload do
* `MobilizonWeb.Upload.Filter` * `MobilizonWeb.Upload.Filter`
""" """
alias Ecto.UUID alias Ecto.UUID
alias Mobilizon.Config
require Logger require Logger
@type source :: @type source ::
@ -110,26 +114,26 @@ defmodule MobilizonWeb.Upload do
{size_limit, activity_type} = {size_limit, activity_type} =
case Keyword.get(opts, :type) do case Keyword.get(opts, :type) do
:banner -> :banner ->
{Mobilizon.CommonConfig.get!([:instance, :banner_upload_limit]), "Image"} {Config.get!([:instance, :banner_upload_limit]), "Image"}
:avatar -> :avatar ->
{Mobilizon.CommonConfig.get!([:instance, :avatar_upload_limit]), "Image"} {Config.get!([:instance, :avatar_upload_limit]), "Image"}
_ -> _ ->
{Mobilizon.CommonConfig.get!([:instance, :upload_limit]), nil} {Config.get!([:instance, :upload_limit]), nil}
end end
%{ %{
activity_type: Keyword.get(opts, :activity_type, activity_type), activity_type: Keyword.get(opts, :activity_type, activity_type),
size_limit: Keyword.get(opts, :size_limit, size_limit), size_limit: Keyword.get(opts, :size_limit, size_limit),
uploader: Keyword.get(opts, :uploader, Mobilizon.CommonConfig.get([__MODULE__, :uploader])), uploader: Keyword.get(opts, :uploader, Config.get([__MODULE__, :uploader])),
filters: Keyword.get(opts, :filters, Mobilizon.CommonConfig.get([__MODULE__, :filters])), filters: Keyword.get(opts, :filters, Config.get([__MODULE__, :filters])),
description: Keyword.get(opts, :description), description: Keyword.get(opts, :description),
base_url: base_url:
Keyword.get( Keyword.get(
opts, opts,
:base_url, :base_url,
Mobilizon.CommonConfig.get([__MODULE__, :base_url], MobilizonWeb.Endpoint.url()) Config.get([__MODULE__, :base_url], MobilizonWeb.Endpoint.url())
) )
} }
end end
@ -173,7 +177,7 @@ defmodule MobilizonWeb.Upload do
defp url_from_spec(%__MODULE__{name: name}, base_url, {:file, path}) do defp url_from_spec(%__MODULE__{name: name}, base_url, {:file, path}) do
path = path =
URI.encode(path, &char_unescaped?/1) <> URI.encode(path, &char_unescaped?/1) <>
if Mobilizon.CommonConfig.get([__MODULE__, :link_name], false) do if Config.get([__MODULE__, :link_name], false) do
"?name=#{URI.encode(name, &char_unescaped?/1)}" "?name=#{URI.encode(name, &char_unescaped?/1)}"
else else
"" ""

View file

@ -9,11 +9,14 @@ defmodule MobilizonWeb.Upload.Filter.AnonymizeFilename do
Should be used after `MobilizonWeb.Upload.Filter.Dedupe`. Should be used after `MobilizonWeb.Upload.Filter.Dedupe`.
""" """
@behaviour MobilizonWeb.Upload.Filter @behaviour MobilizonWeb.Upload.Filter
alias Mobilizon.Config
def filter(upload) do def filter(upload) do
extension = List.last(String.split(upload.name, ".")) extension = List.last(String.split(upload.name, "."))
name = Mobilizon.CommonConfig.get([__MODULE__, :text], random(extension)) name = Config.get([__MODULE__, :text], random(extension))
{:ok, %MobilizonWeb.Upload{upload | name: name}} {:ok, %MobilizonWeb.Upload{upload | name: name}}
end end

View file

@ -7,13 +7,16 @@ defmodule MobilizonWeb.Upload.Filter.Mogrify do
@moduledoc """ @moduledoc """
Handle mogrify transformations Handle mogrify transformations
""" """
@behaviour MobilizonWeb.Upload.Filter @behaviour MobilizonWeb.Upload.Filter
alias Mobilizon.Config
@type conversion :: action :: String.t() | {action :: String.t(), opts :: String.t()} @type conversion :: action :: String.t() | {action :: String.t(), opts :: String.t()}
@type conversions :: conversion() | [conversion()] @type conversions :: conversion() | [conversion()]
def filter(%MobilizonWeb.Upload{tempfile: file, content_type: "image" <> _}) do def filter(%MobilizonWeb.Upload{tempfile: file, content_type: "image" <> _}) do
filters = Mobilizon.CommonConfig.get!([__MODULE__, :args]) filters = Config.get!([__MODULE__, :args])
file file
|> Mogrify.open() |> Mogrify.open()

View file

@ -7,8 +7,11 @@ defmodule MobilizonWeb.Uploaders.Local do
@moduledoc """ @moduledoc """
Local uploader for files Local uploader for files
""" """
@behaviour MobilizonWeb.Uploaders.Uploader @behaviour MobilizonWeb.Uploaders.Uploader
alias Mobilizon.Config
def get_file(_) do def get_file(_) do
{:ok, {:static_dir, upload_path()}} {:ok, {:static_dir, upload_path()}}
end end
@ -59,6 +62,6 @@ defmodule MobilizonWeb.Uploaders.Local do
end end
def upload_path do def upload_path do
Mobilizon.CommonConfig.get!([__MODULE__, :uploads]) Config.get!([__MODULE__, :uploads])
end end
end end

View file

@ -10,6 +10,7 @@ defmodule Mobilizon.Service.ActivityPub do
Every ActivityPub method Every ActivityPub method
""" """
alias Mobilizon.Config
alias Mobilizon.Events alias Mobilizon.Events
alias Mobilizon.Events.{Event, Comment, Participant} alias Mobilizon.Events.{Event, Comment, Participant}
alias Mobilizon.Service.ActivityPub.Transmogrifier alias Mobilizon.Service.ActivityPub.Transmogrifier
@ -521,7 +522,7 @@ defmodule Mobilizon.Service.ActivityPub do
public = is_public?(activity) public = is_public?(activity)
if public && Mobilizon.CommonConfig.get([:instance, :allow_relay]) do if public && Config.get([:instance, :allow_relay]) do
Logger.info(fn -> "Relaying #{activity.data["id"]} out" end) Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
Mobilizon.Service.ActivityPub.Relay.publish(activity) Mobilizon.Service.ActivityPub.Relay.publish(activity)
end end

View file

@ -1,9 +1,8 @@
defmodule Mobilizon.ActorsTest do defmodule Mobilizon.ActorsTest do
use Mobilizon.DataCase use Mobilizon.DataCase
alias Mobilizon.Actors alias Mobilizon.{Actors, Config, Users}
alias Mobilizon.Actors.{Actor, Member, Follower, Bot} alias Mobilizon.Actors.{Actor, Member, Follower, Bot}
alias Mobilizon.Users
alias Mobilizon.Media.File, as: FileModel alias Mobilizon.Media.File, as: FileModel
import Mobilizon.Factory import Mobilizon.Factory
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
@ -281,12 +280,12 @@ defmodule Mobilizon.ActorsTest do
%URI{path: "/media/" <> banner_path} = URI.parse(banner_url) %URI{path: "/media/" <> banner_path} = URI.parse(banner_url)
assert File.exists?( assert File.exists?(
Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <>
"/" <> avatar_path "/" <> avatar_path
) )
assert File.exists?( assert File.exists?(
Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <>
"/" <> banner_path "/" <> banner_path
) )
@ -312,12 +311,12 @@ defmodule Mobilizon.ActorsTest do
refute actor.suspended refute actor.suspended
refute File.exists?( refute File.exists?(
Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <>
"/" <> avatar_path "/" <> avatar_path
) )
assert File.exists?( assert File.exists?(
Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <>
"/" <> banner_path "/" <> banner_path
) )
end end
@ -335,12 +334,12 @@ defmodule Mobilizon.ActorsTest do
%URI{path: "/media/" <> banner_path} = URI.parse(banner_url) %URI{path: "/media/" <> banner_path} = URI.parse(banner_url)
assert File.exists?( assert File.exists?(
Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <>
"/" <> avatar_path "/" <> avatar_path
) )
assert File.exists?( assert File.exists?(
Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <>
"/" <> banner_path "/" <> banner_path
) )
@ -348,7 +347,7 @@ defmodule Mobilizon.ActorsTest do
assert_raise Ecto.NoResultsError, fn -> Actors.get_actor!(actor_id) end assert_raise Ecto.NoResultsError, fn -> Actors.get_actor!(actor_id) end
refute File.exists?( refute File.exists?(
Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <>
"/" <> banner_path "/" <> banner_path
) )
end end

View file

@ -1,9 +1,10 @@
defmodule Mobilizon.MediaTest do defmodule Mobilizon.MediaTest do
use Mobilizon.DataCase use Mobilizon.DataCase
alias Mobilizon.Media
import Mobilizon.Factory import Mobilizon.Factory
alias Mobilizon.{Config, Media}
describe "media" do describe "media" do
setup [:ensure_local_uploader] setup [:ensure_local_uploader]
alias Mobilizon.Media.Picture alias Mobilizon.Media.Picture
@ -48,7 +49,7 @@ defmodule Mobilizon.MediaTest do
%URI{path: "/media/" <> path} = URI.parse(picture.file.url) %URI{path: "/media/" <> path} = URI.parse(picture.file.url)
assert File.exists?( assert File.exists?(
Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <>
"/" <> path "/" <> path
) )
@ -56,7 +57,7 @@ defmodule Mobilizon.MediaTest do
assert_raise Ecto.NoResultsError, fn -> Media.get_picture!(picture.id) end assert_raise Ecto.NoResultsError, fn -> Media.get_picture!(picture.id) end
refute File.exists?( refute File.exists?(
Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <>
"/" <> path "/" <> path
) )
end end

View file

@ -8,7 +8,7 @@ defmodule MobilizonWeb.ActivityPubControllerTest do
import Mobilizon.Factory import Mobilizon.Factory
alias MobilizonWeb.ActivityPub.ActorView alias MobilizonWeb.ActivityPub.ActorView
alias MobilizonWeb.PageView alias MobilizonWeb.PageView
alias Mobilizon.Actors alias Mobilizon.{Actors, Config}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Service.ActivityPub alias Mobilizon.Service.ActivityPub
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
@ -290,14 +290,14 @@ defmodule MobilizonWeb.ActivityPubControllerTest 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
Mobilizon.CommonConfig.put([:instance, :allow_relay], false) Config.put([:instance, :allow_relay], false)
conn conn
|> get(activity_pub_path(conn, :relay)) |> get(activity_pub_path(conn, :relay))
|> json_response(404) |> json_response(404)
|> assert |> assert
Mobilizon.CommonConfig.put([:instance, :allow_relay], true) Config.put([:instance, :allow_relay], true)
end end
end end

View file

@ -1,7 +1,7 @@
defmodule MobilizonWeb.NodeInfoControllerTest do defmodule MobilizonWeb.NodeInfoControllerTest do
use MobilizonWeb.ConnCase use MobilizonWeb.ConnCase
@instance Application.get_env(:mobilizon, :instance) alias Mobilizon.Config
test "Get node info schemas", %{conn: conn} do test "Get node info schemas", %{conn: conn} do
conn = get(conn, node_info_path(conn, :schemas)) conn = get(conn, node_info_path(conn, :schemas))
@ -37,16 +37,16 @@ defmodule MobilizonWeb.NodeInfoControllerTest do
assert resp == %{ assert resp == %{
"metadata" => %{ "metadata" => %{
"nodeName" => Mobilizon.CommonConfig.instance_name(), "nodeName" => Config.instance_name(),
"nodeDescription" => Mobilizon.CommonConfig.instance_description() "nodeDescription" => Config.instance_description()
}, },
"openRegistrations" => Keyword.get(@instance, :registrations_open), "openRegistrations" => Config.instance_registrations_open?(),
"protocols" => ["activitypub"], "protocols" => ["activitypub"],
"services" => %{"inbound" => [], "outbound" => ["atom1.0"]}, "services" => %{"inbound" => [], "outbound" => ["atom1.0"]},
"software" => %{ "software" => %{
"name" => "mobilizon", "name" => "mobilizon",
"version" => Keyword.get(@instance, :version), "version" => Config.instance_version(),
"repository" => Keyword.get(@instance, :repository) "repository" => Config.instance_repository()
}, },
"usage" => %{"localComments" => 0, "localPosts" => 0, "users" => %{"total" => 0}}, "usage" => %{"localComments" => 0, "localPosts" => 0, "users" => %{"total" => 0}},
"version" => "2.1" "version" => "2.1"

View file

@ -5,18 +5,22 @@
defmodule MobilizonWeb.MediaProxyTest do defmodule MobilizonWeb.MediaProxyTest do
use ExUnit.Case use ExUnit.Case
import MobilizonWeb.MediaProxy import MobilizonWeb.MediaProxy
alias Mobilizon.Config
alias MobilizonWeb.MediaProxyController alias MobilizonWeb.MediaProxyController
setup do setup do
enabled = Mobilizon.CommonConfig.get([:media_proxy, :enabled]) enabled = Config.get([:media_proxy, :enabled])
on_exit(fn -> Mobilizon.CommonConfig.put([:media_proxy, :enabled], enabled) end) on_exit(fn -> Config.put([:media_proxy, :enabled], enabled) end)
:ok :ok
end end
describe "when enabled" do describe "when enabled" do
setup do setup do
Mobilizon.CommonConfig.put([:media_proxy, :enabled], true) Config.put([:media_proxy, :enabled], true)
:ok :ok
end end
@ -43,7 +47,7 @@ defmodule MobilizonWeb.MediaProxyTest do
assert String.starts_with?( assert String.starts_with?(
encoded, encoded,
Mobilizon.CommonConfig.get([:media_proxy, :base_url], MobilizonWeb.Endpoint.url()) Config.get([:media_proxy, :base_url], MobilizonWeb.Endpoint.url())
) )
assert String.ends_with?(encoded, "/logo.png") assert String.ends_with?(encoded, "/logo.png")
@ -80,15 +84,15 @@ defmodule MobilizonWeb.MediaProxyTest do
end end
test "validates signature" do test "validates signature" do
secret_key_base = Mobilizon.CommonConfig.get([MobilizonWeb.Endpoint, :secret_key_base]) secret_key_base = Config.get([MobilizonWeb.Endpoint, :secret_key_base])
on_exit(fn -> on_exit(fn ->
Mobilizon.CommonConfig.put([MobilizonWeb.Endpoint, :secret_key_base], secret_key_base) Config.put([MobilizonWeb.Endpoint, :secret_key_base], secret_key_base)
end) end)
encoded = url("https://pleroma.social") encoded = url("https://pleroma.social")
Mobilizon.CommonConfig.put( Config.put(
[MobilizonWeb.Endpoint, :secret_key_base], [MobilizonWeb.Endpoint, :secret_key_base],
"00000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000"
) )
@ -126,20 +130,20 @@ defmodule MobilizonWeb.MediaProxyTest do
end end
test "uses the configured base_url" do test "uses the configured base_url" do
base_url = Mobilizon.CommonConfig.get([:media_proxy, :base_url]) base_url = Config.get([:media_proxy, :base_url])
if base_url do if base_url do
on_exit(fn -> on_exit(fn ->
Mobilizon.CommonConfig.put([:media_proxy, :base_url], base_url) Config.put([:media_proxy, :base_url], base_url)
end) end)
end end
Mobilizon.CommonConfig.put([:media_proxy, :base_url], "https://cache.pleroma.social") Config.put([:media_proxy, :base_url], "https://cache.pleroma.social")
url = "https://pleroma.soykaf.com/static/logo.png" url = "https://pleroma.soykaf.com/static/logo.png"
encoded = url(url) encoded = url(url)
assert String.starts_with?(encoded, Mobilizon.CommonConfig.get([:media_proxy, :base_url])) assert String.starts_with?(encoded, Config.get([:media_proxy, :base_url]))
end end
# https://git.pleroma.social/pleroma/pleroma/issues/580 # https://git.pleroma.social/pleroma/pleroma/issues/580
@ -154,13 +158,13 @@ defmodule MobilizonWeb.MediaProxyTest do
describe "when disabled" do describe "when disabled" do
setup do setup do
enabled = Mobilizon.CommonConfig.get([:media_proxy, :enabled]) enabled = Config.get([:media_proxy, :enabled])
if enabled do if enabled do
Mobilizon.CommonConfig.put([:media_proxy, :enabled], false) Config.put([:media_proxy, :enabled], false)
on_exit(fn -> on_exit(fn ->
Mobilizon.CommonConfig.put([:media_proxy, :enabled], enabled) Config.put([:media_proxy, :enabled], enabled)
:ok :ok
end) end)
end end

View file

@ -1,6 +1,6 @@
defmodule MobilizonWeb.Resolvers.UserResolverTest do defmodule MobilizonWeb.Resolvers.UserResolverTest do
use MobilizonWeb.ConnCase use MobilizonWeb.ConnCase
alias Mobilizon.{Actors, Users, CommonConfig} alias Mobilizon.{Actors, Config, Users}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Users.User alias Mobilizon.Users.User
alias Mobilizon.Users alias Mobilizon.Users
@ -401,7 +401,7 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
end end
test "test create_user/3 doesn't create a user when registration is disabled", context do test "test create_user/3 doesn't create a user when registration is disabled", context do
with_mock CommonConfig, registrations_open?: fn -> false end do with_mock Config, instance_registrations_open?: fn -> false end do
mutation = """ mutation = """
mutation { mutation {
createUser( createUser(

View file

@ -4,9 +4,12 @@
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/upload_test.ex # Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/upload_test.ex
defmodule Mobilizon.UploadTest do defmodule Mobilizon.UploadTest do
alias MobilizonWeb.Upload
use Mobilizon.DataCase use Mobilizon.DataCase
alias Mobilizon.Config
alias MobilizonWeb.Upload
describe "Storing a file with the Local uploader" do describe "Storing a file with the Local uploader" do
setup [:ensure_local_uploader] setup [:ensure_local_uploader]
@ -185,7 +188,7 @@ defmodule Mobilizon.UploadTest do
test "delete a not existing file" do test "delete a not existing file" do
file = file =
Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <>
"/not_existing/definitely.jpg" "/not_existing/definitely.jpg"
refute File.exists?(file) refute File.exists?(file)
@ -215,6 +218,6 @@ defmodule Mobilizon.UploadTest do
assert String.starts_with?(url, MobilizonWeb.Endpoint.url() <> "/media/") assert String.starts_with?(url, MobilizonWeb.Endpoint.url() <> "/media/")
%URI{path: "/media/" <> path} = URI.parse(url) %URI{path: "/media/" <> path} = URI.parse(url)
{Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/" <> path, url} {Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/" <> path, url}
end end
end end

View file

@ -14,6 +14,8 @@ defmodule Mobilizon.DataCase do
use ExUnit.CaseTemplate use ExUnit.CaseTemplate
alias Mobilizon.Config
using do using do
quote do quote do
alias Mobilizon.Repo alias Mobilizon.Repo
@ -52,16 +54,16 @@ defmodule Mobilizon.DataCase do
end end
def ensure_local_uploader(_context) do def ensure_local_uploader(_context) do
uploader = Mobilizon.CommonConfig.get([MobilizonWeb.Upload, :uploader]) uploader = Config.get([MobilizonWeb.Upload, :uploader])
filters = Mobilizon.CommonConfig.get([MobilizonWeb.Upload, :filters]) filters = Config.get([MobilizonWeb.Upload, :filters])
unless uploader == MobilizonWeb.Uploaders.Local || filters != [] do unless uploader == MobilizonWeb.Uploaders.Local || filters != [] do
Mobilizon.CommonConfig.put([MobilizonWeb.Upload, :uploader], MobilizonWeb.Uploaders.Local) Config.put([MobilizonWeb.Upload, :uploader], MobilizonWeb.Uploaders.Local)
Mobilizon.CommonConfig.put([MobilizonWeb.Upload, :filters], []) Config.put([MobilizonWeb.Upload, :filters], [])
on_exit(fn -> on_exit(fn ->
Mobilizon.CommonConfig.put([MobilizonWeb.Upload, :uploader], uploader) Config.put([MobilizonWeb.Upload, :uploader], uploader)
Mobilizon.CommonConfig.put([MobilizonWeb.Upload, :filters], filters) Config.put([MobilizonWeb.Upload, :filters], filters)
end) end)
end end