From 18402f84f00012b05c4c67d46c30fa9dcf392f94 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Wed, 8 Mar 2017 18:34:04 +0800 Subject: [PATCH] modify test cases --- src/ui/api/harborapi_test.go | 30 +++++++++++++++++++++--- src/ui/api/repository_test.go | 43 ++++++++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/src/ui/api/harborapi_test.go b/src/ui/api/harborapi_test.go index 57cc8ea11..ba663b604 100644 --- a/src/ui/api/harborapi_test.go +++ b/src/ui/api/harborapi_test.go @@ -7,6 +7,7 @@ import ( "fmt" "io/ioutil" "log" + "net/http" "net/http/httptest" "path/filepath" "runtime" @@ -453,7 +454,8 @@ func (a testapi) PutProjectMember(authInfo usrInfo, projectID string, userID str //-------------------------Repositories Test---------------------------------------// //Return relevant repos of projectID -func (a testapi) GetRepos(authInfo usrInfo, projectID, detail string) (int, error) { +func (a testapi) GetRepos(authInfo usrInfo, projectID, + keyword, detail string) (int, interface{}, error) { _sling := sling.New().Get(a.basePath) path := "/api/repositories/" @@ -463,14 +465,36 @@ func (a testapi) GetRepos(authInfo usrInfo, projectID, detail string) (int, erro type QueryParams struct { ProjectID string `url:"project_id"` Detail string `url:"detail"` + Keyword string `url:"q"` } _sling = _sling.QueryStruct(&QueryParams{ ProjectID: projectID, Detail: detail, + Keyword: keyword, }) - httpStatusCode, _, err := request(_sling, jsonAcceptHeader, authInfo) - return httpStatusCode, err + code, body, err := request(_sling, jsonAcceptHeader, authInfo) + if err != nil { + return 0, nil, err + } + + if code == http.StatusOK { + if detail == "1" || detail == "true" { + 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 { + return 0, nil, err + } + return code, repositories, nil + } + + return code, nil, nil } //Get tags of a relevant repository diff --git a/src/ui/api/repository_test.go b/src/ui/api/repository_test.go index a473bdadb..a556e3955 100644 --- a/src/ui/api/repository_test.go +++ b/src/ui/api/repository_test.go @@ -5,32 +5,36 @@ import ( "testing" "github.com/stretchr/testify/assert" - // "github.com/vmware/harbor/tests/apitests/apilib" - // "strconv" ) func TestGetRepos(t *testing.T) { - var httpStatusCode int - var err error assert := assert.New(t) apiTest := newHarborAPI() projectID := "1" + keyword := "hello-world" + detail := "true" fmt.Println("Testing Repos Get API") //-------------------case 1 : response code = 200------------------------// fmt.Println("case 1 : response code = 200") - httpStatusCode, err = apiTest.GetRepos(*admin, projectID, "true") + code, repositories, err := apiTest.GetRepos(*admin, projectID, keyword, detail) if err != nil { - t.Error("Error whihle get repos by projectID", err.Error()) - t.Log(err) + t.Errorf("failed to get repositories: %v", err) } else { - assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200") + assert.Equal(int(200), code, "response code should be 200") + if repos, ok := repositories.([]repoResp); ok { + assert.Equal(int(1), len(repos), "the length of repositories should be 1") + assert.Equal(repos[0].Name, "library/hello-world", "unexpected repository name") + } else { + t.Error("the response should return more info as detail is true") + } } + //-------------------case 2 : response code = 400------------------------// - fmt.Println("case 2 : response code = 409,invalid project_id") + fmt.Println("case 2 : response code = 400,invalid project_id") projectID = "ccc" - httpStatusCode, err = apiTest.GetRepos(*admin, projectID, "0") + httpStatusCode, _, err := apiTest.GetRepos(*admin, projectID, keyword, detail) if err != nil { t.Error("Error whihle get repos by projectID", err.Error()) t.Log(err) @@ -40,7 +44,7 @@ func TestGetRepos(t *testing.T) { //-------------------case 3 : response code = 404------------------------// fmt.Println("case 3 : response code = 404:project not found") projectID = "111" - httpStatusCode, err = apiTest.GetRepos(*admin, projectID, "0") + httpStatusCode, _, err = apiTest.GetRepos(*admin, projectID, keyword, detail) if err != nil { t.Error("Error whihle get repos by projectID", err.Error()) t.Log(err) @@ -48,6 +52,23 @@ func TestGetRepos(t *testing.T) { assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404") } + //-------------------case 4 : response code = 200------------------------// + fmt.Println("case 4 : response code = 200") + 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") + } + } + fmt.Printf("\n") }