This commit is contained in:
Wenkai Yin 2017-05-10 17:09:24 +08:00
parent 8f396b44f3
commit 06ba7e0de0
12 changed files with 79 additions and 242 deletions

View File

@ -656,11 +656,6 @@ paths:
format: int32 format: int32
required: true required: true
description: Relevant project ID. description: Relevant project ID.
- name: detail
in: query
type: boolean
required: false
description: Get detail info or not.
- name: q - name: q
in: query in: query
type: string type: string
@ -682,7 +677,7 @@ paths:
- Products - Products
responses: responses:
200: 200:
description: If detail is false, the response body is a string array which contains the names of repositories, or the response body contains an object array as described in schema. description: Get repositories successfully.
schema: schema:
type: array type: array
items: items:
@ -742,16 +737,11 @@ paths:
type: string type: string
required: true required: true
description: Relevant repository name. description: Relevant repository name.
- name: detail
in: query
type: boolean
required: false
description: If detail is true, the manifests is returned too.
tags: tags:
- Products - Products
responses: responses:
200: 200:
description: If detail is false, the response body is a string array, or the response body contains the manifest informations as described in schema. description: Get tags successfully.
schema: schema:
type: array type: array
items: items:
@ -851,16 +841,11 @@ paths:
format: int32 format: int32
required: false required: false
description: The number of the requested public repositories, default is 10 if not provided. description: The number of the requested public repositories, default is 10 if not provided.
- name: detail
in: query
type: boolean
required: false
description: Get detail info or not.
tags: tags:
- Products - Products
responses: responses:
200: 200:
description: If detail is true, the response is described as the schema, or the response contains a TopRepo array. description: Get popular repositories successfully.
schema: schema:
type: array type: array
items: items:

View File

