From 9252dbe7a0d4526cdf9713aed229d6ac3d5d42f1 Mon Sep 17 00:00:00 2001 From: zgdxiaoxiao Date: Fri, 28 Oct 2016 14:30:19 +0800 Subject: [PATCH] add test case of search api --- src/ui/api/harborapi_test.go | 21 +++++++++++++++++---- src/ui/api/search_test.go | 28 +++++++++++++++++++--------- src/ui/api/statistic_test.go | 4 ---- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/ui/api/harborapi_test.go b/src/ui/api/harborapi_test.go index 5bfb200fb..3169a6ed9 100644 --- a/src/ui/api/harborapi_test.go +++ b/src/ui/api/harborapi_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "log" "net/http/httptest" "path/filepath" "runtime" @@ -90,6 +91,11 @@ func init() { _ = updateInitPassword(1, "Harbor12345") + //syncRegistry + if err := SyncRegistry(); err != nil { + log.Fatalf("failed to sync repositories from registry: %v", err) + } + //Init user Info admin = &usrInfo{adminName, adminPwd} 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. //@param q Search parameter for project and repository name. //@return []Search -//func (a testapi) SearchGet (q string) (apilib.Search, error) { -func (a testapi) SearchGet(q string) (apilib.Search, error) { +func (a testapi) SearchGet(q string, authInfo ...usrInfo) (int, apilib.Search, error) { + var httpCode int + var body []byte + var err error _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}) - _, 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) err = json.Unmarshal(body, &successPayload) - return *successPayload, err + return httpCode, *successPayload, err } //Create a new project. diff --git a/src/ui/api/search_test.go b/src/ui/api/search_test.go index 1eecd1009..ac05f74f2 100644 --- a/src/ui/api/search_test.go +++ b/src/ui/api/search_test.go @@ -14,19 +14,29 @@ func TestSearch(t *testing.T) { apiTest := newHarborAPI() 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 { t.Error("Error while search project or repository", err.Error()) t.Log(err) } else { - assert.Equal(result.Projects[0].Id, int64(1), "Project id should be equal") - assert.Equal(result.Projects[0].Name, "library", "Project name should be library") - assert.Equal(result.Projects[0].Public, int32(1), "Project public status should be 1 (true)") - //t.Log(result) + assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200") + assert.Equal(int64(1), result.Projects[0].Id, "Project id should be equal") + assert.Equal("library", result.Projects[0].Name, "Project name should be library") + 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) - //} } diff --git a/src/ui/api/statistic_test.go b/src/ui/api/statistic_test.go index 17e2b9f5a..37400bea4 100644 --- a/src/ui/api/statistic_test.go +++ b/src/ui/api/statistic_test.go @@ -10,10 +10,6 @@ import ( ) 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") assert := assert.New(t)