Added tests for failure conditions (#6501)

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>
This commit is contained in:
SDBrett 2018-12-13 14:35:17 +11:00 committed by Yan
parent 3a38ff928e
commit 0e260d180c
2 changed files with 44 additions and 1 deletions

View File

@ -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{

View File

@ -15,6 +15,7 @@
package auth
import (
"fmt"
"net/http"
"testing"
@ -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)
}