From 0d8df6c925c66f79d24493c29609c2b9ce6a04e7 Mon Sep 17 00:00:00 2001 From: Arne Juul Date: Tue, 12 Nov 2024 11:47:06 +0100 Subject: [PATCH] add unit test --- client/go/internal/cli/cmd/auth_show.go | 12 +++--- client/go/internal/cli/cmd/auth_show_test.go | 39 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 client/go/internal/cli/cmd/auth_show_test.go diff --git a/client/go/internal/cli/cmd/auth_show.go b/client/go/internal/cli/cmd/auth_show.go index 69c8bdb61a34..4a1e3ad88fa9 100644 --- a/client/go/internal/cli/cmd/auth_show.go +++ b/client/go/internal/cli/cmd/auth_show.go @@ -4,6 +4,7 @@ package cmd import ( + "bytes" "encoding/json" "fmt" "net/http" @@ -69,16 +70,17 @@ func doAuthShow(cli *CLI, args []string) error { if err = dec.Decode(&userResponse); err != nil { return err } - fmt.Printf("Logged in as: %s\n", userResponse.User.Email) + var output bytes.Buffer + fmt.Fprintf(&output, "Logged in as: %s\n", userResponse.User.Email) for tenant, data := range userResponse.Tenants { - fmt.Printf("Available tenant: %s\n", tenant) + fmt.Fprintf(&output, "Available tenant: %s\n", tenant) for idx, role := range data.Roles { if idx == 0 { - fmt.Printf(" your roles:") + fmt.Fprintf(&output, " your roles:") } - fmt.Printf(" %s", role) + fmt.Fprintf(&output, " %s", role) } - fmt.Printf("\n") } + cli.printSuccess(output.String()) return nil } diff --git a/client/go/internal/cli/cmd/auth_show_test.go b/client/go/internal/cli/cmd/auth_show_test.go new file mode 100644 index 000000000000..b5ee4310924d --- /dev/null +++ b/client/go/internal/cli/cmd/auth_show_test.go @@ -0,0 +1,39 @@ +// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Author: mpolden + +package cmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vespa-engine/vespa/client/go/internal/mock" +) + +func TestAuthShow(t *testing.T) { + t.Run("auth show", func(t *testing.T) { + testAuthShow(t, []string{"auth", "show"}) + }) +} + +func testAuthShow(t *testing.T, subcommand []string) { + cli, stdout, stderr := newTestCLI(t) + + err := cli.Run("config", "set", "target", "cloud") + assert.Nil(t, err) + err = cli.Run("config", "set", "application", "t1.a1") + assert.Nil(t, err) + err = cli.Run("auth", "api-key") + assert.Nil(t, err) + stdout.Reset() + stderr.Reset() + + httpClient := &mock.HTTPClient{} + httpClient.NextResponseString(200, `{"user":{"email":"foo@bar"}}`) + cli.httpClient = httpClient + + err = cli.Run(subcommand...) + assert.Nil(t, err) + assert.Contains(t, stderr.String(), "Authenticating with API key") + assert.Contains(t, stdout.String(), "Logged in as: foo@bar\n") +}