@ -103,7 +103,8 @@ func GetRepositoryByProjectName(name string) ([]*models.RepoRecord, error) {
return repos, err return repos, err
} }
//GetTopRepos returns the most popular repositories //GetTopRepos returns the most popular repositories whose project ID is
// in projectIDs
func GetTopRepos(projectIDs []int64, n int) ([]*models.RepoRecord, error) { func GetTopRepos(projectIDs []int64, n int) ([]*models.RepoRecord, error) {
repositories := []*models.RepoRecord{} repositories := []*models.RepoRecord{}
_, err := GetOrmer().QueryTable(&models.RepoRecord{}). _, err := GetOrmer().QueryTable(&models.RepoRecord{}).

View File

@ -17,7 +17,6 @@ package dao
import ( import (
"fmt" "fmt"
"testing" "testing"
"time"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/models"
@ -170,47 +169,25 @@ func TestGetTopRepos(t *testing.T) {
require.NoError(GetOrmer().Rollback()) require.NoError(GetOrmer().Rollback())
}() }()
admin, err := GetUser(models.User{Username: "admin"}) projectIDs := []int64{}
require.NoError(err)
user := models.User{
Username: "user",
Password: "user",
Email: "user@test.com",
}
userID, err := Register(user)
require.NoError(err)
user.UserID = int(userID)
//
// public project with 1 repository
// non-public project with 2 repositories visible by admin
// non-public project with 1 repository visible by admin and user
// deleted public project with 1 repository
//
project1 := models.Project{ project1 := models.Project{
OwnerID: admin.UserID, OwnerID: 1,
Name: "project1", Name: "project1",
CreationTime: time.Now(), Public: 0,
OwnerName: admin.Username,
Public: 0,
} }
project1.ProjectID, err = AddProject(project1) project1.ProjectID, err = AddProject(project1)
require.NoError(err) require.NoError(err)
projectIDs = append(projectIDs, project1.ProjectID)
project2 := models.Project{ project2 := models.Project{
OwnerID: user.UserID, OwnerID: 1,
Name: "project2", Name: "project2",
CreationTime: time.Now(), Public: 0,
OwnerName: user.Username,
Public: 0,
} }
project2.ProjectID, err = AddProject(project2) project2.ProjectID, err = AddProject(project2)
require.NoError(err) require.NoError(err)
projectIDs = append(projectIDs, project2.ProjectID)
err = AddRepository(*repository)
require.NoError(err)
repository1 := &models.RepoRecord{ repository1 := &models.RepoRecord{
Name: fmt.Sprintf("%v/repository1", project1.Name), Name: fmt.Sprintf("%v/repository1", project1.Name),
@ -219,8 +196,6 @@ func TestGetTopRepos(t *testing.T) {
err = AddRepository(*repository1) err = AddRepository(*repository1)
require.NoError(err) require.NoError(err)
require.NoError(IncreasePullCount(repository1.Name)) require.NoError(IncreasePullCount(repository1.Name))
repository1, err = GetRepositoryByName(repository1.Name)
require.NoError(err)
repository2 := &models.RepoRecord{ repository2 := &models.RepoRecord{
Name: fmt.Sprintf("%v/repository2", project1.Name), Name: fmt.Sprintf("%v/repository2", project1.Name),
@ -230,8 +205,6 @@ func TestGetTopRepos(t *testing.T) {
require.NoError(err) require.NoError(err)
require.NoError(IncreasePullCount(repository2.Name)) require.NoError(IncreasePullCount(repository2.Name))
require.NoError(IncreasePullCount(repository2.Name)) require.NoError(IncreasePullCount(repository2.Name))
repository2, err = GetRepositoryByName(repository2.Name)
require.NoError(err)
repository3 := &models.RepoRecord{ repository3 := &models.RepoRecord{
Name: fmt.Sprintf("%v/repository3", project2.Name), Name: fmt.Sprintf("%v/repository3", project2.Name),
@ -242,49 +215,11 @@ func TestGetTopRepos(t *testing.T) {
require.NoError(IncreasePullCount(repository3.Name)) require.NoError(IncreasePullCount(repository3.Name))
require.NoError(IncreasePullCount(repository3.Name)) require.NoError(IncreasePullCount(repository3.Name))
require.NoError(IncreasePullCount(repository3.Name)) require.NoError(IncreasePullCount(repository3.Name))
repository3, err = GetRepositoryByName(repository3.Name)
require.NoError(err)
deletedPublicProject := models.Project{ topRepos, err := GetTopRepos(projectIDs, 100)
OwnerID: admin.UserID,
Name: "public-deleted",
CreationTime: time.Now(),
OwnerName: admin.Username,
Public: 1,
}
deletedPublicProject.ProjectID, err = AddProject(deletedPublicProject)
require.NoError(err)
deletedPublicRepository1 := &models.RepoRecord{
Name: fmt.Sprintf("%v/repository1", deletedPublicProject.Name),
ProjectID: deletedPublicProject.ProjectID,
}
err = AddRepository(*deletedPublicRepository1)
require.NoError(err)
err = DeleteProject(deletedPublicProject.ProjectID)
require.NoError(err)
var topRepos []*models.RepoRecord
// anonymous should retrieve public non-deleted repositories
topRepos, err = GetTopRepos(NonExistUserID, 100)
require.NoError(err)
require.Len(topRepos, 1)
require.Equal(topRepos[0].Name, repository.Name)
// admin should retrieve all repositories
topRepos, err = GetTopRepos(admin.UserID, 100)
require.NoError(err)
require.Len(topRepos, 4)
// user should retrieve visible repositories
topRepos, err = GetTopRepos(user.UserID, 100)
require.NoError(err)
require.Len(topRepos, 2)
// limit by count
topRepos, err = GetTopRepos(admin.UserID, 3)
require.NoError(err) require.NoError(err)
require.Len(topRepos, 3) require.Len(topRepos, 3)
require.Equal(topRepos[0].Name, repository3.Name)
} }
func TestGetTotalOfRepositoriesByProject(t *testing.T) { func TestGetTotalOfRepositoriesByProject(t *testing.T) {

View File

@ -1,15 +0,0 @@
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models

View File

@ -33,6 +33,18 @@ func (f *fakePM) IsPublic(projectIDOrName interface{}) bool {
func (f *fakePM) GetRoles(username string, projectIDOrName interface{}) []int { func (f *fakePM) GetRoles(username string, projectIDOrName interface{}) []int {
return f.roles[projectIDOrName.(string)] return f.roles[projectIDOrName.(string)]
} }
func (f *fakePM) Get(projectIDOrName interface{}) *models.Project {
return nil
}
func (f *fakePM) Exist(projectIDOrName interface{}) bool {
return false
}
func (f *fakePM) GetPublic() []models.Project {
return []models.Project{}
}
func (f *fakePM) GetByMember(username string) []models.Project {
return []models.Project{}
}
func TestIsAuthenticated(t *testing.T) { func TestIsAuthenticated(t *testing.T) {
// unauthenticated // unauthenticated

View File

@ -30,6 +30,7 @@ import (
"github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/models"
"github.com/vmware/harbor/src/common/utils" "github.com/vmware/harbor/src/common/utils"
"github.com/vmware/harbor/src/ui/config" "github.com/vmware/harbor/src/ui/config"
"github.com/vmware/harbor/src/ui/filter"
"github.com/vmware/harbor/tests/apitests/apilib" "github.com/vmware/harbor/tests/apitests/apilib"
// "strconv" // "strconv"
// "strings" // "strings"
@ -86,6 +87,8 @@ func init() {
beego.BConfig.WebConfig.Session.SessionOn = true beego.BConfig.WebConfig.Session.SessionOn = true
beego.TestBeegoInit(apppath) beego.TestBeegoInit(apppath)
beego.InsertFilter("/*", beego.BeforeRouter, filter.SecurityFilter)
beego.Router("/api/search/", &SearchAPI{}) beego.Router("/api/search/", &SearchAPI{})
beego.Router("/api/projects/", &ProjectAPI{}, "get:List;post:Post;head:Head") beego.Router("/api/projects/", &ProjectAPI{}, "get:List;post:Post;head:Head")
beego.Router("/api/projects/:id", &ProjectAPI{}, "delete:Delete;get:Get") beego.Router("/api/projects/:id", &ProjectAPI{}, "delete:Delete;get:Get")
@ -473,8 +476,8 @@ func (a testapi) PutProjectMember(authInfo usrInfo, projectID string, userID str
//-------------------------Repositories Test---------------------------------------// //-------------------------Repositories Test---------------------------------------//
//Return relevant repos of projectID //Return relevant repos of projectID
func (a testapi) GetRepos(authInfo usrInfo, projectID, func (a testapi) GetRepos(authInfo usrInfo, projectID, keyword string) (
keyword, detail string) (int, interface{}, error) { int, interface{}, error) {
_sling := sling.New().Get(a.basePath) _sling := sling.New().Get(a.basePath)
path := "/api/repositories/" path := "/api/repositories/"
@ -483,13 +486,11 @@ func (a testapi) GetRepos(authInfo usrInfo, projectID,
type QueryParams struct { type QueryParams struct {
ProjectID string `url:"project_id"` ProjectID string `url:"project_id"`
Detail string `url:"detail"`
Keyword string `url:"q"` Keyword string `url:"q"`
} }
_sling = _sling.QueryStruct(&QueryParams{ _sling = _sling.QueryStruct(&QueryParams{
ProjectID: projectID, ProjectID: projectID,
Detail: detail,
Keyword: keyword, Keyword: keyword,
}) })
code, body, err := request(_sling, jsonAcceptHeader, authInfo) code, body, err := request(_sling, jsonAcceptHeader, authInfo)
@ -498,15 +499,7 @@ func (a testapi) GetRepos(authInfo usrInfo, projectID,
} }
if code == http.StatusOK { if code == http.StatusOK {
if detail == "1" || detail == "true" { repositories := []repoResp{}
repositories := []repoResp{}
if err = json.Unmarshal(body, &repositories); err != nil {
return 0, nil, err
}
return code, repositories, nil
}
repositories := []string{}
if err = json.Unmarshal(body, &repositories); err != nil { if err = json.Unmarshal(body, &repositories); err != nil {
return 0, nil, err return 0, nil, err
} }
@ -517,21 +510,13 @@ func (a testapi) GetRepos(authInfo usrInfo, projectID,
} }
//Get tags of a relevant repository //Get tags of a relevant repository
func (a testapi) GetReposTags(authInfo usrInfo, repoName, func (a testapi) GetReposTags(authInfo usrInfo, repoName string) (int, interface{}, error) {
detail string) (int, interface{}, error) {
_sling := sling.New().Get(a.basePath) _sling := sling.New().Get(a.basePath)
path := fmt.Sprintf("/api/repositories/%s/tags", repoName) path := fmt.Sprintf("/api/repositories/%s/tags", repoName)
_sling = _sling.Path(path) _sling = _sling.Path(path)
type QueryParams struct {
Detail string `url:"detail"`
}
_sling = _sling.QueryStruct(&QueryParams{
Detail: detail,
})
httpStatusCode, body, err := request(_sling, jsonAcceptHeader, authInfo) httpStatusCode, body, err := request(_sling, jsonAcceptHeader, authInfo)
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
@ -541,15 +526,7 @@ func (a testapi) GetReposTags(authInfo usrInfo, repoName,
return httpStatusCode, body, nil return httpStatusCode, body, nil
} }
if detail == "true" || detail == "1" { result := []tagResp{}
result := []detailedTagResp{}
if err := json.Unmarshal(body, &result); err != nil {
return 0, nil, err
}
return http.StatusOK, result, nil
}
result := []string{}
if err := json.Unmarshal(body, &result); err != nil { if err := json.Unmarshal(body, &result); err != nil {
return 0, nil, err return 0, nil, err
} }
@ -569,8 +546,7 @@ func (a testapi) GetReposManifests(authInfo usrInfo, repoName string, tag string
} }
//Get public repositories which are accessed most //Get public repositories which are accessed most
func (a testapi) GetReposTop(authInfo usrInfo, count, func (a testapi) GetReposTop(authInfo usrInfo, count string) (int, interface{}, error) {
detail string) (int, interface{}, error) {
_sling := sling.New().Get(a.basePath) _sling := sling.New().Get(a.basePath)
path := "/api/repositories/top" path := "/api/repositories/top"
@ -578,13 +554,11 @@ func (a testapi) GetReposTop(authInfo usrInfo, count,
_sling = _sling.Path(path) _sling = _sling.Path(path)
type QueryParams struct { type QueryParams struct {
Count string `url:"count"` Count string `url:"count"`
Detail string `url:"detail"`
} }
_sling = _sling.QueryStruct(&QueryParams{ _sling = _sling.QueryStruct(&QueryParams{
Count: count, Count: count,
Detail: detail,
}) })
code, body, err := request(_sling, jsonAcceptHeader, authInfo) code, body, err := request(_sling, jsonAcceptHeader, authInfo)
if err != nil { if err != nil {
@ -595,15 +569,7 @@ func (a testapi) GetReposTop(authInfo usrInfo, count,
return code, body, err return code, body, err
} }
if detail == "true" || detail == "1" { result := []*repoResp{}
result := []*repoResp{}
if err = json.Unmarshal(body, &result); err != nil {
return 0, nil, err
}
return http.StatusOK, result, nil
}
result := []*models.TopRepo{}
if err = json.Unmarshal(body, &result); err != nil { if err = json.Unmarshal(body, &result); err != nil {
return 0, nil, err return 0, nil, err
} }

View File

@ -164,7 +164,7 @@ func (ra *RepositoryAPI) Delete() {
return return
} }
rc, err := ra.initRepositoryClient(ra.SecurityCxt.GetUsername(), repoName) rc, err := ra.initRepositoryClient(repoName)
if err != nil { if err != nil {
log.Errorf("error occurred while initializing repository client for %s: %v", repoName, err) log.Errorf("error occurred while initializing repository client for %s: %v", repoName, err)
ra.CustomAbort(http.StatusInternalServerError, "internal error") ra.CustomAbort(http.StatusInternalServerError, "internal error")
@ -288,7 +288,7 @@ func (ra *RepositoryAPI) GetTags() {
} }
} }
client, err := ra.initRepositoryClient(ra.SecurityCxt.GetUsername(), repoName) client, err := ra.initRepositoryClient(repoName)
if err != nil { if err != nil {
log.Errorf("error occurred while initializing repository client for %s: %v", repoName, err) log.Errorf("error occurred while initializing repository client for %s: %v", repoName, err)
ra.CustomAbort(http.StatusInternalServerError, "internal error") ra.CustomAbort(http.StatusInternalServerError, "internal error")
@ -383,7 +383,7 @@ func (ra *RepositoryAPI) GetManifests() {
} }
} }
rc, err := ra.initRepositoryClient(ra.SecurityCxt.GetUsername(), repoName) rc, err := ra.initRepositoryClient(repoName)
if err != nil { if err != nil {
log.Errorf("error occurred while initializing repository client for %s: %v", repoName, err) log.Errorf("error occurred while initializing repository client for %s: %v", repoName, err)
ra.CustomAbort(http.StatusInternalServerError, "internal error") ra.CustomAbort(http.StatusInternalServerError, "internal error")
@ -445,14 +445,14 @@ func getManifest(client *registry.Repository,
return result, nil return result, nil
} }
func (ra *RepositoryAPI) initRepositoryClient(username, repoName string) (r *registry.Repository, err error) { func (ra *RepositoryAPI) initRepositoryClient(repoName string) (r *registry.Repository, err error) {
endpoint, err := config.RegistryURL() endpoint, err := config.RegistryURL()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return NewRepositoryClient(endpoint, true, username, repoName, return NewRepositoryClient(endpoint, true, ra.SecurityCxt.GetUsername(),
"repository", repoName, "pull", "push", "*") repoName, "repository", repoName, "pull", "push", "*")
} }
//GetTopRepos returns the most populor repositories //GetTopRepos returns the most populor repositories

View File

@ -18,7 +18,6 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/vmware/harbor/src/common/models"
) )
func TestGetRepos(t *testing.T) { func TestGetRepos(t *testing.T) {
@ -27,12 +26,11 @@ func TestGetRepos(t *testing.T) {
apiTest := newHarborAPI() apiTest := newHarborAPI()
projectID := "1" projectID := "1"
keyword := "hello-world" keyword := "hello-world"
detail := "true"
fmt.Println("Testing Repos Get API") fmt.Println("Testing Repos Get API")
//-------------------case 1 : response code = 200------------------------// //-------------------case 1 : response code = 200------------------------//
fmt.Println("case 1 : response code = 200") fmt.Println("case 1 : response code = 200")
code, repositories, err := apiTest.GetRepos(*admin, projectID, keyword, detail) code, repositories, err := apiTest.GetRepos(*admin, projectID, keyword)
if err != nil { if err != nil {
t.Errorf("failed to get repositories: %v", err) t.Errorf("failed to get repositories: %v", err)
} else { } else {
@ -41,14 +39,14 @@ func TestGetRepos(t *testing.T) {
assert.Equal(int(1), len(repos), "the length of repositories should be 1") assert.Equal(int(1), len(repos), "the length of repositories should be 1")
assert.Equal(repos[0].Name, "library/hello-world", "unexpected repository name") assert.Equal(repos[0].Name, "library/hello-world", "unexpected repository name")
} else { } else {
t.Error("the response should return more info as detail is true") t.Error("unexpected reponse")
} }
} }
//-------------------case 2 : response code = 404------------------------// //-------------------case 2 : response code = 404------------------------//
fmt.Println("case 2 : response code = 404:project not found") fmt.Println("case 2 : response code = 404:project not found")
projectID = "111" projectID = "111"
httpStatusCode, _, err := apiTest.GetRepos(*admin, projectID, keyword, detail) httpStatusCode, _, err := apiTest.GetRepos(*admin, projectID, keyword)
if err != nil { if err != nil {
t.Error("Error whihle get repos by projectID", err.Error()) t.Error("Error whihle get repos by projectID", err.Error())
t.Log(err) t.Log(err)
@ -56,27 +54,10 @@ func TestGetRepos(t *testing.T) {
assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404") assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404")
} }
//-------------------case 3 : response code = 200------------------------// //-------------------case 3 : response code = 400------------------------//
fmt.Println("case 3 : response code = 200") fmt.Println("case 3 : response code = 400,invalid project_id")
projectID = "1"
detail = "false"
code, repositories, err = apiTest.GetRepos(*admin, projectID, keyword, detail)
if err != nil {
t.Errorf("failed to get repositories: %v", err)
} else {
assert.Equal(int(200), code, "response code should be 200")
if repos, ok := repositories.([]string); ok {
assert.Equal(int(1), len(repos), "the length of repositories should be 1")
assert.Equal(repos[0], "library/hello-world", "unexpected repository name")
} else {
t.Error("the response should not return detail info as detail is false")
}
}
//-------------------case 4 : response code = 400------------------------//
fmt.Println("case 4 : response code = 400,invalid project_id")
projectID = "ccc" projectID = "ccc"
httpStatusCode, _, err = apiTest.GetRepos(*admin, projectID, keyword, detail) httpStatusCode, _, err = apiTest.GetRepos(*admin, projectID, keyword)
if err != nil { if err != nil {
t.Error("Error whihle get repos by projectID", err.Error()) t.Error("Error whihle get repos by projectID", err.Error())
t.Log(err) t.Log(err)
@ -95,8 +76,7 @@ func TestGetReposTags(t *testing.T) {
//-------------------case 1 : response code = 404------------------------// //-------------------case 1 : response code = 404------------------------//
fmt.Println("case 1 : response code = 404,repo not found") fmt.Println("case 1 : response code = 404,repo not found")
repository := "errorRepos" repository := "errorRepos"
detail := "false" code, _, err := apiTest.GetReposTags(*admin, repository)
code, _, err := apiTest.GetReposTags(*admin, repository, detail)
if err != nil { if err != nil {
t.Errorf("failed to get tags of repository %s: %v", repository, err) t.Errorf("failed to get tags of repository %s: %v", repository, err)
} else { } else {
@ -105,33 +85,16 @@ func TestGetReposTags(t *testing.T) {
//-------------------case 2 : response code = 200------------------------// //-------------------case 2 : response code = 200------------------------//
fmt.Println("case 2 : response code = 200") fmt.Println("case 2 : response code = 200")
repository = "library/hello-world" repository = "library/hello-world"
code, tags, err := apiTest.GetReposTags(*admin, repository, detail) code, tags, err := apiTest.GetReposTags(*admin, repository)
if err != nil { if err != nil {
t.Errorf("failed to get tags of repository %s: %v", repository, err) t.Errorf("failed to get tags of repository %s: %v", repository, err)
} else { } else {
assert.Equal(int(200), code, "httpStatusCode should be 200") assert.Equal(int(200), code, "httpStatusCode should be 200")
if tg, ok := tags.([]string); ok { if tg, ok := tags.([]tagResp); ok {
assert.Equal(1, len(tg), fmt.Sprintf("there should be only one tag, but now %v", tg))
assert.Equal(tg[0], "latest", "the tag should be latest")
} else {
t.Error("the tags should be in simple style as the detail is false")
}
}
//-------------------case 3 : response code = 200------------------------//
fmt.Println("case 3 : response code = 200")
repository = "library/hello-world"
detail = "true"
code, tags, err = apiTest.GetReposTags(*admin, repository, detail)
if err != nil {
t.Errorf("failed to get tags of repository %s: %v", repository, err)
} else {
assert.Equal(int(200), code, "httpStatusCode should be 200")
if tg, ok := tags.([]detailedTagResp); ok {
assert.Equal(1, len(tg), fmt.Sprintf("there should be only one tag, but now %v", tg)) assert.Equal(1, len(tg), fmt.Sprintf("there should be only one tag, but now %v", tg))
assert.Equal(tg[0].Tag, "latest", "the tag should be latest") assert.Equal(tg[0].Tag, "latest", "the tag should be latest")
} else { } else {
t.Error("the tags should be in detail style as the detail is true") t.Error("unexpected response")
} }
} }
@ -190,38 +153,20 @@ func TestGetReposTop(t *testing.T) {
apiTest := newHarborAPI() apiTest := newHarborAPI()
fmt.Println("Testing ReposTop Get API") fmt.Println("Testing ReposTop Get API")
//-------------------case 1 : response code = 200------------------------// //-------------------case 1 : response code = 400------------------------//
fmt.Println("case 1 : response code = 200") fmt.Println("case 1 : response code = 400,invalid count")
count := "1" count := "cc"
detail := "false" code, _, err := apiTest.GetReposTop(*admin, count)
code, repos, err := apiTest.GetReposTop(*admin, count, detail)
if err != nil {
t.Errorf("failed to get the most popular repositories: %v", err)
} else {
assert.Equal(int(200), code, "response code should be 200")
if r, ok := repos.([]*models.TopRepo); ok {
assert.Equal(int(1), len(r), "the length should be 1")
assert.Equal(r[0].RepoName, "library/docker", "the name of repository should be library/docker")
} else {
t.Error("the repositories should in simple style as the detail is false")
}
}
//-------------------case 2 : response code = 400------------------------//
fmt.Println("case 2 : response code = 400,invalid count")
count = "cc"
code, _, err = apiTest.GetReposTop(*admin, count, detail)
if err != nil { if err != nil {
t.Errorf("failed to get the most popular repositories: %v", err) t.Errorf("failed to get the most popular repositories: %v", err)
} else { } else {
assert.Equal(int(400), code, "response code should be 400") assert.Equal(int(400), code, "response code should be 400")
} }
//-------------------case 3 : response code = 200------------------------// //-------------------case 2 : response code = 200------------------------//
fmt.Println("case 3 : response code = 200") fmt.Println("case 2 : response code = 200")
count = "1" count = "1"
detail = "true" code, repos, err := apiTest.GetReposTop(*admin, count)
code, repos, err = apiTest.GetReposTop(*admin, count, detail)
if err != nil { if err != nil {
t.Errorf("failed to get the most popular repositories: %v", err) t.Errorf("failed to get the most popular repositories: %v", err)
} else { } else {
@ -230,7 +175,7 @@ func TestGetReposTop(t *testing.T) {
assert.Equal(int(1), len(r), "the length should be 1") assert.Equal(int(1), len(r), "the length should be 1")
assert.Equal(r[0].Name, "library/docker", "the name of repository should be library/docker") assert.Equal(r[0].Name, "library/docker", "the name of repository should be library/docker")
} else { } else {
t.Error("the repositories should in detail style as the detail is true") t.Error("unexpected response")
} }
} }

View File

@ -118,7 +118,14 @@ func (p *ProjectManager) GetPublic() []models.Project {
// GetByMember returns all projects which the user is a member of // GetByMember returns all projects which the user is a member of
func (p *ProjectManager) GetByMember(username string) []models.Project { func (p *ProjectManager) GetByMember(username string) []models.Project {
projects, err := dao.GetProjects(username) user, err := dao.GetUser(models.User{
Username: username,
})
if err != nil {
log.Errorf("failed to get user %s: %v", username, err)
return []models.Project{}
}
projects, err := dao.GetUserRelevantProjects(user.UserID, "")
if err != nil { if err != nil {
log.Errorf("failed to get projects of %s: %v", username, err) log.Errorf("failed to get projects of %s: %v", username, err)
return []models.Project{} return []models.Project{}

View File

@ -71,8 +71,9 @@ func initRouters() {
beego.Router("/api/users/?:id", &api.UserAPI{}) beego.Router("/api/users/?:id", &api.UserAPI{})
beego.Router("/api/users/:id([0-9]+)/password", &api.UserAPI{}, "put:ChangePassword") beego.Router("/api/users/:id([0-9]+)/password", &api.UserAPI{}, "put:ChangePassword")
beego.Router("/api/internal/syncregistry", &api.InternalAPI{}, "post:SyncRegistry") beego.Router("/api/internal/syncregistry", &api.InternalAPI{}, "post:SyncRegistry")
beego.Router("/api/repositories", &api.RepositoryAPI{}) beego.Router("/api/repositories", &api.RepositoryAPI{}, "get:Get")
beego.Router("/api/repositories/*/tags/?:tag", &api.RepositoryAPI{}, "delete:Delete") beego.Router("/api/repositories/*", &api.RepositoryAPI{}, "delete:Delete")
beego.Router("/api/repositories/*/tags/:tag", &api.RepositoryAPI{}, "delete:Delete")
beego.Router("/api/repositories/*/tags", &api.RepositoryAPI{}, "get:GetTags") beego.Router("/api/repositories/*/tags", &api.RepositoryAPI{}, "get:GetTags")
beego.Router("/api/repositories/*/tags/:tag/manifest", &api.RepositoryAPI{}, "get:GetManifests") beego.Router("/api/repositories/*/tags/:tag/manifest", &api.RepositoryAPI{}, "get:GetManifests")
beego.Router("/api/repositories/*/signatures", &api.RepositoryAPI{}, "get:GetSignatures") beego.Router("/api/repositories/*/signatures", &api.RepositoryAPI{}, "get:GetSignatures")

View File

@ -34,14 +34,14 @@ export class RepositoryService {
params.set('page_size', pageSize + ''); params.set('page_size', pageSize + '');
} }
return this.http return this.http
.get(`/api/repositories?project_id=${projectId}&q=${repoName}&detail=1`, {search: params}) .get(`/api/repositories?project_id=${projectId}&q=${repoName}`, {search: params})
.map(response=>response) .map(response=>response)
.catch(error=>Observable.throw(error)); .catch(error=>Observable.throw(error));
} }
listTags(repoName: string): Observable<Tag[]> { listTags(repoName: string): Observable<Tag[]> {
return this.http return this.http
.get(`/api/repositories/${repoName}/tags?detail=1`) .get(`/api/repositories/${repoName}/tags`)
.map(response=>response.json()) .map(response=>response.json())
.catch(error=>Observable.throw(error)); .catch(error=>Observable.throw(error));
} }
@ -77,7 +77,7 @@ export class RepositoryService {
deleteRepository(repoName: string): Observable<any> { deleteRepository(repoName: string): Observable<any> {
return this.http return this.http
.delete(`/api/repositories/${repoName}/tags`) .delete(`/api/repositories/${repoName}`)
.map(response=>response.status) .map(response=>response.status)
.catch(error=>Observable.throw(error)); .catch(error=>Observable.throw(error));
} }

View File

@ -17,7 +17,7 @@ import 'rxjs/add/operator/toPromise';
import { Repository } from '../repository'; import { Repository } from '../repository';
export const topRepoEndpoint = "/api/repositories/top?detail=1"; export const topRepoEndpoint = "/api/repositories/top";
/** /**
* Declare service to handle the top repositories * Declare service to handle the top repositories
* *