From 3b9a2fd69ec67560c3307e9a1f5f925900ea5e9f Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Fri, 8 Nov 2019 18:03:48 +0800 Subject: [PATCH] Update the test cases of user API Update the test cases of user API Signed-off-by: Wenkai Yin --- src/core/api/harborapi_test.go | 7 +----- src/core/api/user.go | 3 ++- src/core/api/user_test.go | 36 ++++++++++++++++++++++----- tests/apitests/apilib/user_profile.go | 7 +++--- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/core/api/harborapi_test.go b/src/core/api/harborapi_test.go index a60ab8873..87a41fece 100644 --- a/src/core/api/harborapi_test.go +++ b/src/core/api/harborapi_test.go @@ -834,16 +834,11 @@ func (a testapi) UsersSearch(userName string, authInfo ...usrInfo) (int, []apili } // Get registered users by userid. -func (a testapi) UsersGetByID(userName string, authInfo usrInfo, userID int) (int, apilib.User, error) { +func (a testapi) UsersGetByID(userID int, authInfo usrInfo) (int, apilib.User, error) { _sling := sling.New().Get(a.basePath) // create path and map variables path := "/api/users/" + fmt.Sprintf("%d", userID) _sling = _sling.Path(path) - // body params - type QueryParams struct { - UserName string `url:"username, omitempty"` - } - _sling = _sling.QueryStruct(&QueryParams{UserName: userName}) httpStatusCode, body, err := request(_sling, jsonAcceptHeader, authInfo) var successPayLoad apilib.User if 200 == httpStatusCode && nil == err { diff --git a/src/core/api/user.go b/src/core/api/user.go index 1f136d0af..7ffaa4cb1 100644 --- a/src/core/api/user.go +++ b/src/core/api/user.go @@ -261,11 +261,12 @@ func (ua *UserAPI) Put() { ua.SendForbiddenError(fmt.Errorf("User with ID %d cannot be modified", ua.userID)) return } - user := models.User{UserID: ua.userID} + user := models.User{} if err := ua.DecodeJSONReq(&user); err != nil { ua.SendBadRequestError(err) return } + user.UserID = ua.userID err := commonValidate(user) if err != nil { log.Warningf("Bad request in change user profile: %v", err) diff --git a/src/core/api/user_test.go b/src/core/api/user_test.go index 1ae6354f0..1bb3ec2be 100644 --- a/src/core/api/user_test.go +++ b/src/core/api/user_test.go @@ -257,7 +257,7 @@ func TestUsersGetByID(t *testing.T) { apiTest := newHarborAPI() // case 1: Get user2 with userID and his own auth, expect 200 - code, user, err := apiTest.UsersGetByID(testUser0002.Username, *testUser0002Auth, testUser0002ID) + code, user, err := apiTest.UsersGetByID(testUser0002ID, *testUser0002Auth) if err != nil { t.Error("Error occurred while get users", err.Error()) t.Log(err) @@ -280,7 +280,7 @@ func TestUsersGetByID(t *testing.T) { } testUser0003Auth = &usrInfo{"testUser0003", "testUser0003"} - code, user, err = apiTest.UsersGetByID(testUser0002.Username, *testUser0003Auth, testUser0002ID) + code, user, err = apiTest.UsersGetByID(testUser0002ID, *testUser0003Auth) if err != nil { t.Error("Error occurred while get users", err.Error()) t.Log(err) @@ -288,7 +288,7 @@ func TestUsersGetByID(t *testing.T) { assert.Equal(403, code, "Get users status should be 403") } // case 3: Get user that does not exist with user2 auth, expect 404 not found. - code, user, err = apiTest.UsersGetByID(testUser0002.Username, *testUser0002Auth, 1000) + code, user, err = apiTest.UsersGetByID(1000, *testUser0002Auth) if err != nil { t.Error("Error occurred while get users", err.Error()) t.Log(err) @@ -320,7 +320,31 @@ func TestUsersPut(t *testing.T) { } else { assert.Equal(403, code, "Change user profile status should be 403") } - // case 2: change user2 profile with user2 auth, but bad parameters format. + // case 2: change user3 profile with user2 auth + realname := "new realname" + email := "new_email@mydomain.com" + comment := "new comment" + profile.UserID = testUser0003ID + profile.Realname = realname + profile.Email = email + profile.Comment = comment + code, err = apiTest.UsersPut(testUser0002ID, profile, *testUser0002Auth) + if err != nil { + t.Error("Error occurred while change user profile", err.Error()) + t.Log(err) + } else { + assert.Equal(200, code, "Change user profile status should be 200") + } + _, user, err := apiTest.UsersGetByID(testUser0003ID, *testUser0003Auth) + if err != nil { + t.Error("Error occurred while get user", err.Error()) + } else { + assert.NotEqual(realname, user.Realname) + assert.NotEqual(email, user.Email) + assert.NotEqual(comment, user.Comment) + } + // case 3: change user2 profile with user2 auth, but bad parameters format. + profile = apilib.UserProfile{} code, err = apiTest.UsersPut(testUser0002ID, profile, *testUser0002Auth) if err != nil { t.Error("Error occurred while change user profile", err.Error()) @@ -328,7 +352,7 @@ func TestUsersPut(t *testing.T) { } else { assert.Equal(400, code, "Change user profile status should be 400") } - // case 3: change user2 profile with user2 auth, but duplicate email. + // case 4: change user2 profile with user2 auth, but duplicate email. profile.Realname = "test user" profile.Email = "testUser0003@mydomain.com" profile.Comment = "change profile" @@ -339,7 +363,7 @@ func TestUsersPut(t *testing.T) { } else { assert.Equal(409, code, "Change user profile status should be 409") } - // case 4: change user2 profile with user2 auth, right parameters format. + // case 5: change user2 profile with user2 auth, right parameters format. profile.Realname = "test user" profile.Email = "testUser0002@vmware.com" profile.Comment = "change profile" diff --git a/tests/apitests/apilib/user_profile.go b/tests/apitests/apilib/user_profile.go index 73fb82a0c..96adcd398 100644 --- a/tests/apitests/apilib/user_profile.go +++ b/tests/apitests/apilib/user_profile.go @@ -23,9 +23,8 @@ package apilib type UserProfile struct { - Email string `json:"email,omitempty"` - + UserID int `json:"user_id,omitempty"` + Email string `json:"email,omitempty"` Realname string `json:"realname,omitempty"` - - Comment string `json:"comment,omitempty"` + Comment string `json:"comment,omitempty"` }