Skip to content

Commit a0e2ba8

Browse files
refactor: add function variables to auth commands for testability
- login: extract getConfig, confirmFromUser, selectFromOptions, getSingleLineInput - logout: extract getConfig, confirmFromUser - refresh: extract getConfig, newDeepsource - status: extract getConfig Enables mocking of dependencies in unit tests
1 parent 5d86ef7 commit a0e2ba8

4 files changed

Lines changed: 33 additions & 10 deletions

File tree

command/auth/login/login.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import (
99
"github.com/spf13/cobra"
1010
)
1111

12+
// Function variables for testing
13+
var (
14+
getConfigFn = config.GetConfig
15+
confirmFromUserFn = utils.ConfirmFromUser
16+
selectFromOptionsFn = utils.SelectFromOptions
17+
getSingleLineInputFn = utils.GetSingleLineInput
18+
)
19+
1220
var accountTypes = []string{"DeepSource (deepsource.io)", "DeepSource Enterprise"}
1321

1422
// LoginOptions hold the metadata related to login operation
@@ -64,7 +72,7 @@ func NewCmdLogin() *cobra.Command {
6472
// Run executes the auth command and starts the login flow if not already authenticated
6573
func (opts *LoginOptions) Run() (err error) {
6674
// Fetch config
67-
cfg, _ := config.GetConfig()
75+
cfg, _ := getConfigFn()
6876
opts.User = cfg.User
6977
opts.TokenExpired = cfg.IsExpired()
7078

@@ -92,7 +100,7 @@ func (opts *LoginOptions) Run() (err error) {
92100
if !opts.TokenExpired {
93101
// The user is already logged in, confirm re-authentication.
94102
msg := fmt.Sprintf("You're already logged into DeepSource as %s. Do you want to re-authenticate?", opts.User)
95-
response, err := utils.ConfirmFromUser(msg, "")
103+
response, err := confirmFromUserFn(msg, "")
96104
if err != nil {
97105
return fmt.Errorf("Error in fetching response. Please try again.")
98106
}
@@ -121,13 +129,13 @@ func (opts *LoginOptions) handleInteractiveLogin() error {
121129
hostPromptHelpText := "The hostname of the DeepSource instance to authenticate with"
122130

123131
// Display prompt to user
124-
loginType, err := utils.SelectFromOptions(loginPromptMessage, loginPromptHelpText, accountTypes)
132+
loginType, err := selectFromOptionsFn(loginPromptMessage, loginPromptHelpText, accountTypes)
125133
if err != nil {
126134
return err
127135
}
128136
// Prompt the user for hostname only in the case of on-premise
129137
if loginType == "DeepSource Enterprise" {
130-
opts.HostName, err = utils.GetSingleLineInput(hostPromptMessage, hostPromptHelpText)
138+
opts.HostName, err = getSingleLineInputFn(hostPromptMessage, hostPromptHelpText)
131139
if err != nil {
132140
return err
133141
}

command/auth/logout/logout.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13+
// Function variables for testing
14+
var (
15+
getConfigFn = config.GetConfig
16+
confirmFromUserFn = utils.ConfirmFromUser
17+
)
18+
1319
type LogoutOptions struct{}
1420

1521
// NewCmdLogout handles the logout functionality for the CLI
@@ -28,7 +34,7 @@ func NewCmdLogout() *cobra.Command {
2834

2935
func (opts *LogoutOptions) Run() error {
3036
// Fetch config
31-
cfg, err := config.GetConfig()
37+
cfg, err := getConfigFn()
3238
if err != nil {
3339
return fmt.Errorf("Error while reading DeepSource CLI config : %v", err)
3440
}
@@ -39,7 +45,7 @@ func (opts *LogoutOptions) Run() error {
3945

4046
// Confirm from the user if they want to logout
4147
logoutConfirmationMsg := "Are you sure you want to log out of DeepSource account?"
42-
response, err := utils.ConfirmFromUser(logoutConfirmationMsg, "")
48+
response, err := confirmFromUserFn(logoutConfirmationMsg, "")
4349
if err != nil {
4450
return err
4551
}

command/auth/refresh/refresh.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import (
1313
"github.com/spf13/cobra"
1414
)
1515

16+
// Function variables for testing
17+
var (
18+
getConfigFn = config.GetConfig
19+
newDeepsourceFn = deepsource.New
20+
)
21+
1622
type RefreshOptions struct{}
1723

1824
// NewCmdRefresh handles the refreshing of authentication credentials
@@ -41,7 +47,7 @@ func NewCmdRefresh() *cobra.Command {
4147

4248
func (opts *RefreshOptions) Run() error {
4349
// Fetch config
44-
cfg, err := config.GetConfig()
50+
cfg, err := getConfigFn()
4551
if err != nil {
4652
return fmt.Errorf("Error while reading DeepSource CLI config : %v", err)
4753
}
@@ -51,7 +57,7 @@ func (opts *RefreshOptions) Run() error {
5157
}
5258

5359
// Fetching DS Client
54-
deepsource, err := deepsource.New(deepsource.ClientOpts{
60+
dsClient, err := newDeepsourceFn(deepsource.ClientOpts{
5561
Token: config.Cfg.Token,
5662
HostName: config.Cfg.Host,
5763
})
@@ -60,7 +66,7 @@ func (opts *RefreshOptions) Run() error {
6066
}
6167
ctx := context.Background()
6268
// Use the SDK to fetch the new auth data
63-
refreshedConfigData, err := deepsource.RefreshAuthCreds(ctx, cfg.Token)
69+
refreshedConfigData, err := dsClient.RefreshAuthCreds(ctx, cfg.Token)
6470
if err != nil {
6571
return err
6672
}

command/auth/status/status.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14+
// Function variable for testing - allows mocking config.GetConfig
15+
var getConfigFn = config.GetConfig
16+
1417
type AuthStatusOptions struct{}
1518

1619
// NewCmdStatus handles the fetching of authentication status of CLI
@@ -36,7 +39,7 @@ func NewCmdStatus() *cobra.Command {
3639

3740
func (opts *AuthStatusOptions) Run() error {
3841
// Fetch config
39-
cfg, err := config.GetConfig()
42+
cfg, err := getConfigFn()
4043
if err != nil {
4144
return fmt.Errorf("Error while reading DeepSource CLI config : %v", err)
4245
}

0 commit comments

Comments
 (0)