Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
arnej27959 committed Nov 12, 2024
1 parent f1aa6b5 commit 0d8df6c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
12 changes: 7 additions & 5 deletions client/go/internal/cli/cmd/auth_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -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
}
39 changes: 39 additions & 0 deletions client/go/internal/cli/cmd/auth_show_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 0d8df6c

Please sign in to comment.