add validation for robot account registration

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2019-05-15 14:08:09 +08:00
parent a7f8f531f9
commit 2068732eef
3 changed files with 24 additions and 5 deletions

View File

@ -17,6 +17,7 @@ package models
import (
"github.com/astaxie/beego/validation"
"github.com/goharbor/harbor/src/common/rbac"
"github.com/goharbor/harbor/src/common/utils"
"time"
)
@ -52,9 +53,14 @@ type RobotReq struct {
Access []*rbac.Policy `json:"access"`
}
// Valid put request validation
// Valid ...
func (rq *RobotReq) Valid(v *validation.Validation) {
// ToDo: add validation for access info.
if utils.IsIllegalLength(rq.Name, 1, 255) {
v.SetError("name", "robot name with illegal length")
}
if utils.IsContainIllegalChar(rq.Name, []string{",", "~", "#", "$", "%"}) {
v.SetError("name", "robot name contains illegal characters")
}
}
// RobotRep ...

View File

@ -107,7 +107,8 @@ func (r *RobotAPI) Post() {
}
var robotReq models.RobotReq
if err := r.DecodeJSONReq(&robotReq); err != nil {
isValid, err := r.DecodeJSONReqAndValidate(&robotReq)
if !isValid {
r.SendBadRequestError(err)
return
}
@ -230,8 +231,7 @@ func (r *RobotAPI) Put() {
}
var robotReq models.RobotReq
isValid, err := r.DecodeJSONReqAndValidate(&robotReq)
if !isValid {
if err := r.DecodeJSONReq(&robotReq); err != nil {
r.SendBadRequestError(err)
return
}

View File

@ -70,6 +70,19 @@ func TestRobotAPIPost(t *testing.T) {
},
code: http.StatusCreated,
},
// 400
{
request: &testingRequest{
method: http.MethodPost,
url: robotPath,
bodyJSON: &models.RobotReq{
Name: "testIllgel#",
Description: "test desc",
},
credential: projAdmin4Robot,
},
code: http.StatusBadRequest,
},
// 403 -- developer
{
request: &testingRequest{