Skip to content

Commit

Permalink
Merge pull request #134 from bupd/fixTestLogin
Browse files Browse the repository at this point in the history
Fix Login Test
  • Loading branch information
Vad1mo authored Jul 29, 2024
2 parents 4ccab2d + 7efb796 commit 5c50ce2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 93 deletions.
4 changes: 2 additions & 2 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
verbose bool
)

func initConfig() {
func InitConfig() {
viper.SetConfigType("yaml")

// cfgFile = viper.GetStering("config")
Expand Down Expand Up @@ -90,7 +90,7 @@ harbor help
// },
}

cobra.OnInitialize(initConfig)
cobra.OnInitialize(InitConfig)

root.PersistentFlags().StringVarP(&output, "output-format", "o", "", "Output format. One of: json|yaml")
root.PersistentFlags().StringVar(&cfgFile, "config", utils.DefaultConfigPath, "config file (default is $HOME/.harbor/config.yaml)")
Expand Down
6 changes: 4 additions & 2 deletions cmd/harbor/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func LoginCommand() *cobra.Command {
Long: "Authenticate with Harbor Registry.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

if len(args) > 0 {
serverAddress = args[0]
}
Expand All @@ -40,7 +39,8 @@ func LoginCommand() *cobra.Command {

var err error

if loginView.Server != "" && loginView.Username != "" && loginView.Password != "" && loginView.Name != "" {
if loginView.Server != "" && loginView.Username != "" && loginView.Password != "" &&
loginView.Name != "" {
err = runLogin(loginView)
} else {
err = createLoginView(&loginView)
Expand Down Expand Up @@ -75,6 +75,8 @@ func createLoginView(loginView *login.LoginView) error {
}

func runLogin(opts login.LoginView) error {
opts.Server = utils.FormatUrl(opts.Server)

clientConfig := &harbor.ClientSetConfig{
URL: opts.Server,
Username: opts.Username,
Expand Down
131 changes: 42 additions & 89 deletions test/e2e/login_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package e2e

import (
"testing"

"github.com/goharbor/harbor-cli/cmd/harbor/root"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_Success(t *testing.T) {
func initialize(t *testing.T) {
cds := root.RootCmd()
cds.Execute()
cmd := root.LoginCommand()
var err error
err := cds.Execute()
assert.NoError(t, err, "Expected no error for Root command")
}

func Test_Login_Success(t *testing.T) {
initialize(t) // Initialize the root command

cmd := root.LoginCommand()
validServerAddresses := []string{
"http://demo.goharbor.io:80",
"https://demo.goharbor.io:443",
Expand All @@ -21,107 +26,55 @@ func Test_Success(t *testing.T) {
}

for _, serverAddress := range validServerAddresses {
args := []string{
serverAddress,
}
cmd.SetArgs(args)

err = cmd.Flags().Set("name", "test")
if err != nil {
t.Fatal(err)
}
err = cmd.Flags().Set("username", "admin")
if err != nil {
t.Fatal(err)
}
err = cmd.Flags().Set("password", "Harbor12345")
if err != nil {
t.Fatal(err)
}

err = cmd.Execute()

assert.NoError(t, err)
t.Run("ValidServer_"+serverAddress, func(t *testing.T) {
args := []string{serverAddress}
cmd.SetArgs(args)

assert.NoError(t, cmd.Flags().Set("name", "test"))
assert.NoError(t, cmd.Flags().Set("username", "admin"))
assert.NoError(t, cmd.Flags().Set("password", "Harbor12345"))

err := cmd.Execute()
assert.NoError(t, err, "Expected no error for server: %s", serverAddress)
})
}
}

func Test_Failure_WrongServer(t *testing.T) {
func Test_Login_Failure_WrongServer(t *testing.T) {
cmd := root.LoginCommand()
var err error

args := []string{
"demo.goharbor.io",
}
args := []string{"wrongserver"}
cmd.SetArgs(args)

err = cmd.Flags().Set("name", "test")
if err != nil {
t.Fatal(err)
}
err = cmd.Flags().Set("username", "admin")
if err != nil {
t.Fatal(err)
}
err = cmd.Flags().Set("password", "Harbor12345")
if err != nil {
t.Fatal(err)
}

err = cmd.Execute()
assert.NoError(t, cmd.Flags().Set("name", "test"))
assert.NoError(t, cmd.Flags().Set("username", "admin"))
assert.NoError(t, cmd.Flags().Set("password", "Harbor12345"))

assert.Error(t, err)
err := cmd.Execute()
assert.Error(t, err, "Expected error for invalid server")
}

func Test_Failure_WrongUsername(t *testing.T) {
func Test_Login_Failure_WrongUsername(t *testing.T) {
cmd := root.LoginCommand()
var err error

args := []string{
"http://demo.goharbor.io",
}
args := []string{"http://demo.goharbor.io"}
cmd.SetArgs(args)

err = cmd.Flags().Set("name", "test")
if err != nil {
t.Fatal(err)
}
err = cmd.Flags().Set("username", "wrong")
if err != nil {
t.Fatal(err)
}
err = cmd.Flags().Set("password", "Harbor12345")
if err != nil {
t.Fatal(err)
}

err = cmd.Execute()
assert.NoError(t, cmd.Flags().Set("name", "test"))
assert.NoError(t, cmd.Flags().Set("username", "wrong"))
assert.NoError(t, cmd.Flags().Set("password", "Harbor12345"))

assert.Error(t, err)
err := cmd.Execute()
assert.Error(t, err, "Expected error for wrong username")
}

func Test_Failure_WrongPassword(t *testing.T) {
func Test_Login_Failure_WrongPassword(t *testing.T) {
cmd := root.LoginCommand()
var err error

args := []string{
"http://demo.goharbor.io",
}
args := []string{"http://demo.goharbor.io"}
cmd.SetArgs(args)

err = cmd.Flags().Set("name", "test")
if err != nil {
t.Fatal(err)
}
err = cmd.Flags().Set("username", "admin")
if err != nil {
t.Fatal(err)
}
err = cmd.Flags().Set("password", "wrong")
if err != nil {
t.Fatal(err)
}

err = cmd.Execute()
assert.NoError(t, cmd.Flags().Set("name", "test"))
assert.NoError(t, cmd.Flags().Set("username", "admin"))
assert.NoError(t, cmd.Flags().Set("password", "wrong"))

assert.Error(t, err)
err := cmd.Execute()
assert.Error(t, err, "Expected error for wrong password")
}

0 comments on commit 5c50ce2

Please sign in to comment.