Merge pull request #9817 from ywk253100/191108_user

Update the test cases of user API
This commit is contained in:
Wang Yan 2019-11-12 11:34:02 +08:00 committed by GitHub
commit 7910e9f239
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 17 deletions

View File

@ -834,16 +834,11 @@ func (a testapi) UsersSearch(userName string, authInfo ...usrInfo) (int, []apili
} }
// Get registered users by userid. // 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) _sling := sling.New().Get(a.basePath)
// create path and map variables // create path and map variables
path := "/api/users/" + fmt.Sprintf("%d", userID) path := "/api/users/" + fmt.Sprintf("%d", userID)
_sling = _sling.Path(path) _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) httpStatusCode, body, err := request(_sling, jsonAcceptHeader, authInfo)
var successPayLoad apilib.User var successPayLoad apilib.User
if 200 == httpStatusCode && nil == err { if 200 == httpStatusCode && nil == err {

View File

@ -261,11 +261,12 @@ func (ua *UserAPI) Put() {
ua.SendForbiddenError(fmt.Errorf("User with ID %d cannot be modified", ua.userID)) ua.SendForbiddenError(fmt.Errorf("User with ID %d cannot be modified", ua.userID))
return return
} }
user := models.User{UserID: ua.userID} user := models.User{}
if err := ua.DecodeJSONReq(&user); err != nil { if err := ua.DecodeJSONReq(&user); err != nil {
ua.SendBadRequestError(err) ua.SendBadRequestError(err)
return return
} }
user.UserID = ua.userID
err := commonValidate(user) err := commonValidate(user)
if err != nil { if err != nil {
log.Warningf("Bad request in change user profile: %v", err) log.Warningf("Bad request in change user profile: %v", err)

View File

@ -257,7 +257,7 @@ func TestUsersGetByID(t *testing.T) {
apiTest := newHarborAPI() apiTest := newHarborAPI()
// case 1: Get user2 with userID and his own auth, expect 200 // 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 { if err != nil {
t.Error("Error occurred while get users", err.Error()) t.Error("Error occurred while get users", err.Error())
t.Log(err) t.Log(err)
@ -280,7 +280,7 @@ func TestUsersGetByID(t *testing.T) {
} }
testUser0003Auth = &usrInfo{"testUser0003", "testUser0003"} testUser0003Auth = &usrInfo{"testUser0003", "testUser0003"}
code, user, err = apiTest.UsersGetByID(testUser0002.Username, *testUser0003Auth, testUser0002ID) code, user, err = apiTest.UsersGetByID(testUser0002ID, *testUser0003Auth)
if err != nil { if err != nil {
t.Error("Error occurred while get users", err.Error()) t.Error("Error occurred while get users", err.Error())
t.Log(err) t.Log(err)
@ -288,7 +288,7 @@ func TestUsersGetByID(t *testing.T) {
assert.Equal(403, code, "Get users status should be 403") 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. // 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 { if err != nil {
t.Error("Error occurred while get users", err.Error()) t.Error("Error occurred while get users", err.Error())
t.Log(err) t.Log(err)
@ -320,7 +320,31 @@ func TestUsersPut(t *testing.T) {
} else { } else {
assert.Equal(403, code, "Change user profile status should be 403") 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) code, err = apiTest.UsersPut(testUser0002ID, profile, *testUser0002Auth)
if err != nil { if err != nil {
t.Error("Error occurred while change user profile", err.Error()) t.Error("Error occurred while change user profile", err.Error())
@ -328,7 +352,7 @@ func TestUsersPut(t *testing.T) {
} else { } else {
assert.Equal(400, code, "Change user profile status should be 400") 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.Realname = "test user"
profile.Email = "testUser0003@mydomain.com" profile.Email = "testUser0003@mydomain.com"
profile.Comment = "change profile" profile.Comment = "change profile"
@ -339,7 +363,7 @@ func TestUsersPut(t *testing.T) {
} else { } else {
assert.Equal(409, code, "Change user profile status should be 409") 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.Realname = "test user"
profile.Email = "testUser0002@vmware.com" profile.Email = "testUser0002@vmware.com"
profile.Comment = "change profile" profile.Comment = "change profile"

View File

@ -23,9 +23,8 @@
package apilib package apilib
type UserProfile struct { type UserProfile struct {
Email string `json:"email,omitempty"` UserID int `json:"user_id,omitempty"`
Email string `json:"email,omitempty"`
Realname string `json:"realname,omitempty"` Realname string `json:"realname,omitempty"`
Comment string `json:"comment,omitempty"`
Comment string `json:"comment,omitempty"`
} }