mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-23 10:45:45 +01:00
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:
parent
3a38ff928e
commit
0e260d180c
@ -34,7 +34,7 @@ type secretHandler struct {
|
|||||||
secrets map[string]string
|
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.
|
// basic authentication credentials to a request.
|
||||||
func NewSecretHandler(secrets map[string]string) AuthenticationHandler {
|
func NewSecretHandler(secrets map[string]string) AuthenticationHandler {
|
||||||
return &secretHandler{
|
return &secretHandler{
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -49,3 +50,45 @@ func TestAuthorizeRequestValid(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user