config file tests

This commit is contained in:
Jamil Bou Kheir
2021-06-30 07:38:58 -07:00
parent b3ad575265
commit b695881a2d
5 changed files with 101 additions and 21 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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"

View File

@@ -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