From b695881a2d2f650afb8a4e0d761ac017a7d95a90 Mon Sep 17 00:00:00 2001 From: Jamil Bou Kheir Date: Wed, 30 Jun 2021 07:38:58 -0700 Subject: [PATCH] config file tests --- apps/cf_common/lib/config_file.ex | 44 +++++++++++++++--------- apps/cf_common/lib/fake_file.ex | 23 +++++++++++++ apps/cf_common/test/config_file_test.exs | 43 +++++++++++++++++++++++ config/config.exs | 10 +++--- config/test.exs | 2 ++ 5 files changed, 101 insertions(+), 21 deletions(-) create mode 100644 apps/cf_common/lib/fake_file.ex create mode 100644 apps/cf_common/test/config_file_test.exs diff --git a/apps/cf_common/lib/config_file.ex b/apps/cf_common/lib/config_file.ex index 9fca0fdbb..4967135a0 100644 --- a/apps/cf_common/lib/config_file.ex +++ b/apps/cf_common/lib/config_file.ex @@ -17,57 +17,67 @@ defmodule CfCommon.ConfigFile do # Write then load, ensures clean slate def init! do - Map.merge(@default_config, generate_config()) + mkdir!() + + Map.merge(@default_config, generate_config!()) |> write!() load!() end def load! do - %{} = Jason.decode!(File.read!(@config_path)) + %{} = Jason.decode!(file_module().read!(@config_path)) end def write!(config) do @config_path - |> File.write!(Jason.encode!(config), [:write]) + |> file_module().write!(Jason.encode!(config), [:write]) end def exists? do - File.exists?(@config_path) + file_module().exists?(@config_path) end - defp generate_config do + defp generate_config! do %{ - live_view_signing_salt: live_view_signing_salt(), - secret_key_base: secret_key_base(), - database_url: database_url(), - db_encryption_key: db_encryption_key(), - url_host: url_host(), - wg_server_key: wg_server_key() + live_view_signing_salt: live_view_signing_salt!(), + secret_key_base: secret_key_base!(), + database_url: database_url!(), + db_encryption_key: db_encryption_key!(), + url_host: url_host!(), + wg_server_key: wg_server_key!() } end - defp live_view_signing_salt do + defp mkdir! do + CLI.exec!("mkdir -p $HOME/.cloudfire") + end + + defp live_view_signing_salt! do CLI.exec!("openssl rand -base64 24") end - defp secret_key_base do + defp secret_key_base! do CLI.exec!("openssl rand -base64 48") end - defp db_encryption_key do + defp db_encryption_key! do CLI.exec!("openssl rand -base64 32") end - defp url_host do + defp url_host! do CLI.exec!("hostname") end - defp database_url do + defp database_url! do "ecto://postgres:postgres@127.0.0.1/cloudfire" end - defp wg_server_key do + defp wg_server_key! do CLI.exec!("wg genkey") end + + defp file_module do + Application.get_env(:cf_common, :config_file_module) + end end diff --git a/apps/cf_common/lib/fake_file.ex b/apps/cf_common/lib/fake_file.ex new file mode 100644 index 000000000..066f64971 --- /dev/null +++ b/apps/cf_common/lib/fake_file.ex @@ -0,0 +1,23 @@ +defmodule CfCommon.FakeFile do + @moduledoc """ + Provides mocked file operations for testing ConfigFile + """ + + @json ~s({ + "database_url": "ecto://postgres:postgres@127.0.0.1/cloudfire", + "secret_key_base": "fMjyDw9RpP5+f8klEmeEWnBQKd2H7uKH/PQpOTug6vybretclzaE1k4Y3O2Bw8lX", + "live_view_signing_salt": "EHcSipS+bFTFYMbFmvVR8lAuwYyfqcTE", + "db_key": "8Wgh3dPubt6q4Y1PlYRuG9v50zQE+QTUzh8mJnkw+jc=", + "ssl_cert_file": "$HOME/.cloudfire/ssl/cert.pem", + "ssl_key_file": "$HOME/.cloudfire/ssl/key.pem", + "url_host": "localhost", + "wg_server_key": "KDp9lQ6OAi/VrfgYo5VIAqCJFs1Gs55GZRDoA7W8500=", + "https_listen_port": "8800", + "https_listen_address": "127.0.0.1", + "wg_listen_port": "51820" + }) + + def read!(_), do: @json + def write!(_, _, [:write]), do: :ok + def exists?(_), do: true +end diff --git a/apps/cf_common/test/config_file_test.exs b/apps/cf_common/test/config_file_test.exs new file mode 100644 index 000000000..4460eeb9f --- /dev/null +++ b/apps/cf_common/test/config_file_test.exs @@ -0,0 +1,43 @@ +defmodule CfCommon.ConfigFileTest do + use ExUnit.Case, async: true + + alias CfCommon.ConfigFile + + @expected_config %{ + "database_url" => "ecto://postgres:postgres@127.0.0.1/cloudfire", + "secret_key_base" => "fMjyDw9RpP5+f8klEmeEWnBQKd2H7uKH/PQpOTug6vybretclzaE1k4Y3O2Bw8lX", + "live_view_signing_salt" => "EHcSipS+bFTFYMbFmvVR8lAuwYyfqcTE", + "db_key" => "8Wgh3dPubt6q4Y1PlYRuG9v50zQE+QTUzh8mJnkw+jc=", + "ssl_cert_file" => "$HOME/.cloudfire/ssl/cert.pem", + "ssl_key_file" => "$HOME/.cloudfire/ssl/key.pem", + "url_host" => "localhost", + "wg_server_key" => "KDp9lQ6OAi/VrfgYo5VIAqCJFs1Gs55GZRDoA7W8500=", + "https_listen_port" => "8800", + "https_listen_address" => "127.0.0.1", + "wg_listen_port" => "51820" + } + + describe "init!" do + test "loads stubbed config" do + assert ConfigFile.init!() == @expected_config + end + end + + describe "load!" do + test "loads stubbed config" do + assert ConfigFile.load!() == @expected_config + end + end + + describe "write!" do + test "returns :ok" do + assert ConfigFile.write!(@expected_config) == :ok + end + end + + describe "exists?" do + test "returns true" do + assert ConfigFile.exists?() + end + end +end diff --git a/config/config.exs b/config/config.exs index 3cea70e44..697abbfe5 100644 --- a/config/config.exs +++ b/config/config.exs @@ -53,10 +53,6 @@ config :logger, :console, format: "$time $metadata[$level] $message\n", metadata: [:request_id] -# Import environment specific config. This must remain at the bottom -# of this file so it overrides the configuration defined above. -import_config "#{Mix.env()}.exs" - # Configures the vault config :cf_http, CfHttp.Vault, ciphers: [ @@ -73,3 +69,9 @@ config :cf_http, CfHttp.Vault, iv_length: 12 } ] + +config :cf_common, :config_file_module, File + +# Import environment specific config. This must remain at the bottom +# of this file so it overrides the configuration defined above. +import_config "#{Mix.env()}.exs" diff --git a/config/test.exs b/config/test.exs index 060eec0fc..f6c1ab3b2 100644 --- a/config/test.exs +++ b/config/test.exs @@ -47,3 +47,5 @@ config :logger, level: :warn config :cf_vpn, execute_iface_cmds: System.get_env("CI") === "true" + +config :cf_common, :config_file_module, CfCommon.FakeFile