mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-30 06:03:45 +01:00
commit
05759cf0ad
@ -52,7 +52,7 @@ func Register(user models.User) (int64, error) {
|
|||||||
func UserExists(user models.User, target string) (bool, error) {
|
func UserExists(user models.User, target string) (bool, error) {
|
||||||
|
|
||||||
if user.Username == "" && user.Email == "" {
|
if user.Username == "" && user.Email == "" {
|
||||||
return false, errors.New("User name and email are blank.")
|
return false, errors.New("user name and email are blank")
|
||||||
}
|
}
|
||||||
|
|
||||||
o := GetOrmer()
|
o := GetOrmer()
|
||||||
|
@ -131,7 +131,7 @@ func ToggleUserAdminRole(userID, hasAdmin int) error {
|
|||||||
// ChangeUserPassword ...
|
// ChangeUserPassword ...
|
||||||
func ChangeUserPassword(u models.User, oldPassword ...string) (err error) {
|
func ChangeUserPassword(u models.User, oldPassword ...string) (err error) {
|
||||||
if len(oldPassword) > 1 {
|
if len(oldPassword) > 1 {
|
||||||
return errors.New("Wrong numbers of params.")
|
return errors.New("wrong numbers of params")
|
||||||
}
|
}
|
||||||
|
|
||||||
o := GetOrmer()
|
o := GetOrmer()
|
||||||
@ -153,7 +153,7 @@ func ChangeUserPassword(u models.User, oldPassword ...string) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if c == 0 {
|
if c == 0 {
|
||||||
return errors.New("No record has been modified, change password failed.")
|
return errors.New("no record has been modified, change password failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -171,7 +171,7 @@ func ResetUserPassword(u models.User) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
return errors.New("No record be changed, reset password failed.")
|
return errors.New("no record be changed, reset password failed")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ func (sm *SM) EnterState(s string) (string, error) {
|
|||||||
_, exist := targets[s]
|
_, exist := targets[s]
|
||||||
_, isForced := sm.ForcedStates[s]
|
_, isForced := sm.ForcedStates[s]
|
||||||
if !exist && !isForced {
|
if !exist && !isForced {
|
||||||
return "", fmt.Errorf("Job id: %d, transition from %s to %s does not exist!", sm.JobID, sm.CurrentState, s)
|
return "", fmt.Errorf("job id: %d, transition from %s to %s does not exist", sm.JobID, sm.CurrentState, s)
|
||||||
}
|
}
|
||||||
exitHandler, ok := sm.Handlers[sm.CurrentState]
|
exitHandler, ok := sm.Handlers[sm.CurrentState]
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
package api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
|
||||||
}
|
|
||||||
|
|
@ -16,18 +16,22 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/vmware/harbor/src/common/dao"
|
"github.com/vmware/harbor/src/common/dao"
|
||||||
"github.com/vmware/harbor/src/common/models"
|
"github.com/vmware/harbor/src/common/models"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
//Prepare Test info
|
//Prepare Test info
|
||||||
TestUserName = "testUser0001"
|
TestUserName = "testUser0001"
|
||||||
TestUserPwd = "testUser0001"
|
TestUserPwd = "testUser0001"
|
||||||
TestUserEmail = "testUser0001@mydomain.com"
|
TestUserEmail = "testUser0001@mydomain.com"
|
||||||
TestProName = "testProject0001"
|
TestProName = "testProject0001"
|
||||||
TestTargetName = "testTarget0001"
|
TestTargetName = "testTarget0001"
|
||||||
|
TestRepoName = "testRepo0001"
|
||||||
|
AdminName = "admin"
|
||||||
|
DefaultProjectName = "library"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CommonAddUser() {
|
func CommonAddUser() {
|
||||||
@ -104,3 +108,20 @@ func CommonDelTarget() {
|
|||||||
func CommonPolicyEabled(policyID int, enabled int) {
|
func CommonPolicyEabled(policyID int, enabled int) {
|
||||||
_ = dao.UpdateRepPolicyEnablement(int64(policyID), enabled)
|
_ = dao.UpdateRepPolicyEnablement(int64(policyID), enabled)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CommonAddRepository() {
|
||||||
|
commonRepository := &models.RepoRecord{
|
||||||
|
RepositoryID: "1",
|
||||||
|
Name: TestRepoName,
|
||||||
|
OwnerName: AdminName,
|
||||||
|
OwnerID: 1,
|
||||||
|
ProjectName: DefaultProjectName,
|
||||||
|
ProjectID: 1,
|
||||||
|
PullCount: 1,
|
||||||
|
}
|
||||||
|
_ = dao.AddRepository(*commonRepository)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CommonDelRepository() {
|
||||||
|
_ = dao.DeleteRepository(TestRepoName)
|
||||||
|
}
|
||||||
|
@ -174,19 +174,20 @@ func (a testapi) ProjectsPost(prjUsr usrInfo, project apilib.ProjectReq) (int, e
|
|||||||
return httpStatusCode, err
|
return httpStatusCode, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a testapi) StatisticGet(user usrInfo) (apilib.StatisticMap, error) {
|
func (a testapi) StatisticGet(user usrInfo) (int, apilib.StatisticMap, error) {
|
||||||
_sling := sling.New().Get(a.basePath)
|
_sling := sling.New().Get(a.basePath)
|
||||||
|
|
||||||
// create path and map variables
|
// create path and map variables
|
||||||
path := "/api/statistics/"
|
path := "/api/statistics/"
|
||||||
fmt.Printf("project statistic path: %s\n", path)
|
|
||||||
_sling = _sling.Path(path)
|
_sling = _sling.Path(path)
|
||||||
var successPayload = new(apilib.StatisticMap)
|
var successPayload apilib.StatisticMap
|
||||||
code, body, err := request(_sling, jsonAcceptHeader, user)
|
httpStatusCode, body, err := request(_sling, jsonAcceptHeader, user)
|
||||||
if 200 == code && nil == err {
|
|
||||||
|
if err == nil && httpStatusCode == 200 {
|
||||||
err = json.Unmarshal(body, &successPayload)
|
err = json.Unmarshal(body, &successPayload)
|
||||||
}
|
}
|
||||||
return *successPayload, err
|
return httpStatusCode, successPayload, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a testapi) LogGet(user usrInfo, startTime, endTime, lines string) (int, []apilib.AccessLog, error) {
|
func (a testapi) LogGet(user usrInfo, startTime, endTime, lines string) (int, []apilib.AccessLog, error) {
|
||||||
@ -857,7 +858,7 @@ func updateInitPassword(userID int, password string) error {
|
|||||||
return fmt.Errorf("Failed to get user, userID: %d %v", userID, err)
|
return fmt.Errorf("Failed to get user, userID: %d %v", userID, err)
|
||||||
}
|
}
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return fmt.Errorf("User id: %d does not exist.", userID)
|
return fmt.Errorf("user id: %d does not exist", userID)
|
||||||
}
|
}
|
||||||
if user.Salt == "" {
|
if user.Salt == "" {
|
||||||
user.Salt = utils.GenerateRandomString()
|
user.Salt = utils.GenerateRandomString()
|
||||||
|
@ -413,7 +413,7 @@ func validateProjectReq(req projectReq) error {
|
|||||||
validProjectName := regexp.MustCompile(`^[a-z0-9](?:-*[a-z0-9])*(?:[._][a-z0-9](?:-*[a-z0-9])*)*$`)
|
validProjectName := regexp.MustCompile(`^[a-z0-9](?:-*[a-z0-9])*(?:[._][a-z0-9](?:-*[a-z0-9])*)*$`)
|
||||||
legal := validProjectName.MatchString(pn)
|
legal := validProjectName.MatchString(pn)
|
||||||
if !legal {
|
if !legal {
|
||||||
return fmt.Errorf("Project name is not in lower case or contains illegal characters!")
|
return fmt.Errorf("project name is not in lower case or contains illegal characters")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,14 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/vmware/harbor/tests/apitests/apilib"
|
//"github.com/vmware/harbor/tests/apitests/apilib"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStatisticGet(t *testing.T) {
|
func TestStatisticGet(t *testing.T) {
|
||||||
|
|
||||||
fmt.Println("Testing Statistic API")
|
fmt.Println("Testing Statistic API")
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
@ -17,69 +17,60 @@ func TestStatisticGet(t *testing.T) {
|
|||||||
|
|
||||||
//prepare for test
|
//prepare for test
|
||||||
|
|
||||||
var myProCount, pubProCount, totalProCount int32
|
var priMyProjectCount, priMyRepoCount int32
|
||||||
result, err := apiTest.StatisticGet(*admin)
|
var priPublicProjectCount, priPublicRepoCount int32
|
||||||
|
var priTotalProjectCount, priTotalRepoCount int32
|
||||||
|
|
||||||
|
//case 1: case 1: user not login, expect fail to get status info.
|
||||||
|
fmt.Println("case 1: user not login, expect fail to get status info.")
|
||||||
|
httpStatusCode, result, err := apiTest.StatisticGet(*unknownUsr)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Error get statistic info.", err.Error())
|
||||||
|
t.Log(err)
|
||||||
|
} else {
|
||||||
|
assert.Equal(httpStatusCode, int(401), "Case 1: Get status info without login. (401)")
|
||||||
|
}
|
||||||
|
|
||||||
|
//case 2: admin successful login, expect get status info successful.
|
||||||
|
fmt.Println("case 2: admin successful login, expect get status info successful.")
|
||||||
|
httpStatusCode, result, err = apiTest.StatisticGet(*admin)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Error get statistic info.", err.Error())
|
||||||
|
t.Log(err)
|
||||||
|
} else {
|
||||||
|
assert.Equal(httpStatusCode, int(200), "Case 2: Get status info with admin login. (200)")
|
||||||
|
//fmt.Println("pri status data %+v", result)
|
||||||
|
priMyProjectCount = result.MyProjectCount
|
||||||
|
priMyRepoCount = result.MyRepoCount
|
||||||
|
priPublicProjectCount = result.PublicProjectCount
|
||||||
|
priPublicRepoCount = result.PublicRepoCount
|
||||||
|
priTotalProjectCount = result.TotalProjectCount
|
||||||
|
priTotalRepoCount = result.TotalRepoCount
|
||||||
|
}
|
||||||
|
|
||||||
|
//case 3: status info increased after add more project and repo.
|
||||||
|
fmt.Println("case 3: status info increased after add more project and repo.")
|
||||||
|
|
||||||
|
CommonAddProject()
|
||||||
|
CommonAddRepository()
|
||||||
|
|
||||||
|
httpStatusCode, result, err = apiTest.StatisticGet(*admin)
|
||||||
|
//fmt.Println("new status data %+v", result)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Error while get statistic information", err.Error())
|
t.Error("Error while get statistic information", err.Error())
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
} else {
|
} else {
|
||||||
myProCount = result.MyProjectCount
|
assert.Equal(priMyProjectCount+1, result.MyProjectCount, "MyProjectCount should be +1")
|
||||||
pubProCount = result.PublicProjectCount
|
assert.Equal(priMyRepoCount+1, result.MyRepoCount, "MyRepoCount should be +1")
|
||||||
totalProCount = result.TotalProjectCount
|
assert.Equal(priPublicProjectCount, result.PublicProjectCount, "PublicProjectCount should be equal")
|
||||||
}
|
assert.Equal(priPublicRepoCount+1, result.PublicRepoCount, "PublicRepoCount should be +1")
|
||||||
//post project
|
assert.Equal(priTotalProjectCount+1, result.TotalProjectCount, "TotalProCount should be +1")
|
||||||
var project apilib.ProjectReq
|
assert.Equal(priTotalRepoCount+1, result.TotalRepoCount, "TotalRepoCount should be +1")
|
||||||
project.ProjectName = "statistic_project"
|
|
||||||
project.Public = 1
|
|
||||||
|
|
||||||
//case 2: admin successful login, expect project creation success.
|
|
||||||
fmt.Println("case 2: admin successful login, expect project creation success.")
|
|
||||||
reply, err := apiTest.ProjectsPost(*admin, project)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Error while creat project", err.Error())
|
|
||||||
t.Log(err)
|
|
||||||
} else {
|
|
||||||
assert.Equal(reply, int(201), "Case 2: Project creation status should be 201")
|
|
||||||
}
|
|
||||||
|
|
||||||
//get and compare
|
|
||||||
result, err = apiTest.StatisticGet(*admin)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Error while get statistic information", err.Error())
|
|
||||||
t.Log(err)
|
|
||||||
} else {
|
|
||||||
assert.Equal(myProCount+1, result.MyProjectCount, "MyProjectCount should be equal")
|
|
||||||
assert.Equal(int32(2), result.MyRepoCount, "MyRepoCount should be equal")
|
|
||||||
assert.Equal(pubProCount+1, result.PublicProjectCount, "PublicProjectCount should be equal")
|
|
||||||
assert.Equal(int32(2), result.PublicRepoCount, "PublicRepoCount should be equal")
|
|
||||||
assert.Equal(totalProCount+1, result.TotalProjectCount, "TotalProCount should be equal")
|
|
||||||
assert.Equal(int32(2), result.TotalRepoCount, "TotalRepoCount should be equal")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//get the project
|
//delete the project and repo
|
||||||
var projects []apilib.Project
|
CommonDelProject()
|
||||||
var addProjectID int32
|
CommonDelRepository()
|
||||||
httpStatusCode, projects, err := apiTest.ProjectsGet(project.ProjectName, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Error while search project by proName and isPublic", err.Error())
|
|
||||||
t.Log(err)
|
|
||||||
} else {
|
|
||||||
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
|
|
||||||
addProjectID = projects[0].ProjectId
|
|
||||||
}
|
|
||||||
|
|
||||||
//delete the project
|
|
||||||
projectID := strconv.Itoa(int(addProjectID))
|
|
||||||
httpStatusCode, err = apiTest.ProjectsDelete(*admin, projectID)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Error while delete project", err.Error())
|
|
||||||
t.Log(err)
|
|
||||||
} else {
|
|
||||||
assert.Equal(int(200), httpStatusCode, "Case 1: Project creation status should be 200")
|
|
||||||
//t.Log(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("\n")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -323,13 +323,13 @@ func (ua *UserAPI) ToggleUserAdminRole() {
|
|||||||
func validate(user models.User) error {
|
func validate(user models.User) error {
|
||||||
|
|
||||||
if isIllegalLength(user.Username, 1, 20) {
|
if isIllegalLength(user.Username, 1, 20) {
|
||||||
return fmt.Errorf("Username with illegal length.")
|
return fmt.Errorf("username with illegal length")
|
||||||
}
|
}
|
||||||
if isContainIllegalChar(user.Username, []string{",", "~", "#", "$", "%"}) {
|
if isContainIllegalChar(user.Username, []string{",", "~", "#", "$", "%"}) {
|
||||||
return fmt.Errorf("Username contains illegal characters.")
|
return fmt.Errorf("username contains illegal characters")
|
||||||
}
|
}
|
||||||
if isIllegalLength(user.Password, 8, 20) {
|
if isIllegalLength(user.Password, 8, 20) {
|
||||||
return fmt.Errorf("Password with illegal length.")
|
return fmt.Errorf("password with illegal length")
|
||||||
}
|
}
|
||||||
if err := commonValidate(user); err != nil {
|
if err := commonValidate(user); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -342,21 +342,21 @@ func commonValidate(user models.User) error {
|
|||||||
|
|
||||||
if len(user.Email) > 0 {
|
if len(user.Email) > 0 {
|
||||||
if m, _ := regexp.MatchString(`^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$`, user.Email); !m {
|
if m, _ := regexp.MatchString(`^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$`, user.Email); !m {
|
||||||
return fmt.Errorf("Email with illegal format.")
|
return fmt.Errorf("email with illegal format")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("Email can't be empty")
|
return fmt.Errorf("Email can't be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if isIllegalLength(user.Realname, 0, 20) {
|
if isIllegalLength(user.Realname, 0, 20) {
|
||||||
return fmt.Errorf("Realname with illegal length.")
|
return fmt.Errorf("realname with illegal length")
|
||||||
}
|
}
|
||||||
|
|
||||||
if isContainIllegalChar(user.Realname, []string{",", "~", "#", "$", "%"}) {
|
if isContainIllegalChar(user.Realname, []string{",", "~", "#", "$", "%"}) {
|
||||||
return fmt.Errorf("Realname contains illegal characters.")
|
return fmt.Errorf("realname contains illegal characters")
|
||||||
}
|
}
|
||||||
if isIllegalLength(user.Comment, -1, 30) {
|
if isIllegalLength(user.Comment, -1, 30) {
|
||||||
return fmt.Errorf("Comment with illegal length.")
|
return fmt.Errorf("comment with illegal length")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ func (l *Auth) Authenticate(m models.AuthModel) (*models.User, error) {
|
|||||||
}
|
}
|
||||||
ldapURL := os.Getenv("LDAP_URL")
|
ldapURL := os.Getenv("LDAP_URL")
|
||||||
if ldapURL == "" {
|
if ldapURL == "" {
|
||||||
return nil, errors.New("Can not get any available LDAP_URL.")
|
return nil, errors.New("can not get any available LDAP_URL")
|
||||||
}
|
}
|
||||||
log.Debug("ldapURL:", ldapURL)
|
log.Debug("ldapURL:", ldapURL)
|
||||||
ldap, err := openldap.Initialize(ldapURL)
|
ldap, err := openldap.Initialize(ldapURL)
|
||||||
@ -59,7 +59,7 @@ func (l *Auth) Authenticate(m models.AuthModel) (*models.User, error) {
|
|||||||
|
|
||||||
ldapBaseDn := os.Getenv("LDAP_BASE_DN")
|
ldapBaseDn := os.Getenv("LDAP_BASE_DN")
|
||||||
if ldapBaseDn == "" {
|
if ldapBaseDn == "" {
|
||||||
return nil, errors.New("Can not get any available LDAP_BASE_DN.")
|
return nil, errors.New("can not get any available LDAP_BASE_DN")
|
||||||
}
|
}
|
||||||
log.Debug("baseDn:", ldapBaseDn)
|
log.Debug("baseDn:", ldapBaseDn)
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ func updateInitPassword(userID int, password string) error {
|
|||||||
return fmt.Errorf("Failed to get user, userID: %d %v", userID, err)
|
return fmt.Errorf("Failed to get user, userID: %d %v", userID, err)
|
||||||
}
|
}
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return fmt.Errorf("User id: %d does not exist.", userID)
|
return fmt.Errorf("user id: %d does not exist", userID)
|
||||||
}
|
}
|
||||||
if user.Salt == "" {
|
if user.Salt == "" {
|
||||||
salt := utils.GenerateRandomString()
|
salt := utils.GenerateRandomString()
|
||||||
|
Loading…
Reference in New Issue
Block a user