From 1bab50f00b1c804aa3169fc553ec87dec47f18b7 Mon Sep 17 00:00:00 2001 From: Bruno Sofiato Date: Mon, 27 Jan 2025 14:07:07 -0300 Subject: [PATCH] Return adjustments and integration test --- routers/api/v1/org/org.go | 6 ++++++ tests/integration/api_org_test.go | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index aacefad647..f1c5b0e41a 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/optional" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/web" @@ -343,6 +344,8 @@ func Rename(ctx *context.APIContext) { org := ctx.Org.Organization form := web.GetForm(ctx).(*api.RenameOrgOption) + oldName := org.AsUser().Name + if err := user_service.RenameUser(ctx, org.AsUser(), form.NewName); err != nil { if user_model.IsErrUserAlreadyExist(err) { ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", ctx.Tr("form.username_been_taken")) @@ -353,6 +356,9 @@ func Rename(ctx *context.APIContext) { } else { ctx.ServerError("RenameOrg", err) } + } else { + log.Trace("Org name changed: %s -> %s", oldName, form.NewName) + ctx.Status(http.StatusNoContent) } } diff --git a/tests/integration/api_org_test.go b/tests/integration/api_org_test.go index fff121490c..95419c90cf 100644 --- a/tests/integration/api_org_test.go +++ b/tests/integration/api_org_test.go @@ -226,3 +226,26 @@ func TestAPIOrgSearchEmptyTeam(t *testing.T) { } }) } + +func TestAPIOrgRename(t *testing.T) { + onGiteaRun(t, func(*testing.T, *url.URL) { + token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization) + orgName := "org_to_rename" + newOrgName := "renamed_org" + + // create org + req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &api.CreateOrgOption{ + UserName: orgName, + }).AddTokenAuth(token) + MakeRequest(t, req, http.StatusCreated) + + req = NewRequestWithJSON(t, "POST", "/api/v1/orgs/org_to_rename/rename", &api.RenameOrgOption{ + NewName: newOrgName, + }).AddTokenAuth(token) + MakeRequest(t, req, http.StatusNoContent) + + unittest.AssertExistsAndLoadBean(t, &org_model.Organization{ + Name: newOrgName, + }) + }) +}