-
Notifications
You must be signed in to change notification settings - Fork 136
tests: Added unit tests for user list command and user view #685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ package user | |||||||
| import ( | ||||||||
| "fmt" | ||||||||
|
|
||||||||
| "github.com/goharbor/go-client/pkg/sdk/v2.0/client/user" | ||||||||
| "github.com/goharbor/go-client/pkg/sdk/v2.0/models" | ||||||||
| "github.com/goharbor/harbor-cli/pkg/api" | ||||||||
| "github.com/goharbor/harbor-cli/pkg/utils" | ||||||||
|
|
@@ -25,68 +26,78 @@ import ( | |||||||
| "github.com/spf13/viper" | ||||||||
| ) | ||||||||
|
|
||||||||
| func UserListCmd() *cobra.Command { | ||||||||
| var opts api.ListFlags | ||||||||
| func GetUsers(opts api.ListFlags, userListAPI func(opts ...api.ListFlags) (*user.ListUsersOK, error)) ([]*models.UserResp, error) { | ||||||||
| var allUsers []*models.UserResp | ||||||||
|
|
||||||||
| cmd := &cobra.Command{ | ||||||||
| Use: "list", | ||||||||
| Short: "List users", | ||||||||
| Args: cobra.ExactArgs(0), | ||||||||
| Aliases: []string{"ls"}, | ||||||||
| RunE: func(cmd *cobra.Command, args []string) error { | ||||||||
| if opts.PageSize < 0 { | ||||||||
| return fmt.Errorf("page size must be greater than or equal to 0") | ||||||||
| } | ||||||||
|
|
||||||||
| if opts.PageSize > 100 { | ||||||||
| return fmt.Errorf("page size should be less than or equal to 100") | ||||||||
| } | ||||||||
| if opts.PageSize > 100 || opts.PageSize < 0 { | ||||||||
| return nil, fmt.Errorf("page size should be greater than or equal to 0 and less than or equal to 100") | ||||||||
| } | ||||||||
|
|
||||||||
| if opts.PageSize == 0 { | ||||||||
| opts.PageSize = 100 | ||||||||
| opts.Page = 1 | ||||||||
| if opts.PageSize == 0 { | ||||||||
| opts.PageSize = 100 | ||||||||
| opts.Page = 1 | ||||||||
|
|
||||||||
| for { | ||||||||
| response, err := api.ListUsers(opts) | ||||||||
| if err != nil { | ||||||||
| if isUnauthorizedError(err) { | ||||||||
| return fmt.Errorf("Permission denied: Admin privileges are required to execute this command.") | ||||||||
| } | ||||||||
| return fmt.Errorf("failed to list users: %v", err) | ||||||||
| } | ||||||||
| for { | ||||||||
| response, err := userListAPI(opts) | ||||||||
| if err != nil { | ||||||||
| if isUnauthorizedError(err) { | ||||||||
| return nil, fmt.Errorf("Permission denied: Admin privileges are required to execute this command.") | ||||||||
| } | ||||||||
| return nil, fmt.Errorf("failed to list users: %w", err) | ||||||||
| } | ||||||||
|
Comment on lines
+43
to
+47
|
||||||||
|
|
||||||||
| allUsers = append(allUsers, response.Payload...) | ||||||||
| allUsers = append(allUsers, response.Payload...) | ||||||||
|
|
||||||||
| if len(response.Payload) < int(opts.PageSize) { | ||||||||
| break | ||||||||
| } | ||||||||
| opts.Page++ | ||||||||
| } | ||||||||
| } else { | ||||||||
| response, err := api.ListUsers(opts) | ||||||||
| if err != nil { | ||||||||
| if isUnauthorizedError(err) { | ||||||||
| return fmt.Errorf("Permission denied: Admin privileges are required to execute this command.") | ||||||||
| } | ||||||||
| return fmt.Errorf("failed to list users: %v", err) | ||||||||
| } | ||||||||
| allUsers = response.Payload | ||||||||
| if len(response.Payload) < int(opts.PageSize) { | ||||||||
| break | ||||||||
| } | ||||||||
| if len(allUsers) == 0 { | ||||||||
| log.Info("No users found") | ||||||||
| return nil | ||||||||
| opts.Page++ | ||||||||
| } | ||||||||
| } else { | ||||||||
| response, err := userListAPI(opts) | ||||||||
| if err != nil { | ||||||||
| if isUnauthorizedError(err) { | ||||||||
| return nil, fmt.Errorf("Permission denied: Admin privileges are required to execute this command.") | ||||||||
| } | ||||||||
| formatFlag := viper.GetString("output-format") | ||||||||
| if formatFlag != "" { | ||||||||
| err := utils.PrintFormat(allUsers, formatFlag) | ||||||||
| if err != nil { | ||||||||
| log.Error(err) | ||||||||
| } | ||||||||
| } else { | ||||||||
| list.ListUsers(allUsers) | ||||||||
| return nil, fmt.Errorf("failed to list users: %w", err) | ||||||||
| } | ||||||||
|
Comment on lines
+59
to
+63
|
||||||||
| allUsers = response.Payload | ||||||||
| } | ||||||||
| return allUsers, nil | ||||||||
| } | ||||||||
| func PrintUsers(allUsers []*models.UserResp) error { | ||||||||
| if len(allUsers) == 0 { | ||||||||
| log.Info("No users found") | ||||||||
| return nil | ||||||||
| } | ||||||||
| formatFlag := viper.GetString("output-format") | ||||||||
| if formatFlag != "" { | ||||||||
| err := utils.PrintFormat(allUsers, formatFlag) | ||||||||
| if err != nil { | ||||||||
| log.Error(err) | ||||||||
|
||||||||
| log.Error(err) | |
| log.Error(err) | |
| return err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this file’s
package user, importinggithub.com/goharbor/go-client/pkg/sdk/v2.0/client/userasuseris very easy to confuse with the current package name and makes types like*user.ListUsersOKhard to read. Consider aliasing the import (e.g.,clientuser) and updating the interface return type accordingly.