Skip to content

Commit 1bb5c09

Browse files
6543lafriks
andauthored
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 <[email protected]>
1 parent 24b3b21 commit 1bb5c09

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

integrations/api_admin_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package integrations
66

77
import (
8+
"encoding/json"
89
"fmt"
910
"net/http"
1011
"testing"
@@ -163,3 +164,32 @@ func TestAPICreateUserInvalidEmail(t *testing.T) {
163164
})
164165
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
165166
}
167+
168+
func TestAPIEditUser(t *testing.T) {
169+
defer prepareTestEnv(t)()
170+
adminUsername := "user1"
171+
session := loginUser(t, adminUsername)
172+
token := getTokenForLoggedInUser(t, session)
173+
urlStr := fmt.Sprintf("/api/v1/admin/users/%s?token=%s", "user2", token)
174+
175+
req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
176+
// required
177+
"login_name": "user2",
178+
"source_id": "0",
179+
// to change
180+
"full_name": "Full Name User 2",
181+
})
182+
session.MakeRequest(t, req, http.StatusOK)
183+
184+
empty := ""
185+
req = NewRequestWithJSON(t, "PATCH", urlStr, api.EditUserOption{
186+
LoginName: "user2",
187+
SourceID: 0,
188+
Email: &empty,
189+
})
190+
resp := session.MakeRequest(t, req, http.StatusUnprocessableEntity)
191+
192+
errMap := make(map[string]interface{})
193+
json.Unmarshal(resp.Body.Bytes(), &errMap)
194+
assert.EqualValues(t, "email is not allowed to be empty string", errMap["message"].(string))
195+
}

modules/structs/admin_user.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@ type CreateUserOption struct {
2323

2424
// EditUserOption edit user options
2525
type EditUserOption struct {
26-
SourceID int64 `json:"source_id"`
27-
LoginName string `json:"login_name"`
28-
FullName string `json:"full_name" binding:"MaxSize(100)"`
2926
// required: true
27+
SourceID int64 `json:"source_id"`
28+
// required: true
29+
LoginName string `json:"login_name" binding:"Required"`
3030
// swagger:strfmt email
31-
Email string `json:"email" binding:"Required;Email;MaxSize(254)"`
32-
Password string `json:"password" binding:"MaxSize(255)"`
33-
MustChangePassword *bool `json:"must_change_password"`
34-
Website string `json:"website" binding:"MaxSize(50)"`
35-
Location string `json:"location" binding:"MaxSize(50)"`
36-
Active *bool `json:"active"`
37-
Admin *bool `json:"admin"`
38-
AllowGitHook *bool `json:"allow_git_hook"`
39-
AllowImportLocal *bool `json:"allow_import_local"`
40-
MaxRepoCreation *int `json:"max_repo_creation"`
41-
ProhibitLogin *bool `json:"prohibit_login"`
42-
AllowCreateOrganization *bool `json:"allow_create_organization"`
31+
Email *string `json:"email" binding:"MaxSize(254)"`
32+
FullName *string `json:"full_name" binding:"MaxSize(100)"`
33+
Password string `json:"password" binding:"MaxSize(255)"`
34+
MustChangePassword *bool `json:"must_change_password"`
35+
Website *string `json:"website" binding:"MaxSize(50)"`
36+
Location *string `json:"location" binding:"MaxSize(50)"`
37+
Active *bool `json:"active"`
38+
Admin *bool `json:"admin"`
39+
AllowGitHook *bool `json:"allow_git_hook"`
40+
AllowImportLocal *bool `json:"allow_import_local"`
41+
MaxRepoCreation *int `json:"max_repo_creation"`
42+
ProhibitLogin *bool `json:"prohibit_login"`
43+
AllowCreateOrganization *bool `json:"allow_create_organization"`
4344
}

routers/api/v1/admin/user.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
155155
return
156156
}
157157

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

184184
u.LoginName = form.LoginName
185-
u.FullName = form.FullName
186-
u.Email = form.Email
187-
u.Website = form.Website
188-
u.Location = form.Location
185+
186+
if form.FullName != nil {
187+
u.FullName = *form.FullName
188+
}
189+
if form.Email != nil {
190+
u.Email = *form.Email
191+
if len(u.Email) == 0 {
192+
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("email is not allowed to be empty string"))
193+
return
194+
}
195+
}
196+
if form.Website != nil {
197+
u.Website = *form.Website
198+
}
199+
if form.Location != nil {
200+
u.Location = *form.Location
201+
}
189202
if form.Active != nil {
190203
u.IsActive = *form.Active
191204
}

templates/swagger/v1_json.tmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13097,7 +13097,8 @@
1309713097
"description": "EditUserOption edit user options",
1309813098
"type": "object",
1309913099
"required": [
13100-
"email"
13100+
"source_id",
13101+
"login_name"
1310113102
],
1310213103
"properties": {
1310313104
"active": {

0 commit comments

Comments
 (0)