Skip to content

Commit

Permalink
Merge pull request #90 from muaz-32/add-e2e-tests
Browse files Browse the repository at this point in the history
Added e2e for the `login` command using flags.
  • Loading branch information
Vad1mo authored May 30, 2024
2 parents 3b98c71 + c3f63d6 commit 24b2bd4
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 4 deletions.
6 changes: 3 additions & 3 deletions cmd/harbor/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func LoginCommand() *cobra.Command {
Short: "Log in to Harbor registry",
Long: "Authenticate with Harbor Registry.",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {

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

if err != nil {
fmt.Println(err)
return err
}

return nil
},
}

Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/catppuccin/go v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -29,14 +31,14 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
Expand Down
125 changes: 125 additions & 0 deletions test/e2e/login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package e2e

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

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

validServerAddresses := []string{
"http://demo.goharbor.io:80",
"https://demo.goharbor.io:8443",
"http://demo.goharbor.io",
"https://demo.goharbor.io",
"demo.goharbor.io",
}

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)
}
}

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

args := []string{
"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", "Harbor12345")
if err != nil {
t.Fatal(err)
}

err = cmd.Execute()

assert.Error(t, err)
}

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

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.Error(t, err)
}

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

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.Error(t, err)
}

0 comments on commit 24b2bd4

Please sign in to comment.