mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-29 12:07:56 +01:00
Merge pull request #2656 from ywk253100/170628_pms
Update test cases of PMS project manager
This commit is contained in:
commit
e5e5e75768
@ -92,14 +92,14 @@ func (a *AuthContext) HasAllPerm(project string) bool {
|
||||
|
||||
// GetMyProjects returns all projects which the user is a member of
|
||||
func (a *AuthContext) GetMyProjects() ([]string, error) {
|
||||
existence := map[string]string{}
|
||||
existence := map[string]bool{}
|
||||
projects := []string{}
|
||||
for _, list := range a.Projects {
|
||||
for _, p := range list {
|
||||
if len(existence[p]) > 0 {
|
||||
if existence[p] {
|
||||
continue
|
||||
}
|
||||
existence[p] = p
|
||||
existence[p] = true
|
||||
projects = append(projects, p)
|
||||
}
|
||||
|
||||
@ -107,9 +107,19 @@ func (a *AuthContext) GetMyProjects() ([]string, error) {
|
||||
return projects, nil
|
||||
}
|
||||
|
||||
// GetByToken gets the user's auth context, if the username is not provided
|
||||
// GetByToken ...
|
||||
func GetByToken(url, token string) (*AuthContext, error) {
|
||||
return get(url, token)
|
||||
}
|
||||
|
||||
// GetAuthCtxOfUser gets the user's auth context
|
||||
func GetAuthCtxOfUser(url, token string, username string) (*AuthContext, error) {
|
||||
return get(url, token, username)
|
||||
}
|
||||
|
||||
// get the user's auth context, if the username is not provided
|
||||
// get the default auth context of the token
|
||||
func GetByToken(url, token string, username ...string) (*AuthContext, error) {
|
||||
func get(url, token string, username ...string) (*AuthContext, error) {
|
||||
principalID := ""
|
||||
if len(username) > 0 {
|
||||
principalID = username[0]
|
||||
@ -138,7 +148,7 @@ func GetByToken(url, token string, username ...string) (*AuthContext, error) {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
// Login ...
|
||||
// Login with credential and returns token, auth context and error
|
||||
func Login(url, username, password string) (string, *AuthContext, error) {
|
||||
data, err := json.Marshal(&struct {
|
||||
Username string `json:"username"`
|
||||
|
@ -24,7 +24,6 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/vmware/harbor/src/common"
|
||||
"github.com/vmware/harbor/src/common/models"
|
||||
@ -55,7 +54,7 @@ type project struct {
|
||||
CustomProperties map[string]string `json:"customProperties"`
|
||||
Administrators []*user `json:"administrators"`
|
||||
Developers []*user `json:"members"`
|
||||
Guests []*user `json:"guests"` // TODO the json name needs to be modified according to the API
|
||||
Guests []*user `json:"viewers"`
|
||||
}
|
||||
|
||||
// NewProjectManager returns an instance of ProjectManager
|
||||
@ -81,7 +80,7 @@ func (p *ProjectManager) Get(projectIDOrName interface{}) (*models.Project, erro
|
||||
func (p *ProjectManager) get(projectIDOrName interface{}) (*project, error) {
|
||||
m := map[string]string{}
|
||||
if id, ok := projectIDOrName.(int64); ok {
|
||||
m["customProperties.__harborId"] = strconv.FormatInt(id, 10)
|
||||
m["customProperties.__projectIndex"] = strconv.FormatInt(id, 10)
|
||||
} else if name, ok := projectIDOrName.(string); ok {
|
||||
m["name"] = name
|
||||
} else {
|
||||
@ -162,14 +161,14 @@ func convert(p *project) (*models.Project, error) {
|
||||
project.Public = 1
|
||||
}
|
||||
|
||||
value := p.CustomProperties["__harborId"]
|
||||
value := p.CustomProperties["__projectIndex"]
|
||||
if len(value) == 0 {
|
||||
return nil, fmt.Errorf("property __harborId is null")
|
||||
return nil, fmt.Errorf("property __projectIndex is null")
|
||||
}
|
||||
|
||||
id, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse __harborId %s to int64: %v", value, err)
|
||||
return nil, fmt.Errorf("failed to parse __projectIndex %s to int64: %v", value, err)
|
||||
}
|
||||
project.ProjectID = id
|
||||
|
||||
@ -308,7 +307,7 @@ func (p *ProjectManager) GetPublic() ([]*models.Project, error) {
|
||||
// GetByMember ...
|
||||
func (p *ProjectManager) GetByMember(username string) ([]*models.Project, error) {
|
||||
projects := []*models.Project{}
|
||||
ctx, err := authcontext.GetByToken(p.endpoint, p.token, username)
|
||||
ctx, err := authcontext.GetAuthCtxOfUser(p.endpoint, p.token, username)
|
||||
if err != nil {
|
||||
return projects, err
|
||||
}
|
||||
@ -341,9 +340,6 @@ func (p *ProjectManager) Create(pro *models.Project) (int64, error) {
|
||||
proj.CustomProperties["__preventVulnerableImagesFromRunningSeverity"] = pro.PreventVulnerableImagesFromRunningSeverity
|
||||
proj.CustomProperties["__automaticallyScanImagesOnPush"] = strconv.FormatBool(pro.AutomaticallyScanImagesOnPush)
|
||||
|
||||
// TODO remove the logic if Admiral generates the harborId
|
||||
proj.CustomProperties["__harborId"] = strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||
|
||||
data, err := json.Marshal(proj)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
@ -34,16 +34,16 @@ func TestConvert(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, pro)
|
||||
|
||||
//project without property __harborId
|
||||
//project without property __projectIndex
|
||||
p := &project{}
|
||||
pro, err = convert(p)
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, pro)
|
||||
|
||||
//project with invalid __harborId
|
||||
//project with invalid __projectIndex
|
||||
p = &project{
|
||||
CustomProperties: map[string]string{
|
||||
"__harborId": "invalid_value",
|
||||
"__projectIndex": "invalid_value",
|
||||
},
|
||||
}
|
||||
pro, err = convert(p)
|
||||
@ -85,7 +85,7 @@ func TestConvert(t *testing.T) {
|
||||
Name: "test",
|
||||
Public: true,
|
||||
CustomProperties: map[string]string{
|
||||
"__harborId": "1",
|
||||
"__projectIndex": "1",
|
||||
"__enableContentTrust": "true",
|
||||
"__preventVulnerableImagesFromRunning": "true",
|
||||
"__preventVulnerableImagesFromRunningSeverity": "medium",
|
||||
@ -118,7 +118,7 @@ func TestParse(t *testing.T) {
|
||||
"id": "41427587-70e9-4671-9a9e-b9def0a07bb7",
|
||||
"name": "project02",
|
||||
"customProperties": {
|
||||
"__harborId": "2",
|
||||
"__projectIndex": "2",
|
||||
"__enableContentTrust": "true",
|
||||
"__preventVulnerableImagesFromRunning": "true",
|
||||
"__preventVulnerableImagesFromRunningSeverity": "medium",
|
||||
@ -140,7 +140,7 @@ func TestParse(t *testing.T) {
|
||||
"id": "default-project",
|
||||
"name": "default-project",
|
||||
"customProperties": {
|
||||
"__harborId": "2",
|
||||
"__projectIndex": "2",
|
||||
"__enableContentTrust": "true",
|
||||
"__preventVulnerableImagesFromRunning": "true",
|
||||
"__preventVulnerableImagesFromRunningSeverity": "medium",
|
||||
@ -374,8 +374,6 @@ func TestCreate(t *testing.T) {
|
||||
assert.True(t, project.AutomaticallyScanImagesOnPush)
|
||||
}
|
||||
|
||||
// TODO get the case back after Admiral'API is fixed
|
||||
/*
|
||||
func TestDelete(t *testing.T) {
|
||||
pm := NewProjectManager(endpoint, token)
|
||||
|
||||
@ -401,7 +399,7 @@ func TestDelete(t *testing.T) {
|
||||
err = pm.Delete(name)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
*/
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
pm := NewProjectManager(endpoint, token)
|
||||
err := pm.Update(nil, nil)
|
||||
@ -490,9 +488,10 @@ func TestGetTotal(t *testing.T) {
|
||||
assert.Equal(t, total1+1, total2)
|
||||
}
|
||||
|
||||
// TODO add test case
|
||||
func TestGetHasReadPerm(t *testing.T) {
|
||||
|
||||
pm := NewProjectManager(endpoint, token)
|
||||
_, err := pm.GetHasReadPerm()
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func delete(t *testing.T, id int64) {
|
||||
|
Loading…
Reference in New Issue
Block a user