Merge pull request #970 from zgdxiaoxiao/api-test3

add test case of search api
This commit is contained in:
Daniel Jiang 2016-10-28 16:56:33 +08:00 committed by GitHub
commit c732b624e9
3 changed files with 36 additions and 17 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http/httptest" "net/http/httptest"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -90,6 +91,11 @@ func init() {
_ = updateInitPassword(1, "Harbor12345") _ = updateInitPassword(1, "Harbor12345")
//syncRegistry
if err := SyncRegistry(); err != nil {
log.Fatalf("failed to sync repositories from registry: %v", err)
}
//Init user Info //Init user Info
admin = &usrInfo{adminName, adminPwd} admin = &usrInfo{adminName, adminPwd}
unknownUsr = &usrInfo{"unknown", "unknown"} unknownUsr = &usrInfo{"unknown", "unknown"}
@ -119,8 +125,10 @@ func request(_sling *sling.Sling, acceptHeader string, authInfo ...usrInfo) (int
//The response includes the project and repository list in a proper display order. //The response includes the project and repository list in a proper display order.
//@param q Search parameter for project and repository name. //@param q Search parameter for project and repository name.
//@return []Search //@return []Search
//func (a testapi) SearchGet (q string) (apilib.Search, error) { func (a testapi) SearchGet(q string, authInfo ...usrInfo) (int, apilib.Search, error) {
func (a testapi) SearchGet(q string) (apilib.Search, error) { var httpCode int
var body []byte
var err error
_sling := sling.New().Get(a.basePath) _sling := sling.New().Get(a.basePath)
@ -134,10 +142,15 @@ func (a testapi) SearchGet(q string) (apilib.Search, error) {
_sling = _sling.QueryStruct(&QueryParams{Query: q}) _sling = _sling.QueryStruct(&QueryParams{Query: q})
_, body, err := request(_sling, jsonAcceptHeader) if len(authInfo) > 0 {
httpCode, body, err = request(_sling, jsonAcceptHeader, authInfo[0])
} else {
httpCode, body, err = request(_sling, jsonAcceptHeader)
}
var successPayload = new(apilib.Search) var successPayload = new(apilib.Search)
err = json.Unmarshal(body, &successPayload) err = json.Unmarshal(body, &successPayload)
return *successPayload, err return httpCode, *successPayload, err
} }
//Create a new project. //Create a new project.

View File

@ -14,19 +14,29 @@ func TestSearch(t *testing.T) {
apiTest := newHarborAPI() apiTest := newHarborAPI()
var result apilib.Search var result apilib.Search
result, err := apiTest.SearchGet("library")
//fmt.Printf("%+v\n", result) //-------------case 1 : Response Code = 200, Not sysAdmin --------------//
httpStatusCode, result, err := apiTest.SearchGet("library")
if err != nil { if err != nil {
t.Error("Error while search project or repository", err.Error()) t.Error("Error while search project or repository", err.Error())
t.Log(err) t.Log(err)
} else { } else {
assert.Equal(result.Projects[0].Id, int64(1), "Project id should be equal") assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
assert.Equal(result.Projects[0].Name, "library", "Project name should be library") assert.Equal(int64(1), result.Projects[0].Id, "Project id should be equal")
assert.Equal(result.Projects[0].Public, int32(1), "Project public status should be 1 (true)") assert.Equal("library", result.Projects[0].Name, "Project name should be library")
//t.Log(result) assert.Equal(int32(1), result.Projects[0].Public, "Project public status should be 1 (true)")
}
//--------case 2 : Response Code = 200, sysAdmin and search repo--------//
httpStatusCode, result, err = apiTest.SearchGet("docker", *admin)
if err != nil {
t.Error("Error while search project or repository", err.Error())
t.Log(err)
} else {
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
assert.Equal("library", result.Repositories[0].ProjectName, "Project name should be library")
assert.Equal("library/docker", result.Repositories[0].RepositoryName, "Repository name should be library/docker")
assert.Equal(int32(1), result.Repositories[0].ProjectPublic, "Project public status should be 1 (true)")
} }
//if result.Response.StatusCode != 200 {
// t.Log(result.Response)
//}
} }

View File

@ -10,10 +10,6 @@ import (
) )
func TestStatisticGet(t *testing.T) { func TestStatisticGet(t *testing.T) {
if err := SyncRegistry(); err != nil {
t.Fatalf("failed to sync repositories from registry: %v", err)
}
fmt.Println("Testing Statistic API") fmt.Println("Testing Statistic API")
assert := assert.New(t) assert := assert.New(t)