Skip to content

Commit 1bab50f

Browse files
committed
Return adjustments and integration test
1 parent ff24828 commit 1bab50f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

routers/api/v1/org/org.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models/organization"
1313
"code.gitea.io/gitea/models/perm"
1414
user_model "code.gitea.io/gitea/models/user"
15+
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/optional"
1617
api "code.gitea.io/gitea/modules/structs"
1718
"code.gitea.io/gitea/modules/web"
@@ -343,6 +344,8 @@ func Rename(ctx *context.APIContext) {
343344
org := ctx.Org.Organization
344345
form := web.GetForm(ctx).(*api.RenameOrgOption)
345346

347+
oldName := org.AsUser().Name
348+
346349
if err := user_service.RenameUser(ctx, org.AsUser(), form.NewName); err != nil {
347350
if user_model.IsErrUserAlreadyExist(err) {
348351
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", ctx.Tr("form.username_been_taken"))
@@ -353,6 +356,9 @@ func Rename(ctx *context.APIContext) {
353356
} else {
354357
ctx.ServerError("RenameOrg", err)
355358
}
359+
} else {
360+
log.Trace("Org name changed: %s -> %s", oldName, form.NewName)
361+
ctx.Status(http.StatusNoContent)
356362
}
357363
}
358364

tests/integration/api_org_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,26 @@ func TestAPIOrgSearchEmptyTeam(t *testing.T) {
226226
}
227227
})
228228
}
229+
230+
func TestAPIOrgRename(t *testing.T) {
231+
onGiteaRun(t, func(*testing.T, *url.URL) {
232+
token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization)
233+
orgName := "org_to_rename"
234+
newOrgName := "renamed_org"
235+
236+
// create org
237+
req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &api.CreateOrgOption{
238+
UserName: orgName,
239+
}).AddTokenAuth(token)
240+
MakeRequest(t, req, http.StatusCreated)
241+
242+
req = NewRequestWithJSON(t, "POST", "/api/v1/orgs/org_to_rename/rename", &api.RenameOrgOption{
243+
NewName: newOrgName,
244+
}).AddTokenAuth(token)
245+
MakeRequest(t, req, http.StatusNoContent)
246+
247+
unittest.AssertExistsAndLoadBean(t, &org_model.Organization{
248+
Name: newOrgName,
249+
})
250+
})
251+
}

0 commit comments

Comments
 (0)