Skip to content

Commit

Permalink
Merge pull request #861 from aziontech/whoami-tests
Browse files Browse the repository at this point in the history
tests: add tests to whoami command
  • Loading branch information
PatrickMenoti authored Jul 1, 2024
2 parents d6cd8ac + 62540f7 commit 58a6253
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,8 @@ changelog:
- title: "Documentation Updates"
regexp: ^.*?docs(\([[:word:]]+\))??!?:.+$
order: 50
- title: "Tests"
regexp: ^.*?tests(\([[:word:]]+\))??!?:.+$
order: 60
- title: Other Work
order: 999
66 changes: 43 additions & 23 deletions pkg/cmd/whoami/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,63 @@ import (
"github.com/MakeNowJust/heredoc"
msg "github.com/aziontech/azion-cli/messages/whoami"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/iostreams"
"github.com/aziontech/azion-cli/pkg/output"
"github.com/aziontech/azion-cli/pkg/token"
"github.com/spf13/cobra"
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
whoamiCmd := &cobra.Command{
type WhoamiCmd struct {
Io *iostreams.IOStreams
ReadSettings func() (token.Settings, error)
F *cmdutil.Factory
}

func NewWhoamiCmd(f *cmdutil.Factory) *WhoamiCmd {
return &WhoamiCmd{
Io: f.IOStreams,
ReadSettings: token.ReadSettings,
F: f,
}
}

func NewCobraCmd(whoami *WhoamiCmd, f *cmdutil.Factory) *cobra.Command {
cobraCmd := &cobra.Command{
Use: msg.Usage,
Short: msg.ShortDescription,
Long: msg.LongDescription,
Example: heredoc.Doc(`
$ azion whoami
`),
$ azion whoami --help
`),
RunE: func(cmd *cobra.Command, args []string) error {
settings, err := token.ReadSettings()
if err != nil {
return err
}

if settings.Email == "" {
return msg.ErrorNotLoggedIn
}

whoamiOut := output.GeneralOutput{
Msg: fmt.Sprintf(settings.Email + "\n"),
Out: f.IOStreams.Out,
Flags: f.Flags,
}
return output.Print(&whoamiOut)
return whoami.run()
},
}

whoamiCmd.SetIn(f.IOStreams.In)
whoamiCmd.SetOut(f.IOStreams.Out)
whoamiCmd.SetErr(f.IOStreams.Err)
cobraCmd.Flags().BoolP("help", "h", false, msg.HelpFlag)

whoamiCmd.Flags().BoolP("help", "h", false, msg.HelpFlag)
return cobraCmd
}

return whoamiCmd
func NewCmd(f *cmdutil.Factory) *cobra.Command {
return NewCobraCmd(NewWhoamiCmd(f), f)
}

func (cmd *WhoamiCmd) run() error {
settings, err := cmd.ReadSettings()
if err != nil {
return err
}

if settings.Email == "" {
return msg.ErrorNotLoggedIn
}

whoamiOut := output.GeneralOutput{
Msg: fmt.Sprintf(settings.Email + "\n"),
Out: cmd.Io.Out,
Flags: cmd.F.Flags,
}
return output.Print(&whoamiOut)
}
81 changes: 81 additions & 0 deletions pkg/cmd/whoami/whoami_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package whoami

import (
"errors"
"testing"

msg "github.com/aziontech/azion-cli/messages/whoami"
"github.com/aziontech/azion-cli/pkg/httpmock"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/testutils"
"github.com/aziontech/azion-cli/pkg/token"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
)

func TestWhoami(t *testing.T) {
logger.New(zapcore.DebugLevel)

tests := []struct {
name string
tokenSettings token.Settings
mockReadError error
expectedOutput string
expectedError error
}{
{
name: "whoami - logged in",
tokenSettings: token.Settings{
Email: "test@example.com",
},
mockReadError: nil,
expectedOutput: "test@example.com\n",
expectedError: nil,
},
{
name: "whoami - not logged in",
tokenSettings: token.Settings{
Email: "",
},
mockReadError: nil,
expectedOutput: "",
expectedError: msg.ErrorNotLoggedIn,
},
{
name: "whoami - failed to read settings",
tokenSettings: token.Settings{},
mockReadError: errors.New("failed to get token dir"),
expectedOutput: "",
expectedError: errors.New("failed to get token dir"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockReadSettings := func() (token.Settings, error) {
return tt.tokenSettings, tt.mockReadError
}

mock := &httpmock.Registry{}

f, out, _ := testutils.NewFactory(mock)

whoamiCmd := &WhoamiCmd{
Io: f.IOStreams,
ReadSettings: mockReadSettings,
F: f,
}
cmd := NewCobraCmd(whoamiCmd, f)

_, err := cmd.ExecuteC()
if tt.expectedError != nil {
require.Error(t, err)
assert.Equal(t, tt.expectedError.Error(), err.Error())
} else {
require.NoError(t, err)
assert.Equal(t, tt.expectedOutput, out.String())
}
})
}
}

0 comments on commit 58a6253

Please sign in to comment.