Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
134 changes: 134 additions & 0 deletions cmd/login_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading