API: Admin EditUser: Make FullName, Email, Website & Location optional (#13562)

* API: Admin EditUser: Make FullName, Email, Website & Location optional

* update swagger docs

* add Tests

Co-authored-by: Lauris BH <lauris@nix.lv>
release/v1.15
6543 2020-11-20 02:56:42 +01:00 committed by GitHub
parent 24b3b2140a
commit 1bb5c09b5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 21 deletions

View File

@ -5,6 +5,7 @@
package integrations package integrations
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"testing" "testing"
@ -163,3 +164,32 @@ func TestAPICreateUserInvalidEmail(t *testing.T) {
}) })
session.MakeRequest(t, req, http.StatusUnprocessableEntity) session.MakeRequest(t, req, http.StatusUnprocessableEntity)
} }
func TestAPIEditUser(t *testing.T) {
defer prepareTestEnv(t)()
adminUsername := "user1"
session := loginUser(t, adminUsername)
token := getTokenForLoggedInUser(t, session)
urlStr := fmt.Sprintf("/api/v1/admin/users/%s?token=%s", "user2", token)
req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
// required
"login_name": "user2",
"source_id": "0",
// to change
"full_name": "Full Name User 2",
})
session.MakeRequest(t, req, http.StatusOK)
empty := ""
req = NewRequestWithJSON(t, "PATCH", urlStr, api.EditUserOption{
LoginName: "user2",
SourceID: 0,
Email: &empty,
})
resp := session.MakeRequest(t, req, http.StatusUnprocessableEntity)
errMap := make(map[string]interface{})
json.Unmarshal(resp.Body.Bytes(), &errMap)
assert.EqualValues(t, "email is not allowed to be empty string", errMap["message"].(string))
}

View File

@ -23,21 +23,22 @@ type CreateUserOption struct {
// EditUserOption edit user options // EditUserOption edit user options
type EditUserOption struct { type EditUserOption struct {
SourceID int64 `json:"source_id"`
LoginName string `json:"login_name"`
FullName string `json:"full_name" binding:"MaxSize(100)"`
// required: true // required: true
SourceID int64 `json:"source_id"`
// required: true
LoginName string `json:"login_name" binding:"Required"`
// swagger:strfmt email // swagger:strfmt email
Email string `json:"email" binding:"Required;Email;MaxSize(254)"` Email *string `json:"email" binding:"MaxSize(254)"`
Password string `json:"password" binding:"MaxSize(255)"` FullName *string `json:"full_name" binding:"MaxSize(100)"`
MustChangePassword *bool `json:"must_change_password"` Password string `json:"password" binding:"MaxSize(255)"`
Website string `json:"website" binding:"MaxSize(50)"` MustChangePassword *bool `json:"must_change_password"`
Location string `json:"location" binding:"MaxSize(50)"` Website *string `json:"website" binding:"MaxSize(50)"`
Active *bool `json:"active"` Location *string `json:"location" binding:"MaxSize(50)"`
Admin *bool `json:"admin"` Active *bool `json:"active"`
AllowGitHook *bool `json:"allow_git_hook"` Admin *bool `json:"admin"`
AllowImportLocal *bool `json:"allow_import_local"` AllowGitHook *bool `json:"allow_git_hook"`
MaxRepoCreation *int `json:"max_repo_creation"` AllowImportLocal *bool `json:"allow_import_local"`
ProhibitLogin *bool `json:"prohibit_login"` MaxRepoCreation *int `json:"max_repo_creation"`
AllowCreateOrganization *bool `json:"allow_create_organization"` ProhibitLogin *bool `json:"prohibit_login"`
AllowCreateOrganization *bool `json:"allow_create_organization"`
} }

View File

@ -155,7 +155,7 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
return return
} }
if len(form.Password) > 0 { if len(form.Password) != 0 {
if !password.IsComplexEnough(form.Password) { if !password.IsComplexEnough(form.Password) {
err := errors.New("PasswordComplexity") err := errors.New("PasswordComplexity")
ctx.Error(http.StatusBadRequest, "PasswordComplexity", err) ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
@ -182,10 +182,23 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
} }
u.LoginName = form.LoginName u.LoginName = form.LoginName
u.FullName = form.FullName
u.Email = form.Email if form.FullName != nil {
u.Website = form.Website u.FullName = *form.FullName
u.Location = form.Location }
if form.Email != nil {
u.Email = *form.Email
if len(u.Email) == 0 {
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("email is not allowed to be empty string"))
return
}
}
if form.Website != nil {
u.Website = *form.Website
}
if form.Location != nil {
u.Location = *form.Location
}
if form.Active != nil { if form.Active != nil {
u.IsActive = *form.Active u.IsActive = *form.Active
} }

View File

@ -13097,7 +13097,8 @@
"description": "EditUserOption edit user options", "description": "EditUserOption edit user options",
"type": "object", "type": "object",
"required": [ "required": [
"email" "source_id",
"login_name"
], ],
"properties": { "properties": {
"active": { "active": {