mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-03 20:36:07 +01:00 
			
		
		
		
	Inclusion of rename organization api (#33303)
This adds an endpoint (`/orgs/{org}/rename`) to rename organizations.
I've modeled the endpoint using the rename user endpoint --
`/admin/users/{username}/rename` -- as base.
It is the 1st time I wrote a new API endpoint (I've tried to follow the
rename users endpoint code while writing it). So feel free to ping me if
there is something wrong or missing.
Resolves #32995
---------
Signed-off-by: Bruno Sofiato <bruno.sofiato@gmail.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
			
			
This commit is contained in:
		@@ -57,3 +57,12 @@ type EditOrgOption struct {
 | 
				
			|||||||
	Visibility                string `json:"visibility" binding:"In(,public,limited,private)"`
 | 
						Visibility                string `json:"visibility" binding:"In(,public,limited,private)"`
 | 
				
			||||||
	RepoAdminChangeTeamAccess *bool  `json:"repo_admin_change_team_access"`
 | 
						RepoAdminChangeTeamAccess *bool  `json:"repo_admin_change_team_access"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// RenameOrgOption options when renaming an organization
 | 
				
			||||||
 | 
					type RenameOrgOption struct {
 | 
				
			||||||
 | 
						// New username for this org. This name cannot be in use yet by any other user.
 | 
				
			||||||
 | 
						//
 | 
				
			||||||
 | 
						// required: true
 | 
				
			||||||
 | 
						// unique: true
 | 
				
			||||||
 | 
						NewName string `json:"new_name" binding:"Required"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -477,26 +477,16 @@ func RenameUser(ctx *context.APIContext) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	oldName := ctx.ContextUser.Name
 | 
					 | 
				
			||||||
	newName := web.GetForm(ctx).(*api.RenameUserOption).NewName
 | 
						newName := web.GetForm(ctx).(*api.RenameUserOption).NewName
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Check if username has been changed
 | 
						// Check if username has been changed
 | 
				
			||||||
	if err := user_service.RenameUser(ctx, ctx.ContextUser, newName); err != nil {
 | 
						if err := user_service.RenameUser(ctx, ctx.ContextUser, newName); err != nil {
 | 
				
			||||||
		switch {
 | 
							if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) || db.IsErrNameCharsNotAllowed(err) {
 | 
				
			||||||
		case user_model.IsErrUserAlreadyExist(err):
 | 
								ctx.Error(http.StatusUnprocessableEntity, "", err)
 | 
				
			||||||
			ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("form.username_been_taken"))
 | 
							} else {
 | 
				
			||||||
		case db.IsErrNameReserved(err):
 | 
					 | 
				
			||||||
			ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_reserved", newName))
 | 
					 | 
				
			||||||
		case db.IsErrNamePatternNotAllowed(err):
 | 
					 | 
				
			||||||
			ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_pattern_not_allowed", newName))
 | 
					 | 
				
			||||||
		case db.IsErrNameCharsNotAllowed(err):
 | 
					 | 
				
			||||||
			ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_chars_not_allowed", newName))
 | 
					 | 
				
			||||||
		default:
 | 
					 | 
				
			||||||
			ctx.ServerError("ChangeUserName", err)
 | 
								ctx.ServerError("ChangeUserName", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	log.Trace("User name changed: %s -> %s", oldName, newName)
 | 
					 | 
				
			||||||
	ctx.Status(http.StatusNoContent)
 | 
						ctx.Status(http.StatusNoContent)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1530,6 +1530,7 @@ func Routes() *web.Router {
 | 
				
			|||||||
			m.Combo("").Get(org.Get).
 | 
								m.Combo("").Get(org.Get).
 | 
				
			||||||
				Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
 | 
									Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
 | 
				
			||||||
				Delete(reqToken(), reqOrgOwnership(), org.Delete)
 | 
									Delete(reqToken(), reqOrgOwnership(), org.Delete)
 | 
				
			||||||
 | 
								m.Post("/rename", reqToken(), reqOrgOwnership(), bind(api.RenameOrgOption{}), org.Rename)
 | 
				
			||||||
			m.Combo("/repos").Get(user.ListOrgRepos).
 | 
								m.Combo("/repos").Get(user.ListOrgRepos).
 | 
				
			||||||
				Post(reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
 | 
									Post(reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
 | 
				
			||||||
			m.Group("/members", func() {
 | 
								m.Group("/members", func() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -315,6 +315,44 @@ func Get(ctx *context.APIContext) {
 | 
				
			|||||||
	ctx.JSON(http.StatusOK, org)
 | 
						ctx.JSON(http.StatusOK, org)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func Rename(ctx *context.APIContext) {
 | 
				
			||||||
 | 
						// swagger:operation POST /orgs/{org}/rename organization renameOrg
 | 
				
			||||||
 | 
						// ---
 | 
				
			||||||
 | 
						// summary: Rename an organization
 | 
				
			||||||
 | 
						// produces:
 | 
				
			||||||
 | 
						// - application/json
 | 
				
			||||||
 | 
						// parameters:
 | 
				
			||||||
 | 
						// - name: org
 | 
				
			||||||
 | 
						//   in: path
 | 
				
			||||||
 | 
						//   description: existing org name
 | 
				
			||||||
 | 
						//   type: string
 | 
				
			||||||
 | 
						//   required: true
 | 
				
			||||||
 | 
						// - name: body
 | 
				
			||||||
 | 
						//   in: body
 | 
				
			||||||
 | 
						//   required: true
 | 
				
			||||||
 | 
						//   schema:
 | 
				
			||||||
 | 
						//     "$ref": "#/definitions/RenameOrgOption"
 | 
				
			||||||
 | 
						// responses:
 | 
				
			||||||
 | 
						//   "204":
 | 
				
			||||||
 | 
						//     "$ref": "#/responses/empty"
 | 
				
			||||||
 | 
						//   "403":
 | 
				
			||||||
 | 
						//     "$ref": "#/responses/forbidden"
 | 
				
			||||||
 | 
						//   "422":
 | 
				
			||||||
 | 
						//     "$ref": "#/responses/validationError"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						form := web.GetForm(ctx).(*api.RenameOrgOption)
 | 
				
			||||||
 | 
						orgUser := ctx.Org.Organization.AsUser()
 | 
				
			||||||
 | 
						if err := user_service.RenameUser(ctx, orgUser, form.NewName); err != nil {
 | 
				
			||||||
 | 
							if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) || db.IsErrNameCharsNotAllowed(err) {
 | 
				
			||||||
 | 
								ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", err)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								ctx.ServerError("RenameOrg", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						ctx.Status(http.StatusNoContent)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Edit change an organization's information
 | 
					// Edit change an organization's information
 | 
				
			||||||
func Edit(ctx *context.APIContext) {
 | 
					func Edit(ctx *context.APIContext) {
 | 
				
			||||||
	// swagger:operation PATCH /orgs/{org} organization orgEdit
 | 
						// swagger:operation PATCH /orgs/{org} organization orgEdit
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -208,6 +208,9 @@ type swaggerParameterBodies struct {
 | 
				
			|||||||
	// in:body
 | 
						// in:body
 | 
				
			||||||
	CreateVariableOption api.CreateVariableOption
 | 
						CreateVariableOption api.CreateVariableOption
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// in:body
 | 
				
			||||||
 | 
						RenameOrgOption api.RenameOrgOption
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// in:body
 | 
						// in:body
 | 
				
			||||||
	UpdateVariableOption api.UpdateVariableOption
 | 
						UpdateVariableOption api.UpdateVariableOption
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										56
									
								
								templates/swagger/v1_json.tmpl
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										56
									
								
								templates/swagger/v1_json.tmpl
									
									
									
										generated
									
									
									
								
							@@ -2991,6 +2991,46 @@
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					    "/orgs/{org}/rename": {
 | 
				
			||||||
 | 
					      "post": {
 | 
				
			||||||
 | 
					        "produces": [
 | 
				
			||||||
 | 
					          "application/json"
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					        "tags": [
 | 
				
			||||||
 | 
					          "organization"
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					        "summary": "Rename an organization",
 | 
				
			||||||
 | 
					        "operationId": "renameOrg",
 | 
				
			||||||
 | 
					        "parameters": [
 | 
				
			||||||
 | 
					          {
 | 
				
			||||||
 | 
					            "type": "string",
 | 
				
			||||||
 | 
					            "description": "existing org name",
 | 
				
			||||||
 | 
					            "name": "org",
 | 
				
			||||||
 | 
					            "in": "path",
 | 
				
			||||||
 | 
					            "required": true
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
 | 
					          {
 | 
				
			||||||
 | 
					            "name": "body",
 | 
				
			||||||
 | 
					            "in": "body",
 | 
				
			||||||
 | 
					            "required": true,
 | 
				
			||||||
 | 
					            "schema": {
 | 
				
			||||||
 | 
					              "$ref": "#/definitions/RenameOrgOption"
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					        "responses": {
 | 
				
			||||||
 | 
					          "204": {
 | 
				
			||||||
 | 
					            "$ref": "#/responses/empty"
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
 | 
					          "403": {
 | 
				
			||||||
 | 
					            "$ref": "#/responses/forbidden"
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
 | 
					          "422": {
 | 
				
			||||||
 | 
					            "$ref": "#/responses/validationError"
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
    "/orgs/{org}/repos": {
 | 
					    "/orgs/{org}/repos": {
 | 
				
			||||||
      "get": {
 | 
					      "get": {
 | 
				
			||||||
        "produces": [
 | 
					        "produces": [
 | 
				
			||||||
@@ -24207,6 +24247,22 @@
 | 
				
			|||||||
      },
 | 
					      },
 | 
				
			||||||
      "x-go-package": "code.gitea.io/gitea/modules/structs"
 | 
					      "x-go-package": "code.gitea.io/gitea/modules/structs"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					    "RenameOrgOption": {
 | 
				
			||||||
 | 
					      "description": "RenameOrgOption options when renaming an organization",
 | 
				
			||||||
 | 
					      "type": "object",
 | 
				
			||||||
 | 
					      "required": [
 | 
				
			||||||
 | 
					        "new_name"
 | 
				
			||||||
 | 
					      ],
 | 
				
			||||||
 | 
					      "properties": {
 | 
				
			||||||
 | 
					        "new_name": {
 | 
				
			||||||
 | 
					          "description": "New username for this org. This name cannot be in use yet by any other user.",
 | 
				
			||||||
 | 
					          "type": "string",
 | 
				
			||||||
 | 
					          "uniqueItems": true,
 | 
				
			||||||
 | 
					          "x-go-name": "NewName"
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      "x-go-package": "code.gitea.io/gitea/modules/structs"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
    "RenameUserOption": {
 | 
					    "RenameUserOption": {
 | 
				
			||||||
      "description": "RenameUserOption options when renaming a user",
 | 
					      "description": "RenameUserOption options when renaming a user",
 | 
				
			||||||
      "type": "object",
 | 
					      "type": "object",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,7 +6,6 @@ package integration
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"net/url"
 | 
					 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -19,13 +18,14 @@ import (
 | 
				
			|||||||
	user_model "code.gitea.io/gitea/models/user"
 | 
						user_model "code.gitea.io/gitea/models/user"
 | 
				
			||||||
	"code.gitea.io/gitea/modules/setting"
 | 
						"code.gitea.io/gitea/modules/setting"
 | 
				
			||||||
	api "code.gitea.io/gitea/modules/structs"
 | 
						api "code.gitea.io/gitea/modules/structs"
 | 
				
			||||||
 | 
						"code.gitea.io/gitea/modules/test"
 | 
				
			||||||
	"code.gitea.io/gitea/tests"
 | 
						"code.gitea.io/gitea/tests"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestAPIOrgCreate(t *testing.T) {
 | 
					func TestAPIOrgCreateRename(t *testing.T) {
 | 
				
			||||||
	onGiteaRun(t, func(*testing.T, *url.URL) {
 | 
						defer tests.PrepareTestEnv(t)()
 | 
				
			||||||
	token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization)
 | 
						token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	org := api.CreateOrgOption{
 | 
						org := api.CreateOrgOption{
 | 
				
			||||||
@@ -36,8 +36,7 @@ func TestAPIOrgCreate(t *testing.T) {
 | 
				
			|||||||
		Location:    "Shanghai",
 | 
							Location:    "Shanghai",
 | 
				
			||||||
		Visibility:  "limited",
 | 
							Visibility:  "limited",
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
		req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &org).
 | 
						req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &org).AddTokenAuth(token)
 | 
				
			||||||
			AddTokenAuth(token)
 | 
					 | 
				
			||||||
	resp := MakeRequest(t, req, http.StatusCreated)
 | 
						resp := MakeRequest(t, req, http.StatusCreated)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var apiOrg api.Organization
 | 
						var apiOrg api.Organization
 | 
				
			||||||
@@ -56,9 +55,15 @@ func TestAPIOrgCreate(t *testing.T) {
 | 
				
			|||||||
		FullName:  org.FullName,
 | 
							FullName:  org.FullName,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// check org name
 | 
				
			||||||
 | 
						req = NewRequestf(t, "GET", "/api/v1/orgs/%s", org.UserName).AddTokenAuth(token)
 | 
				
			||||||
 | 
						resp = MakeRequest(t, req, http.StatusOK)
 | 
				
			||||||
 | 
						DecodeJSON(t, resp, &apiOrg)
 | 
				
			||||||
 | 
						assert.EqualValues(t, org.UserName, apiOrg.Name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("CheckPermission", func(t *testing.T) {
 | 
				
			||||||
		// Check owner team permission
 | 
							// Check owner team permission
 | 
				
			||||||
		ownerTeam, _ := org_model.GetOwnerTeam(db.DefaultContext, apiOrg.ID)
 | 
							ownerTeam, _ := org_model.GetOwnerTeam(db.DefaultContext, apiOrg.ID)
 | 
				
			||||||
 | 
					 | 
				
			||||||
		for _, ut := range unit_model.AllRepoUnitTypes {
 | 
							for _, ut := range unit_model.AllRepoUnitTypes {
 | 
				
			||||||
			up := perm.AccessModeOwner
 | 
								up := perm.AccessModeOwner
 | 
				
			||||||
			if ut == unit_model.TypeExternalTracker || ut == unit_model.TypeExternalWiki {
 | 
								if ut == unit_model.TypeExternalTracker || ut == unit_model.TypeExternalWiki {
 | 
				
			||||||
@@ -71,25 +76,10 @@ func TestAPIOrgCreate(t *testing.T) {
 | 
				
			|||||||
				AccessMode: up,
 | 
									AccessMode: up,
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		req = NewRequestf(t, "GET", "/api/v1/orgs/%s", org.UserName).
 | 
						t.Run("CheckMembers", func(t *testing.T) {
 | 
				
			||||||
			AddTokenAuth(token)
 | 
							req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", org.UserName).AddTokenAuth(token)
 | 
				
			||||||
		resp = MakeRequest(t, req, http.StatusOK)
 | 
					 | 
				
			||||||
		DecodeJSON(t, resp, &apiOrg)
 | 
					 | 
				
			||||||
		assert.EqualValues(t, org.UserName, apiOrg.Name)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", org.UserName).
 | 
					 | 
				
			||||||
			AddTokenAuth(token)
 | 
					 | 
				
			||||||
		resp = MakeRequest(t, req, http.StatusOK)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		var repos []*api.Repository
 | 
					 | 
				
			||||||
		DecodeJSON(t, resp, &repos)
 | 
					 | 
				
			||||||
		for _, repo := range repos {
 | 
					 | 
				
			||||||
			assert.False(t, repo.Private)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", org.UserName).
 | 
					 | 
				
			||||||
			AddTokenAuth(token)
 | 
					 | 
				
			||||||
		resp = MakeRequest(t, req, http.StatusOK)
 | 
							resp = MakeRequest(t, req, http.StatusOK)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// user1 on this org is public
 | 
							// user1 on this org is public
 | 
				
			||||||
@@ -98,10 +88,30 @@ func TestAPIOrgCreate(t *testing.T) {
 | 
				
			|||||||
		assert.Len(t, users, 1)
 | 
							assert.Len(t, users, 1)
 | 
				
			||||||
		assert.EqualValues(t, "user1", users[0].UserName)
 | 
							assert.EqualValues(t, "user1", users[0].UserName)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("RenameOrg", func(t *testing.T) {
 | 
				
			||||||
 | 
							req = NewRequestWithJSON(t, "POST", "/api/v1/orgs/user1_org/rename", &api.RenameOrgOption{
 | 
				
			||||||
 | 
								NewName: "renamed_org",
 | 
				
			||||||
 | 
							}).AddTokenAuth(token)
 | 
				
			||||||
 | 
							MakeRequest(t, req, http.StatusNoContent)
 | 
				
			||||||
 | 
							unittest.AssertExistsAndLoadBean(t, &org_model.Organization{Name: "renamed_org"})
 | 
				
			||||||
 | 
							org.UserName = "renamed_org" // update the variable so the following tests could still use it
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("ListRepos", func(t *testing.T) {
 | 
				
			||||||
 | 
							// FIXME: this test is wrong, there is no repository at all, so the for-loop is empty
 | 
				
			||||||
 | 
							req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", org.UserName).AddTokenAuth(token)
 | 
				
			||||||
 | 
							resp = MakeRequest(t, req, http.StatusOK)
 | 
				
			||||||
 | 
							var repos []*api.Repository
 | 
				
			||||||
 | 
							DecodeJSON(t, resp, &repos)
 | 
				
			||||||
 | 
							for _, repo := range repos {
 | 
				
			||||||
 | 
								assert.False(t, repo.Private)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestAPIOrgEdit(t *testing.T) {
 | 
					func TestAPIOrgEdit(t *testing.T) {
 | 
				
			||||||
	onGiteaRun(t, func(*testing.T, *url.URL) {
 | 
						defer tests.PrepareTestEnv(t)()
 | 
				
			||||||
	session := loginUser(t, "user1")
 | 
						session := loginUser(t, "user1")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization)
 | 
						token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization)
 | 
				
			||||||
@@ -125,11 +135,10 @@ func TestAPIOrgEdit(t *testing.T) {
 | 
				
			|||||||
	assert.Equal(t, org.Website, apiOrg.Website)
 | 
						assert.Equal(t, org.Website, apiOrg.Website)
 | 
				
			||||||
	assert.Equal(t, org.Location, apiOrg.Location)
 | 
						assert.Equal(t, org.Location, apiOrg.Location)
 | 
				
			||||||
	assert.Equal(t, org.Visibility, apiOrg.Visibility)
 | 
						assert.Equal(t, org.Visibility, apiOrg.Visibility)
 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestAPIOrgEditBadVisibility(t *testing.T) {
 | 
					func TestAPIOrgEditBadVisibility(t *testing.T) {
 | 
				
			||||||
	onGiteaRun(t, func(*testing.T, *url.URL) {
 | 
						defer tests.PrepareTestEnv(t)()
 | 
				
			||||||
	session := loginUser(t, "user1")
 | 
						session := loginUser(t, "user1")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization)
 | 
						token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteOrganization)
 | 
				
			||||||
@@ -143,15 +152,11 @@ func TestAPIOrgEditBadVisibility(t *testing.T) {
 | 
				
			|||||||
	req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org).
 | 
						req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/org3", &org).
 | 
				
			||||||
		AddTokenAuth(token)
 | 
							AddTokenAuth(token)
 | 
				
			||||||
	MakeRequest(t, req, http.StatusUnprocessableEntity)
 | 
						MakeRequest(t, req, http.StatusUnprocessableEntity)
 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestAPIOrgDeny(t *testing.T) {
 | 
					func TestAPIOrgDeny(t *testing.T) {
 | 
				
			||||||
	onGiteaRun(t, func(*testing.T, *url.URL) {
 | 
						defer tests.PrepareTestEnv(t)()
 | 
				
			||||||
		setting.Service.RequireSignInView = true
 | 
						defer test.MockVariableValue(&setting.Service.RequireSignInView, true)()
 | 
				
			||||||
		defer func() {
 | 
					 | 
				
			||||||
			setting.Service.RequireSignInView = false
 | 
					 | 
				
			||||||
		}()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	orgName := "user1_org"
 | 
						orgName := "user1_org"
 | 
				
			||||||
	req := NewRequestf(t, "GET", "/api/v1/orgs/%s", orgName)
 | 
						req := NewRequestf(t, "GET", "/api/v1/orgs/%s", orgName)
 | 
				
			||||||
@@ -162,12 +167,10 @@ func TestAPIOrgDeny(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", orgName)
 | 
						req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", orgName)
 | 
				
			||||||
	MakeRequest(t, req, http.StatusNotFound)
 | 
						MakeRequest(t, req, http.StatusNotFound)
 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestAPIGetAll(t *testing.T) {
 | 
					func TestAPIGetAll(t *testing.T) {
 | 
				
			||||||
	defer tests.PrepareTestEnv(t)()
 | 
						defer tests.PrepareTestEnv(t)()
 | 
				
			||||||
 | 
					 | 
				
			||||||
	token := getUserToken(t, "user1", auth_model.AccessTokenScopeReadOrganization)
 | 
						token := getUserToken(t, "user1", auth_model.AccessTokenScopeReadOrganization)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// accessing with a token will return all orgs
 | 
						// accessing with a token will return all orgs
 | 
				
			||||||
@@ -192,7 +195,7 @@ func TestAPIGetAll(t *testing.T) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestAPIOrgSearchEmptyTeam(t *testing.T) {
 | 
					func TestAPIOrgSearchEmptyTeam(t *testing.T) {
 | 
				
			||||||
	onGiteaRun(t, func(*testing.T, *url.URL) {
 | 
						defer tests.PrepareTestEnv(t)()
 | 
				
			||||||
	token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization)
 | 
						token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization)
 | 
				
			||||||
	orgName := "org_with_empty_team"
 | 
						orgName := "org_with_empty_team"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -224,5 +227,4 @@ func TestAPIOrgSearchEmptyTeam(t *testing.T) {
 | 
				
			|||||||
	if assert.Len(t, data.Data, 1) {
 | 
						if assert.Len(t, data.Data, 1) {
 | 
				
			||||||
		assert.EqualValues(t, "Empty", data.Data[0].Name)
 | 
							assert.EqualValues(t, "Empty", data.Data[0].Name)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user