-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #861 from aziontech/whoami-tests
tests: add tests to whoami command
- Loading branch information
Showing
3 changed files
with
127 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
}) | ||
} | ||
} |