From c2c5a775e7d8e68ea0a96b5d95f2e3ac5d45f1e9 Mon Sep 17 00:00:00 2001 From: Arya Rathore Date: Wed, 28 Oct 2026 01:18:20 +0530 Subject: [PATCH] fix: honor --config flag in login command (#594) The login command bypassed the global --config flag and hardcoded a call to config.DefaultLocalConfigPath(), causing credentials and contexts to always be written to the default ~/.config/microcks/config. Update cmd/login.go to use globalClientOpts.ConfigPath, falling back to DefaultLocalConfigPath if unset. Add unit tests verifying custom config paths are respected and preserved. Signed-off-by: Arya Rathore --- cmd/login.go | 10 ++-- cmd/login_test.go | 134 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 cmd/login_test.go diff --git a/cmd/login.go b/cmd/login.go index 5953ed6..eef2b71 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -110,9 +110,13 @@ microcks login http://localhost:8080 --sso --sso-launch-browser=false ClientSecret: "", } - configFile, err := config.DefaultLocalConfigPath() - if err != nil { - return err + configFile := globalClientOpts.ConfigPath + if configFile == "" { + var err error + configFile, err = config.DefaultLocalConfigPath() + if err != nil { + return err + } } localConfig, err := config.ReadLocalConfig(configFile) if err != nil { diff --git a/cmd/login_test.go b/cmd/login_test.go new file mode 100644 index 0000000..4b2e51d --- /dev/null +++ b/cmd/login_test.go @@ -0,0 +1,134 @@ +/* + * Copyright The Microcks Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "net/http" + "net/http/httptest" + "path/filepath" + "testing" + + "github.com/microcks/microcks-cli/pkg/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestLoginHonorsConfigFlag(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/keycloak/config" { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"enabled": false}`)) + return + } + http.NotFound(w, r) + })) + defer server.Close() + + customConfigPath := filepath.Join(t.TempDir(), "custom-config.yaml") + + out, err := executeCLIForTest(t, "login", server.URL, "--config", customConfigPath) + require.NoError(t, err, "output: %s", out) + assert.Contains(t, out, "No login required...") + assert.Contains(t, out, "Context '"+server.URL+"' updated") + + // Verify the custom configuration file was created and populated + cfg, err := config.ReadLocalConfig(customConfigPath) + require.NoError(t, err) + require.NotNil(t, cfg) + + assert.Equal(t, server.URL, cfg.CurrentContext) + require.Len(t, cfg.Contexts, 1) + assert.Equal(t, server.URL, cfg.Contexts[0].Name) + assert.Equal(t, server.URL, cfg.Contexts[0].Server) + + require.Len(t, cfg.Servers, 1) + assert.Equal(t, server.URL, cfg.Servers[0].Server) + assert.False(t, cfg.Servers[0].KeycloakEnable) + + require.Len(t, cfg.Users, 1) + assert.Equal(t, server.URL, cfg.Users[0].Name) +} + +func TestLoginHonorsConfigFlagWithCustomContextName(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/keycloak/config" { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"enabled": false}`)) + return + } + http.NotFound(w, r) + })) + defer server.Close() + + customConfigPath := filepath.Join(t.TempDir(), "custom-config.yaml") + + out, err := executeCLIForTest(t, "login", server.URL, "--name", "my-env", "--config", customConfigPath) + require.NoError(t, err, "output: %s", out) + assert.Contains(t, out, "Context 'my-env' updated") + + cfg, err := config.ReadLocalConfig(customConfigPath) + require.NoError(t, err) + require.NotNil(t, cfg) + + assert.Equal(t, "my-env", cfg.CurrentContext) + require.Len(t, cfg.Contexts, 1) + assert.Equal(t, "my-env", cfg.Contexts[0].Name) + assert.Equal(t, server.URL, cfg.Contexts[0].Server) +} + +func TestLoginPreservesExistingContextsInCustomConfig(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/keycloak/config" { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"enabled": false}`)) + return + } + http.NotFound(w, r) + })) + defer server.Close() + + customConfigPath := filepath.Join(t.TempDir(), "custom-config.yaml") + + initialCfg := config.LocalConfig{ + CurrentContext: "existing-env", + Contexts: []config.ContextRef{ + {Name: "existing-env", Server: "http://existing:8080", User: "existing-user"}, + }, + Servers: []config.Server{ + {Server: "http://existing:8080"}, + }, + Users: []config.User{ + {Name: "existing-user"}, + }, + } + require.NoError(t, config.WriteLocalConfig(initialCfg, customConfigPath)) + + out, err := executeCLIForTest(t, "login", server.URL, "--name", "new-env", "--config", customConfigPath) + require.NoError(t, err, "output: %s", out) + assert.Contains(t, out, "Context 'new-env' updated") + + cfg, err := config.ReadLocalConfig(customConfigPath) + require.NoError(t, err) + require.NotNil(t, cfg) + + assert.Equal(t, "new-env", cfg.CurrentContext) + require.Len(t, cfg.Contexts, 2) + assert.Equal(t, "existing-env", cfg.Contexts[0].Name) + assert.Equal(t, "new-env", cfg.Contexts[1].Name) + require.Len(t, cfg.Servers, 2) + require.Len(t, cfg.Users, 2) +}