[go: nahoru, domu]

Skip to content

Commit

Permalink
Added tests for failure conditions (#6501)
Browse files Browse the repository at this point in the history
Added unit tests for registryctl auth to cover failure conditions to increase test coverage.
New tests cover no secret specified and incorrect harbor secret prefix

Signed-off-by: Brett Johnson <brett@sdbrett.com>
  • Loading branch information
SDBrett authored and wy65701436 committed Dec 13, 2018
1 parent 3a38ff9 commit 0e260d1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/registryctl/auth/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type secretHandler struct {
secrets map[string]string
}

// NewSecretHandler creaters a new authentiation handler which adds
// NewSecretHandler creates a new authentication handler which adds
// basic authentication credentials to a request.
func NewSecretHandler(secrets map[string]string) AuthenticationHandler {
return &secretHandler{
Expand Down
43 changes: 43 additions & 0 deletions src/registryctl/auth/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package auth

import (
"fmt"
"net/http"
"testing"

Expand Down Expand Up @@ -49,3 +50,45 @@ func TestAuthorizeRequestValid(t *testing.T) {
assert.Nil(t, err)

}

func TestNilRequest(t *testing.T) {
secret := "Correct"
req, err := http.NewRequest("", "", nil)
req = nil
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
_ = commonsecret.AddToRequest(req, secret)

authenticator := NewSecretHandler(map[string]string{"secret1": "correct"})
err = authenticator.AuthorizeRequest(req)
assert.Equal(t, err, ErrNoSecret)
}

func TestNoSecret(t *testing.T) {
secret := ""
req, err := http.NewRequest("", "", nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
_ = commonsecret.AddToRequest(req, secret)

authenticator := NewSecretHandler(map[string]string{})
err = authenticator.AuthorizeRequest(req)
assert.Equal(t, err, ErrNoSecret)
}

func TestIncorrectHarborSecret(t *testing.T) {
secret := "correct"
req, err := http.NewRequest("", "", nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
_ = commonsecret.AddToRequest(req, secret)

// Set req header to an incorrect value to trigger error return
req.Header.Set("Authorization", fmt.Sprintf("%s%s", "WrongPrefix", secret))
authenticator := NewSecretHandler(map[string]string{"secret1": "correct"})
err = authenticator.AuthorizeRequest(req)
assert.Equal(t, err, ErrInvalidCredential)
}

0 comments on commit 0e260d1

Please sign in to comment.