Merge pull request #7635 from heww/validation-failed-status-code

Return 400 status code for validation failed
This commit is contained in:
Wenkai Yin 2019-05-10 14:22:05 +08:00 committed by GitHub
commit 8348c1fa4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 12 additions and 18 deletions

View File

@ -515,8 +515,6 @@ paths:
description: User need to log in first. description: User need to log in first.
'403': '403':
description: User in session does not have permission to the project. description: User in session does not have permission to the project.
'404':
description: 'Project does not exist, or the username does not found, or the user group does not found.'
'409': '409':
description: An LDAP user group with same DN already exist. description: An LDAP user group with same DN already exist.
'500': '500':
@ -1846,8 +1844,6 @@ paths:
$ref: '#/responses/Unauthorized' $ref: '#/responses/Unauthorized'
'403': '403':
$ref: '#/responses/Forbidden' $ref: '#/responses/Forbidden'
'404':
$ref: '#/responses/NotFound'
'409': '409':
$ref: '#/responses/Conflict' $ref: '#/responses/Conflict'
'415': '415':
@ -2578,8 +2574,6 @@ paths:
description: User need to log in first. description: User need to log in first.
'403': '403':
description: User in session does not have permission to the user group. description: User in session does not have permission to the user group.
'404':
description: The LDAP group is not found.
'409': '409':
description: An LDAP user group with same DN already exist. description: An LDAP user group with same DN already exist.
'500': '500':

View File

@ -116,7 +116,7 @@ func (l *LabelAPI) Post() {
return return
} }
if !exist { if !exist {
l.SendNotFoundError(fmt.Errorf("project %d not found", label.ProjectID)) l.SendBadRequestError(fmt.Errorf("project %d not found", label.ProjectID))
return return
} }
} }

View File

@ -106,7 +106,7 @@ func TestLabelAPIPost(t *testing.T) {
code: http.StatusForbidden, code: http.StatusForbidden,
}, },
// 404 non-exist project // 400 non-exist project
{ {
request: &testingRequest{ request: &testingRequest{
method: http.MethodPost, method: http.MethodPost,
@ -118,7 +118,7 @@ func TestLabelAPIPost(t *testing.T) {
}, },
credential: projAdmin, credential: projAdmin,
}, },
code: http.StatusNotFound, code: http.StatusBadRequest,
}, },
// 200 // 200

View File

@ -158,7 +158,7 @@ func (pma *ProjectMemberAPI) Post() {
pmid, err := AddProjectMember(projectID, request) pmid, err := AddProjectMember(projectID, request)
if err == auth.ErrorGroupNotExist || err == auth.ErrorUserNotExist { if err == auth.ErrorGroupNotExist || err == auth.ErrorUserNotExist {
pma.SendNotFoundError(fmt.Errorf("Failed to add project member, error: %v", err)) pma.SendBadRequestError(fmt.Errorf("Failed to add project member, error: %v", err))
return return
} else if err == auth.ErrDuplicateLDAPGroup { } else if err == auth.ErrDuplicateLDAPGroup {
pma.SendConflictError(fmt.Errorf("Failed to add project member, already exist LDAP group or project member, groupDN:%v", request.MemberGroup.LdapGroupDN)) pma.SendConflictError(fmt.Errorf("Failed to add project member, already exist LDAP group or project member, groupDN:%v", request.MemberGroup.LdapGroupDN))

View File

@ -126,7 +126,7 @@ func TestProjectMemberAPI_Post(t *testing.T) {
}, },
credential: admin, credential: admin,
}, },
code: http.StatusNotFound, code: http.StatusBadRequest,
}, },
{ {
request: &testingRequest{ request: &testingRequest{

View File

@ -132,7 +132,7 @@ func (r *ReplicationPolicyAPI) validateRegistry(policy *model.Policy) bool {
return false return false
} }
if registry == nil { if registry == nil {
r.SendNotFoundError(fmt.Errorf("registry %d not found", registryID)) r.SendBadRequestError(fmt.Errorf("registry %d not found", registryID))
return false return false
} }
return true return true

View File

@ -159,7 +159,7 @@ func TestReplicationPolicyAPICreate(t *testing.T) {
}, },
code: http.StatusConflict, code: http.StatusConflict,
}, },
// 404, registry not found // 400, registry not found
{ {
request: &testingRequest{ request: &testingRequest{
method: http.MethodPost, method: http.MethodPost,
@ -172,7 +172,7 @@ func TestReplicationPolicyAPICreate(t *testing.T) {
}, },
}, },
}, },
code: http.StatusNotFound, code: http.StatusBadRequest,
}, },
// 201 // 201
{ {
@ -310,7 +310,7 @@ func TestReplicationPolicyAPIUpdate(t *testing.T) {
}, },
code: http.StatusConflict, code: http.StatusConflict,
}, },
// 404, registry not found // 400, registry not found
{ {
request: &testingRequest{ request: &testingRequest{
method: http.MethodPut, method: http.MethodPut,
@ -323,7 +323,7 @@ func TestReplicationPolicyAPIUpdate(t *testing.T) {
}, },
}, },
}, },
code: http.StatusNotFound, code: http.StatusBadRequest,
}, },
// 200 // 200
{ {

View File

@ -15,12 +15,12 @@
package api package api
import ( import (
"errors"
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"errors"
"github.com/goharbor/harbor/src/common" "github.com/goharbor/harbor/src/common"
"github.com/goharbor/harbor/src/common/dao/group" "github.com/goharbor/harbor/src/common/dao/group"
"github.com/goharbor/harbor/src/common/models" "github.com/goharbor/harbor/src/common/models"
@ -123,7 +123,7 @@ func (uga *UserGroupAPI) Post() {
// User can not add ldap group when the ldap server is offline // User can not add ldap group when the ldap server is offline
ldapGroup, err := auth.SearchGroup(userGroup.LdapGroupDN) ldapGroup, err := auth.SearchGroup(userGroup.LdapGroupDN)
if err == ldap.ErrNotFound || ldapGroup == nil { if err == ldap.ErrNotFound || ldapGroup == nil {
uga.SendNotFoundError(fmt.Errorf("LDAP Group DN is not found: DN:%v", userGroup.LdapGroupDN)) uga.SendBadRequestError(fmt.Errorf("LDAP Group DN is not found: DN:%v", userGroup.LdapGroupDN))
return return
} }
if err == ldap.ErrDNSyntax { if err == ldap.ErrDNSyntax {