Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions integrations/api_admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package integrations

import (
"encoding/json"
"fmt"
"net/http"
"testing"
Expand Down Expand Up @@ -163,3 +164,32 @@ func TestAPICreateUserInvalidEmail(t *testing.T) {
})
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))
}
31 changes: 16 additions & 15 deletions modules/structs/admin_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ type CreateUserOption struct {

// EditUserOption edit user options
type EditUserOption struct {
SourceID int64 `json:"source_id"`
LoginName string `json:"login_name"`
FullName string `json:"full_name" binding:"MaxSize(100)"`
// required: true
SourceID int64 `json:"source_id"`
// required: true
LoginName string `json:"login_name" binding:"Required"`
// swagger:strfmt email
Email string `json:"email" binding:"Required;Email;MaxSize(254)"`
Password string `json:"password" binding:"MaxSize(255)"`
MustChangePassword *bool `json:"must_change_password"`
Website string `json:"website" binding:"MaxSize(50)"`
Location string `json:"location" binding:"MaxSize(50)"`
Active *bool `json:"active"`
Admin *bool `json:"admin"`
AllowGitHook *bool `json:"allow_git_hook"`
AllowImportLocal *bool `json:"allow_import_local"`
MaxRepoCreation *int `json:"max_repo_creation"`
ProhibitLogin *bool `json:"prohibit_login"`
AllowCreateOrganization *bool `json:"allow_create_organization"`
Email *string `json:"email" binding:"MaxSize(254)"`
FullName *string `json:"full_name" binding:"MaxSize(100)"`
Password string `json:"password" binding:"MaxSize(255)"`
MustChangePassword *bool `json:"must_change_password"`
Website *string `json:"website" binding:"MaxSize(50)"`
Location *string `json:"location" binding:"MaxSize(50)"`
Active *bool `json:"active"`
Admin *bool `json:"admin"`
AllowGitHook *bool `json:"allow_git_hook"`
AllowImportLocal *bool `json:"allow_import_local"`
MaxRepoCreation *int `json:"max_repo_creation"`
ProhibitLogin *bool `json:"prohibit_login"`
AllowCreateOrganization *bool `json:"allow_create_organization"`
}
23 changes: 18 additions & 5 deletions routers/api/v1/admin/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
return
}

if len(form.Password) > 0 {
if len(form.Password) != 0 {
if !password.IsComplexEnough(form.Password) {
err := errors.New("PasswordComplexity")
ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
Expand All @@ -182,10 +182,23 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
}

u.LoginName = form.LoginName
u.FullName = form.FullName
u.Email = form.Email
u.Website = form.Website
u.Location = form.Location

if form.FullName != nil {
u.FullName = *form.FullName
}
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 {
u.IsActive = *form.Active
}
Expand Down
3 changes: 2 additions & 1 deletion templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13097,7 +13097,8 @@
"description": "EditUserOption edit user options",
"type": "object",
"required": [
"email"
"source_id",
"login_name"
],
"properties": {
"active": {
Expand Down