From a32ab590abe293ea8a82f8f96ac724c29e23f3c1 Mon Sep 17 00:00:00 2001 From: miffy Date: Sun, 8 Sep 2019 00:05:54 +0200 Subject: [PATCH] Refactoring of config wrapper --- lib/mobilizon/actors/actor.ex | 8 +- lib/mobilizon/application.ex | 6 +- lib/mobilizon/common-config.ex | 71 ----------------- lib/mobilizon/config.ex | 77 +++++++++++++++++++ lib/mobilizon_web/api/utils.ex | 4 +- .../controllers/activity_pub_controller.ex | 8 +- .../controllers/media_proxy_controller.ex | 5 +- .../controllers/node_info_controller.ex | 14 ++-- lib/mobilizon_web/media_proxy.ex | 5 +- lib/mobilizon_web/plugs/uploaded_media.ex | 10 ++- lib/mobilizon_web/resolvers/config.ex | 12 +-- lib/mobilizon_web/resolvers/user.ex | 10 ++- lib/mobilizon_web/upload.ex | 18 +++-- .../upload/filter/anonymize_filename.ex | 5 +- lib/mobilizon_web/upload/filter/mogrify.ex | 5 +- lib/mobilizon_web/uploaders/local.ex | 5 +- lib/service/activity_pub/activity_pub.ex | 3 +- test/mobilizon/actors/actors_test.exs | 17 ++-- test/mobilizon/media/media_test.exs | 7 +- .../activity_pub_controller_test.exs | 6 +- .../controllers/nodeinfo_controller_test.exs | 12 +-- test/mobilizon_web/media_proxy_test.exs | 32 ++++---- .../resolvers/user_resolver_test.exs | 4 +- test/mobilizon_web/upload_test.exs | 9 ++- test/support/data_case.ex | 14 ++-- 25 files changed, 207 insertions(+), 160 deletions(-) delete mode 100644 lib/mobilizon/common-config.ex create mode 100644 lib/mobilizon/config.ex diff --git a/lib/mobilizon/actors/actor.ex b/lib/mobilizon/actors/actor.ex index 2cf983479..6fe728e2a 100644 --- a/lib/mobilizon/actors/actor.ex +++ b/lib/mobilizon/actors/actor.ex @@ -30,10 +30,11 @@ defmodule Mobilizon.Actors.Actor do import Ecto.Changeset alias Mobilizon.Actors - alias Mobilizon.Users.User alias Mobilizon.Actors.{Actor, Follower, Member} + alias Mobilizon.Config alias Mobilizon.Events.{Event, FeedToken} alias Mobilizon.Media.File + alias Mobilizon.Users.User alias Mobilizon.Reports.{Report, Note} @@ -207,9 +208,8 @@ defmodule Mobilizon.Actors.Actor do pem = [entry] |> :public_key.pem_encode() |> String.trim_trailing() vars = %{ - "name" => Mobilizon.CommonConfig.get([:instance, :name], "Mobilizon"), - "summary" => - Mobilizon.CommonConfig.get( + "name" => Config.get([:instance, :name], "Mobilizon"), + "summary" => Config.get( [:instance, :description], "An internal service actor for this Mobilizon instance" ), diff --git a/lib/mobilizon/application.ex b/lib/mobilizon/application.ex index 22bf699d8..3bbb4b0f3 100644 --- a/lib/mobilizon/application.ex +++ b/lib/mobilizon/application.ex @@ -2,8 +2,12 @@ defmodule Mobilizon.Application do @moduledoc """ The Mobilizon application """ + use Application + import Cachex.Spec + + alias Mobilizon.Config alias Mobilizon.Service.Export.{Feed, ICalendar} @name Mix.Project.config()[:name] @@ -90,7 +94,7 @@ defmodule Mobilizon.Application do def user_agent do info = - "#{MobilizonWeb.Endpoint.url()} <#{Mobilizon.CommonConfig.get([:instance, :email], "")}>" + "#{MobilizonWeb.Endpoint.url()} <#{Config.get([:instance, :email], "")}>" named_version() <> "; " <> info end diff --git a/lib/mobilizon/common-config.ex b/lib/mobilizon/common-config.ex deleted file mode 100644 index 091857777..000000000 --- a/lib/mobilizon/common-config.ex +++ /dev/null @@ -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 diff --git a/lib/mobilizon/config.ex b/lib/mobilizon/config.ex new file mode 100644 index 000000000..1d3581baa --- /dev/null +++ b/lib/mobilizon/config.ex @@ -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 diff --git a/lib/mobilizon_web/api/utils.ex b/lib/mobilizon_web/api/utils.ex index 6c983e447..c99710093 100644 --- a/lib/mobilizon_web/api/utils.ex +++ b/lib/mobilizon_web/api/utils.ex @@ -2,7 +2,9 @@ defmodule MobilizonWeb.API.Utils do @moduledoc """ Utils for API """ + alias Mobilizon.Actors.Actor + alias Mobilizon.Config alias Mobilizon.Service.Formatter @doc """ @@ -125,7 +127,7 @@ defmodule MobilizonWeb.API.Utils do def make_report_content_html(nil), do: {:ok, {nil, [], []}} 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 {:ok, Formatter.html_escape(comment, "text/plain")} diff --git a/lib/mobilizon_web/controllers/activity_pub_controller.ex b/lib/mobilizon_web/controllers/activity_pub_controller.ex index 63e7d3fb9..1bdbbf49a 100644 --- a/lib/mobilizon_web/controllers/activity_pub_controller.ex +++ b/lib/mobilizon_web/controllers/activity_pub_controller.ex @@ -5,11 +5,13 @@ defmodule MobilizonWeb.ActivityPubController do 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.Federator + alias MobilizonWeb.ActivityPub.ActorView + require Logger action_fallback(:errors) @@ -17,7 +19,7 @@ defmodule MobilizonWeb.ActivityPubController do plug(:relay_active? when action in [:relay]) def relay_active?(conn, _) do - if Mobilizon.CommonConfig.get([:instance, :allow_relay]) do + if Config.get([:instance, :allow_relay]) do conn else conn diff --git a/lib/mobilizon_web/controllers/media_proxy_controller.ex b/lib/mobilizon_web/controllers/media_proxy_controller.ex index 55f3fc20a..630d8b25f 100644 --- a/lib/mobilizon_web/controllers/media_proxy_controller.ex +++ b/lib/mobilizon_web/controllers/media_proxy_controller.ex @@ -5,13 +5,16 @@ defmodule MobilizonWeb.MediaProxyController do use MobilizonWeb, :controller + + alias Mobilizon.Config + alias MobilizonWeb.ReverseProxy alias MobilizonWeb.MediaProxy @default_proxy_opts [max_body_length: 25 * 1_048_576, http: [follow_redirect: true]] 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), {:ok, url} <- MediaProxy.decode_url(sig64, url64), :ok <- filename_matches(Map.has_key?(params, "filename"), conn.request_path, url) do diff --git a/lib/mobilizon_web/controllers/node_info_controller.ex b/lib/mobilizon_web/controllers/node_info_controller.ex index 31bf6bc18..a2c2f45d3 100644 --- a/lib/mobilizon_web/controllers/node_info_controller.ex +++ b/lib/mobilizon_web/controllers/node_info_controller.ex @@ -6,10 +6,8 @@ defmodule MobilizonWeb.NodeInfoController do use MobilizonWeb, :controller - alias Mobilizon.{Events, Users} - alias Mobilizon.CommonConfig + alias Mobilizon.{Config, Events, Users} - @instance Application.get_env(:mobilizon, :instance) @node_info_supported_versions ["2.0", "2.1"] @node_info_schema_uri "http://nodeinfo.diaspora.software/ns/schema/" @@ -35,14 +33,14 @@ defmodule MobilizonWeb.NodeInfoController do version: version, software: %{ name: "mobilizon", - version: Keyword.get(@instance, :version) + version: Config.instance_version() }, protocols: ["activitypub"], services: %{ inbound: [], outbound: ["atom1.0"] }, - openRegistrations: CommonConfig.registrations_open?(), + openRegistrations: Config.instance_registrations_open?(), usage: %{ users: %{ total: Users.count_users() @@ -51,14 +49,14 @@ defmodule MobilizonWeb.NodeInfoController do localComments: Events.count_local_comments() }, metadata: %{ - nodeName: CommonConfig.instance_name(), - nodeDescription: CommonConfig.instance_description() + nodeName: Config.instance_name(), + nodeDescription: Config.instance_description() } } response = if version == "2.1" do - put_in(response, [:software, :repository], Keyword.get(@instance, :repository)) + put_in(response, [:software, :repository], Config.instance_repository()) else response end diff --git a/lib/mobilizon_web/media_proxy.ex b/lib/mobilizon_web/media_proxy.ex index 229e50e24..0f03544e2 100644 --- a/lib/mobilizon_web/media_proxy.ex +++ b/lib/mobilizon_web/media_proxy.ex @@ -7,6 +7,9 @@ defmodule MobilizonWeb.MediaProxy do @moduledoc """ Handles proxifying media files """ + + alias Mobilizon.Config + @base64_opts [padding: false] def url(nil), do: nil @@ -66,7 +69,7 @@ defmodule MobilizonWeb.MediaProxy 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", sig_base64, url_base64, diff --git a/lib/mobilizon_web/plugs/uploaded_media.ex b/lib/mobilizon_web/plugs/uploaded_media.ex index f755e0e87..13164a8d5 100644 --- a/lib/mobilizon_web/plugs/uploaded_media.ex +++ b/lib/mobilizon_web/plugs/uploaded_media.ex @@ -8,10 +8,14 @@ defmodule MobilizonWeb.Plugs.UploadedMedia do Serves uploaded media files """ + @behaviour Plug + import Plug.Conn + + alias Mobilizon.Config + require Logger - @behaviour Plug # no slashes @path "media" @@ -38,7 +42,7 @@ defmodule MobilizonWeb.Plugs.UploadedMedia do conn end - config = Mobilizon.CommonConfig.get([MobilizonWeb.Upload]) + config = Config.get([MobilizonWeb.Upload]) with uploader <- Keyword.fetch!(config, :uploader), proxy_remote = Keyword.get(config, :proxy_remote, false), @@ -75,7 +79,7 @@ defmodule MobilizonWeb.Plugs.UploadedMedia do conn |> MobilizonWeb.ReverseProxy.call( url, - Mobilizon.CommonConfig.get([Mobilizon.Upload, :proxy_opts], []) + Config.get([Mobilizon.Upload, :proxy_opts], []) ) end diff --git a/lib/mobilizon_web/resolvers/config.ex b/lib/mobilizon_web/resolvers/config.ex index 3cb632da9..dc762c1e2 100644 --- a/lib/mobilizon_web/resolvers/config.ex +++ b/lib/mobilizon_web/resolvers/config.ex @@ -1,19 +1,19 @@ defmodule MobilizonWeb.Resolvers.Config do @moduledoc """ - Handles the config-related GraphQL calls + Handles the config-related GraphQL calls. """ - import Mobilizon.CommonConfig + alias Mobilizon.Config @doc """ - Get config + Gets config. """ def get_config(_parent, _params, _context) do {:ok, %{ - name: instance_name(), - registrations_open: registrations_open?(), - description: instance_description() + name: Config.instance_name(), + registrations_open: Config.instance_registrations_open?(), + description: Config.instance_description() }} end end diff --git a/lib/mobilizon_web/resolvers/user.ex b/lib/mobilizon_web/resolvers/user.ex index d25b971f2..5d954f35c 100644 --- a/lib/mobilizon_web/resolvers/user.ex +++ b/lib/mobilizon_web/resolvers/user.ex @@ -2,12 +2,14 @@ defmodule MobilizonWeb.Resolvers.User do @moduledoc """ Handles the user-related GraphQL calls """ + + alias Mobilizon.{Actors, Config, Users} alias Mobilizon.Actors.Actor - alias Mobilizon.CommonConfig - alias Mobilizon.Users.User - alias Mobilizon.{Actors, Users} alias Mobilizon.Service.Users.{ResetPassword, Activation} + alias Mobilizon.Users.User + import Mobilizon.Users.Guards + require Logger @doc """ @@ -110,7 +112,7 @@ defmodule MobilizonWeb.Resolvers.User do """ @spec create_user(any(), map(), any()) :: tuple() 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 Activation.send_confirmation_email(user) {:ok, user} diff --git a/lib/mobilizon_web/upload.ex b/lib/mobilizon_web/upload.ex index 486d820f1..b245f74b6 100644 --- a/lib/mobilizon_web/upload.ex +++ b/lib/mobilizon_web/upload.ex @@ -31,7 +31,11 @@ defmodule MobilizonWeb.Upload do * `MobilizonWeb.Upload.Filter` """ + alias Ecto.UUID + + alias Mobilizon.Config + require Logger @type source :: @@ -110,26 +114,26 @@ defmodule MobilizonWeb.Upload do {size_limit, activity_type} = case Keyword.get(opts, :type) do :banner -> - {Mobilizon.CommonConfig.get!([:instance, :banner_upload_limit]), "Image"} + {Config.get!([:instance, :banner_upload_limit]), "Image"} :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 %{ activity_type: Keyword.get(opts, :activity_type, activity_type), size_limit: Keyword.get(opts, :size_limit, size_limit), - uploader: Keyword.get(opts, :uploader, Mobilizon.CommonConfig.get([__MODULE__, :uploader])), - filters: Keyword.get(opts, :filters, Mobilizon.CommonConfig.get([__MODULE__, :filters])), + uploader: Keyword.get(opts, :uploader, Config.get([__MODULE__, :uploader])), + filters: Keyword.get(opts, :filters, Config.get([__MODULE__, :filters])), description: Keyword.get(opts, :description), base_url: Keyword.get( opts, :base_url, - Mobilizon.CommonConfig.get([__MODULE__, :base_url], MobilizonWeb.Endpoint.url()) + Config.get([__MODULE__, :base_url], MobilizonWeb.Endpoint.url()) ) } end @@ -173,7 +177,7 @@ defmodule MobilizonWeb.Upload do defp url_from_spec(%__MODULE__{name: name}, base_url, {:file, path}) do path = 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)}" else "" diff --git a/lib/mobilizon_web/upload/filter/anonymize_filename.ex b/lib/mobilizon_web/upload/filter/anonymize_filename.ex index 57c5cbb76..290b9df5a 100644 --- a/lib/mobilizon_web/upload/filter/anonymize_filename.ex +++ b/lib/mobilizon_web/upload/filter/anonymize_filename.ex @@ -9,11 +9,14 @@ defmodule MobilizonWeb.Upload.Filter.AnonymizeFilename do Should be used after `MobilizonWeb.Upload.Filter.Dedupe`. """ + @behaviour MobilizonWeb.Upload.Filter + alias Mobilizon.Config + def filter(upload) do 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}} end diff --git a/lib/mobilizon_web/upload/filter/mogrify.ex b/lib/mobilizon_web/upload/filter/mogrify.ex index f0d1f42c2..e7ae715cd 100644 --- a/lib/mobilizon_web/upload/filter/mogrify.ex +++ b/lib/mobilizon_web/upload/filter/mogrify.ex @@ -7,13 +7,16 @@ defmodule MobilizonWeb.Upload.Filter.Mogrify do @moduledoc """ Handle mogrify transformations """ + @behaviour MobilizonWeb.Upload.Filter + alias Mobilizon.Config + @type conversion :: action :: String.t() | {action :: String.t(), opts :: String.t()} @type conversions :: conversion() | [conversion()] def filter(%MobilizonWeb.Upload{tempfile: file, content_type: "image" <> _}) do - filters = Mobilizon.CommonConfig.get!([__MODULE__, :args]) + filters = Config.get!([__MODULE__, :args]) file |> Mogrify.open() diff --git a/lib/mobilizon_web/uploaders/local.ex b/lib/mobilizon_web/uploaders/local.ex index 82d87ae56..4e5f0ea99 100644 --- a/lib/mobilizon_web/uploaders/local.ex +++ b/lib/mobilizon_web/uploaders/local.ex @@ -7,8 +7,11 @@ defmodule MobilizonWeb.Uploaders.Local do @moduledoc """ Local uploader for files """ + @behaviour MobilizonWeb.Uploaders.Uploader + alias Mobilizon.Config + def get_file(_) do {:ok, {:static_dir, upload_path()}} end @@ -59,6 +62,6 @@ defmodule MobilizonWeb.Uploaders.Local do end def upload_path do - Mobilizon.CommonConfig.get!([__MODULE__, :uploads]) + Config.get!([__MODULE__, :uploads]) end end diff --git a/lib/service/activity_pub/activity_pub.ex b/lib/service/activity_pub/activity_pub.ex index 6cad45aab..c0d98a0ae 100644 --- a/lib/service/activity_pub/activity_pub.ex +++ b/lib/service/activity_pub/activity_pub.ex @@ -10,6 +10,7 @@ defmodule Mobilizon.Service.ActivityPub do Every ActivityPub method """ + alias Mobilizon.Config alias Mobilizon.Events alias Mobilizon.Events.{Event, Comment, Participant} alias Mobilizon.Service.ActivityPub.Transmogrifier @@ -521,7 +522,7 @@ defmodule Mobilizon.Service.ActivityPub do 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) Mobilizon.Service.ActivityPub.Relay.publish(activity) end diff --git a/test/mobilizon/actors/actors_test.exs b/test/mobilizon/actors/actors_test.exs index 6b2ae6917..2c589a0da 100644 --- a/test/mobilizon/actors/actors_test.exs +++ b/test/mobilizon/actors/actors_test.exs @@ -1,9 +1,8 @@ defmodule Mobilizon.ActorsTest do use Mobilizon.DataCase - alias Mobilizon.Actors + alias Mobilizon.{Actors, Config, Users} alias Mobilizon.Actors.{Actor, Member, Follower, Bot} - alias Mobilizon.Users alias Mobilizon.Media.File, as: FileModel import Mobilizon.Factory use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney @@ -281,12 +280,12 @@ defmodule Mobilizon.ActorsTest do %URI{path: "/media/" <> banner_path} = URI.parse(banner_url) assert File.exists?( - Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> + Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/" <> avatar_path ) assert File.exists?( - Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> + Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/" <> banner_path ) @@ -312,12 +311,12 @@ defmodule Mobilizon.ActorsTest do refute actor.suspended refute File.exists?( - Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> + Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/" <> avatar_path ) assert File.exists?( - Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> + Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/" <> banner_path ) end @@ -335,12 +334,12 @@ defmodule Mobilizon.ActorsTest do %URI{path: "/media/" <> banner_path} = URI.parse(banner_url) assert File.exists?( - Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> + Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/" <> avatar_path ) assert File.exists?( - Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> + Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/" <> banner_path ) @@ -348,7 +347,7 @@ defmodule Mobilizon.ActorsTest do assert_raise Ecto.NoResultsError, fn -> Actors.get_actor!(actor_id) end refute File.exists?( - Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> + Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/" <> banner_path ) end diff --git a/test/mobilizon/media/media_test.exs b/test/mobilizon/media/media_test.exs index 50b3b96ee..2fb5e3f21 100644 --- a/test/mobilizon/media/media_test.exs +++ b/test/mobilizon/media/media_test.exs @@ -1,9 +1,10 @@ defmodule Mobilizon.MediaTest do use Mobilizon.DataCase - alias Mobilizon.Media import Mobilizon.Factory + alias Mobilizon.{Config, Media} + describe "media" do setup [:ensure_local_uploader] alias Mobilizon.Media.Picture @@ -48,7 +49,7 @@ defmodule Mobilizon.MediaTest do %URI{path: "/media/" <> path} = URI.parse(picture.file.url) assert File.exists?( - Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> + Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/" <> path ) @@ -56,7 +57,7 @@ defmodule Mobilizon.MediaTest do assert_raise Ecto.NoResultsError, fn -> Media.get_picture!(picture.id) end refute File.exists?( - Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> + Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/" <> path ) end diff --git a/test/mobilizon_web/controllers/activity_pub_controller_test.exs b/test/mobilizon_web/controllers/activity_pub_controller_test.exs index df97d7763..54cb958da 100644 --- a/test/mobilizon_web/controllers/activity_pub_controller_test.exs +++ b/test/mobilizon_web/controllers/activity_pub_controller_test.exs @@ -8,7 +8,7 @@ defmodule MobilizonWeb.ActivityPubControllerTest do import Mobilizon.Factory alias MobilizonWeb.ActivityPub.ActorView alias MobilizonWeb.PageView - alias Mobilizon.Actors + alias Mobilizon.{Actors, Config} alias Mobilizon.Actors.Actor alias Mobilizon.Service.ActivityPub use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney @@ -290,14 +290,14 @@ defmodule MobilizonWeb.ActivityPubControllerTest do end 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 |> get(activity_pub_path(conn, :relay)) |> json_response(404) |> assert - Mobilizon.CommonConfig.put([:instance, :allow_relay], true) + Config.put([:instance, :allow_relay], true) end end diff --git a/test/mobilizon_web/controllers/nodeinfo_controller_test.exs b/test/mobilizon_web/controllers/nodeinfo_controller_test.exs index 44592c05c..9cd94f63f 100644 --- a/test/mobilizon_web/controllers/nodeinfo_controller_test.exs +++ b/test/mobilizon_web/controllers/nodeinfo_controller_test.exs @@ -1,7 +1,7 @@ defmodule MobilizonWeb.NodeInfoControllerTest do use MobilizonWeb.ConnCase - @instance Application.get_env(:mobilizon, :instance) + alias Mobilizon.Config test "Get node info schemas", %{conn: conn} do conn = get(conn, node_info_path(conn, :schemas)) @@ -37,16 +37,16 @@ defmodule MobilizonWeb.NodeInfoControllerTest do assert resp == %{ "metadata" => %{ - "nodeName" => Mobilizon.CommonConfig.instance_name(), - "nodeDescription" => Mobilizon.CommonConfig.instance_description() + "nodeName" => Config.instance_name(), + "nodeDescription" => Config.instance_description() }, - "openRegistrations" => Keyword.get(@instance, :registrations_open), + "openRegistrations" => Config.instance_registrations_open?(), "protocols" => ["activitypub"], "services" => %{"inbound" => [], "outbound" => ["atom1.0"]}, "software" => %{ "name" => "mobilizon", - "version" => Keyword.get(@instance, :version), - "repository" => Keyword.get(@instance, :repository) + "version" => Config.instance_version(), + "repository" => Config.instance_repository() }, "usage" => %{"localComments" => 0, "localPosts" => 0, "users" => %{"total" => 0}}, "version" => "2.1" diff --git a/test/mobilizon_web/media_proxy_test.exs b/test/mobilizon_web/media_proxy_test.exs index 912806c59..46ba74420 100644 --- a/test/mobilizon_web/media_proxy_test.exs +++ b/test/mobilizon_web/media_proxy_test.exs @@ -5,18 +5,22 @@ defmodule MobilizonWeb.MediaProxyTest do use ExUnit.Case + import MobilizonWeb.MediaProxy + + alias Mobilizon.Config + alias MobilizonWeb.MediaProxyController setup do - enabled = Mobilizon.CommonConfig.get([:media_proxy, :enabled]) - on_exit(fn -> Mobilizon.CommonConfig.put([:media_proxy, :enabled], enabled) end) + enabled = Config.get([:media_proxy, :enabled]) + on_exit(fn -> Config.put([:media_proxy, :enabled], enabled) end) :ok end describe "when enabled" do setup do - Mobilizon.CommonConfig.put([:media_proxy, :enabled], true) + Config.put([:media_proxy, :enabled], true) :ok end @@ -43,7 +47,7 @@ defmodule MobilizonWeb.MediaProxyTest do assert String.starts_with?( 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") @@ -80,15 +84,15 @@ defmodule MobilizonWeb.MediaProxyTest do end 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 -> - Mobilizon.CommonConfig.put([MobilizonWeb.Endpoint, :secret_key_base], secret_key_base) + Config.put([MobilizonWeb.Endpoint, :secret_key_base], secret_key_base) end) encoded = url("https://pleroma.social") - Mobilizon.CommonConfig.put( + Config.put( [MobilizonWeb.Endpoint, :secret_key_base], "00000000000000000000000000000000000000000000000" ) @@ -126,20 +130,20 @@ defmodule MobilizonWeb.MediaProxyTest do end 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 on_exit(fn -> - Mobilizon.CommonConfig.put([:media_proxy, :base_url], base_url) + Config.put([:media_proxy, :base_url], base_url) 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" 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 # https://git.pleroma.social/pleroma/pleroma/issues/580 @@ -154,13 +158,13 @@ defmodule MobilizonWeb.MediaProxyTest do describe "when disabled" do setup do - enabled = Mobilizon.CommonConfig.get([:media_proxy, :enabled]) + enabled = Config.get([:media_proxy, :enabled]) if enabled do - Mobilizon.CommonConfig.put([:media_proxy, :enabled], false) + Config.put([:media_proxy, :enabled], false) on_exit(fn -> - Mobilizon.CommonConfig.put([:media_proxy, :enabled], enabled) + Config.put([:media_proxy, :enabled], enabled) :ok end) end diff --git a/test/mobilizon_web/resolvers/user_resolver_test.exs b/test/mobilizon_web/resolvers/user_resolver_test.exs index b996d7ea9..52e37c6e9 100644 --- a/test/mobilizon_web/resolvers/user_resolver_test.exs +++ b/test/mobilizon_web/resolvers/user_resolver_test.exs @@ -1,6 +1,6 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do use MobilizonWeb.ConnCase - alias Mobilizon.{Actors, Users, CommonConfig} + alias Mobilizon.{Actors, Config, Users} alias Mobilizon.Actors.Actor alias Mobilizon.Users.User alias Mobilizon.Users @@ -401,7 +401,7 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do end 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 { createUser( diff --git a/test/mobilizon_web/upload_test.exs b/test/mobilizon_web/upload_test.exs index 4434d0937..de41d172f 100644 --- a/test/mobilizon_web/upload_test.exs +++ b/test/mobilizon_web/upload_test.exs @@ -4,9 +4,12 @@ # Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/upload_test.ex defmodule Mobilizon.UploadTest do - alias MobilizonWeb.Upload use Mobilizon.DataCase + alias Mobilizon.Config + + alias MobilizonWeb.Upload + describe "Storing a file with the Local uploader" do setup [:ensure_local_uploader] @@ -185,7 +188,7 @@ defmodule Mobilizon.UploadTest do test "delete a not existing file" do file = - Mobilizon.CommonConfig.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> + Config.get!([MobilizonWeb.Uploaders.Local, :uploads]) <> "/not_existing/definitely.jpg" refute File.exists?(file) @@ -215,6 +218,6 @@ defmodule Mobilizon.UploadTest do assert String.starts_with?(url, MobilizonWeb.Endpoint.url() <> "/media/") %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 diff --git a/test/support/data_case.ex b/test/support/data_case.ex index bb22f12bf..b4c47f9f5 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -14,6 +14,8 @@ defmodule Mobilizon.DataCase do use ExUnit.CaseTemplate + alias Mobilizon.Config + using do quote do alias Mobilizon.Repo @@ -52,16 +54,16 @@ defmodule Mobilizon.DataCase do end def ensure_local_uploader(_context) do - uploader = Mobilizon.CommonConfig.get([MobilizonWeb.Upload, :uploader]) - filters = Mobilizon.CommonConfig.get([MobilizonWeb.Upload, :filters]) + uploader = Config.get([MobilizonWeb.Upload, :uploader]) + filters = Config.get([MobilizonWeb.Upload, :filters]) unless uploader == MobilizonWeb.Uploaders.Local || filters != [] do - Mobilizon.CommonConfig.put([MobilizonWeb.Upload, :uploader], MobilizonWeb.Uploaders.Local) - Mobilizon.CommonConfig.put([MobilizonWeb.Upload, :filters], []) + Config.put([MobilizonWeb.Upload, :uploader], MobilizonWeb.Uploaders.Local) + Config.put([MobilizonWeb.Upload, :filters], []) on_exit(fn -> - Mobilizon.CommonConfig.put([MobilizonWeb.Upload, :uploader], uploader) - Mobilizon.CommonConfig.put([MobilizonWeb.Upload, :filters], filters) + Config.put([MobilizonWeb.Upload, :uploader], uploader) + Config.put([MobilizonWeb.Upload, :filters], filters) end) end