From 20e5e83e95ab57f72ece70191a973bc7afe79ed3 Mon Sep 17 00:00:00 2001 From: Thuyen Phan Date: Fri, 19 Apr 2024 16:17:48 +0800 Subject: [PATCH 1/2] Add a test case where the SDK config is blank --- .../configurations/blank_config_default | 0 test/goth/config_test.exs | 24 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 test/data/home/gcloud/configurations/blank_config_default diff --git a/test/data/home/gcloud/configurations/blank_config_default b/test/data/home/gcloud/configurations/blank_config_default new file mode 100644 index 0000000..e69de29 diff --git a/test/goth/config_test.exs b/test/goth/config_test.exs index 3b8b1b1..d5342a0 100644 --- a/test/goth/config_test.exs +++ b/test/goth/config_test.exs @@ -150,6 +150,30 @@ defmodule Goth.ConfigTest do Application.start(:goth) end + test "Reading blank configuration should return nil" do + current_json = Application.get_env(:goth, :json) + Application.put_env(:goth, :json, nil, persistent: true) + + current_config_root = Application.get_env(:goth, :config_root_dir) + + config_root = Path.expand("test/data/home/gcloud") + Application.put_env(:goth, :config_root_dir, config_root) + + Application.stop(:goth) + Application.start(:goth) + + data = + Path.expand("test/data/home/gcloud/configurations/blank_config_default") + |> Config.get_configuration_data() + + assert(data == nil) + + Application.put_env(:goth, :config_root_dir, current_config_root, persistent: true) + Application.put_env(:goth, :json, current_json, persistent: true) + Application.stop(:goth) + Application.start(:goth) + end + test "GOOGLE_APPLICATION_CREDENTIALS is read" do # The test configuration sets an example JSON blob. We override it briefly # during this test. From 5051fe930e507f718c4c4994b1b684b11ddeba0a Mon Sep 17 00:00:00 2001 From: Thuyen Phan Date: Fri, 19 Apr 2024 16:18:14 +0800 Subject: [PATCH 2/2] Return nil instead when loading an empty SDK config file --- lib/goth/config.ex | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/goth/config.ex b/lib/goth/config.ex index 82439dc..cf7b29f 100644 --- a/lib/goth/config.ex +++ b/lib/goth/config.ex @@ -232,13 +232,14 @@ defmodule Goth.Config do end def get_configuration_data(configuration_file) do - if File.regular?(configuration_file) do - configuration_data = configuration_file |> File.read!() |> decode_ini() - - # Only retrieve the required data. - %{"project_id" => configuration_data["core"]["project"], "actor_email" => configuration_data["core"]["account"]} + with true <- File.regular?(configuration_file), + configuration_data <- configuration_file |> File.read!() |> decode_ini(), + project_id when not is_nil(project_id) <- configuration_data["core"]["project"], + actor_email when not is_nil(actor_email) <- configuration_data["core"]["account"] do + %{"project_id" => project_id, "actor_email" => actor_email} else - nil + _ -> + nil end end