Skip to content
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

Fixes #7023 - API Org Visibility #7028

Merged
merged 16 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
86 changes: 86 additions & 0 deletions integrations/api_admin_org_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package integrations

import (
"net/http"
"net/url"
"strings"
"testing"

"code.gitea.io/gitea/models"
api "code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
)

func TestAPIAdminOrgCreate(t *testing.T) {
onGiteaRun(t, func(*testing.T, *url.URL) {
session := loginUser(t, "user1")
token := getTokenForLoggedInUser(t, session)

var org = api.CreateOrgOption{
UserName: "user2_org",
FullName: "User2's organization",
Description: "This organization created by admin for user2",
Website: "https://try.gitea.io",
Location: "Shanghai",
Visibility: "private",
}
req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs?token="+token, &org)
resp := session.MakeRequest(t, req, http.StatusCreated)

var apiOrg api.Organization
DecodeJSON(t, resp, &apiOrg)

assert.Equal(t, org.UserName, apiOrg.UserName)
assert.Equal(t, org.FullName, apiOrg.FullName)
assert.Equal(t, org.Description, apiOrg.Description)
assert.Equal(t, org.Website, apiOrg.Website)
assert.Equal(t, org.Location, apiOrg.Location)
assert.Equal(t, org.Visibility, apiOrg.Visibility)

models.AssertExistsAndLoadBean(t, &models.User{
Name: org.UserName,
LowerName: strings.ToLower(org.UserName),
FullName: org.FullName,
})
})
}

func TestAPIAdminOrgCreateBadVisibility(t *testing.T) {
onGiteaRun(t, func(*testing.T, *url.URL) {
session := loginUser(t, "user1")
token := getTokenForLoggedInUser(t, session)

var org = api.CreateOrgOption{
UserName: "user2_org",
FullName: "User2's organization",
Description: "This organization created by admin for user2",
Website: "https://try.gitea.io",
Location: "Shanghai",
Visibility: "notvalid",
}
req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs?token="+token, &org)
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
})
}

func TestAPIAdminOrgCreateNotAdmin(t *testing.T) {
prepareTestEnv(t)
nonAdminUsername := "user2"
session := loginUser(t, nonAdminUsername)
token := getTokenForLoggedInUser(t, session)
var org = api.CreateOrgOption{
UserName: "user2_org",
FullName: "User2's organization",
Description: "This organization created by admin for user2",
Website: "https://try.gitea.io",
Location: "Shanghai",
Visibility: "public",
}
req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs?token="+token, &org)
session.MakeRequest(t, req, http.StatusForbidden)
}
48 changes: 47 additions & 1 deletion integrations/api_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestAPIOrg(t *testing.T) {
func TestAPIOrgCreate(t *testing.T) {
onGiteaRun(t, func(*testing.T, *url.URL) {
session := loginUser(t, "user1")

Expand All @@ -28,6 +28,7 @@ func TestAPIOrg(t *testing.T) {
Description: "This organization created by user1",
Website: "https://try.gitea.io",
Location: "Shanghai",
Visibility: "limited",
}
req := NewRequestWithJSON(t, "POST", "/api/v1/orgs?token="+token, &org)
resp := session.MakeRequest(t, req, http.StatusCreated)
Expand All @@ -40,6 +41,7 @@ func TestAPIOrg(t *testing.T) {
assert.Equal(t, org.Description, apiOrg.Description)
assert.Equal(t, org.Website, apiOrg.Website)
assert.Equal(t, org.Location, apiOrg.Location)
assert.Equal(t, org.Visibility, apiOrg.Visibility)

models.AssertExistsAndLoadBean(t, &models.User{
Name: org.UserName,
Expand Down Expand Up @@ -72,6 +74,50 @@ func TestAPIOrg(t *testing.T) {
})
}

func TestAPIOrgEdit(t *testing.T) {
onGiteaRun(t, func(*testing.T, *url.URL) {
session := loginUser(t, "user1")

token := getTokenForLoggedInUser(t, session)
var org = api.EditOrgOption{
FullName: "User3 organization new full name",
Description: "A new description",
Website: "https://try.gitea.io/new",
Location: "Beijing",
Visibility: "private",
}
req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/user3?token="+token, &org)
resp := session.MakeRequest(t, req, http.StatusOK)

var apiOrg api.Organization
DecodeJSON(t, resp, &apiOrg)

assert.Equal(t, "user3", apiOrg.UserName)
assert.Equal(t, org.FullName, apiOrg.FullName)
assert.Equal(t, org.Description, apiOrg.Description)
assert.Equal(t, org.Website, apiOrg.Website)
assert.Equal(t, org.Location, apiOrg.Location)
assert.Equal(t, org.Visibility, apiOrg.Visibility)
})
}

func TestAPIOrgEditBadVisibility(t *testing.T) {
onGiteaRun(t, func(*testing.T, *url.URL) {
session := loginUser(t, "user1")

token := getTokenForLoggedInUser(t, session)
var org = api.EditOrgOption{
FullName: "User3 organization new full name",
Description: "A new description",
Website: "https://try.gitea.io/new",
Location: "Beijing",
Visibility: "badvisibility",
}
req := NewRequestWithJSON(t, "PATCH", "/api/v1/orgs/user3?token="+token, &org)
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
})
}

func TestAPIOrgDeny(t *testing.T) {
onGiteaRun(t, func(*testing.T, *url.URL) {
setting.Service.RequireSignInView = true
Expand Down
2 changes: 2 additions & 0 deletions integrations/api_user_orgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestUserOrgs(t *testing.T) {
Description: "",
Website: "",
Location: "",
Visibility: "public",
},
}, orgs)
}
Expand All @@ -63,6 +64,7 @@ func TestMyOrgs(t *testing.T) {
Description: "",
Website: "",
Location: "",
Visibility: "public",
},
}, orgs)
}
31 changes: 17 additions & 14 deletions modules/structs/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ package structs

// Organization represents an organization
type Organization struct {
ID int64 `json:"id"`
UserName string `json:"username"`
FullName string `json:"full_name"`
AvatarURL string `json:"avatar_url"`
Description string `json:"description"`
Website string `json:"website"`
Location string `json:"location"`
Visibility VisibleType `json:"visibility"`
ID int64 `json:"id"`
UserName string `json:"username"`
FullName string `json:"full_name"`
AvatarURL string `json:"avatar_url"`
Description string `json:"description"`
Website string `json:"website"`
Location string `json:"location"`
Visibility string `json:"visibility"`
zeripath marked this conversation as resolved.
Show resolved Hide resolved
}

// CreateOrgOption options for creating an organization
type CreateOrgOption struct {
// required: true
UserName string `json:"username" binding:"Required"`
FullName string `json:"full_name"`
Description string `json:"description"`
Website string `json:"website"`
Location string `json:"location"`
Visibility VisibleType `json:"visibility"`
UserName string `json:"username" binding:"Required"`
FullName string `json:"full_name"`
Description string `json:"description"`
Website string `json:"website"`
Location string `json:"location"`
// enum: public,limited,private
Visibility string `json:"visibility" binding:"In(,public,limited,private)"`
}

// EditOrgOption options for editing an organization
Expand All @@ -33,4 +34,6 @@ type EditOrgOption struct {
Description string `json:"description"`
Website string `json:"website"`
Location string `json:"location"`
// enum: public,limited,private
Visibility string `json:"visibility" binding:"In(,public,limited,private)"`
}
10 changes: 10 additions & 0 deletions modules/structs/org_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ func (vt VisibleType) IsPrivate() bool {
return vt == VisibleTypePrivate
}

// VisibilityString provides the mode string of the visibility type (public, limited, private)
zeripath marked this conversation as resolved.
Show resolved Hide resolved
func (vt VisibleType) String() string {
for k, v := range VisibilityModes {
if vt == v {
return k
}
}
return ""
}

// ExtractKeysFromMapString provides a slice of keys from map
func ExtractKeysFromMapString(in map[string]VisibleType) (keys []string) {
for k := range in {
Expand Down
8 changes: 8 additions & 0 deletions routers/api/v1/admin/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
// required: true
// - name: organization
// in: body
// description: "'visibility' can be \"public\", \"limited\" or \"private\""
// required: true
// schema: { "$ref": "#/definitions/CreateOrgOption" }
// responses:
Expand All @@ -45,6 +46,11 @@ func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
return
}

visibility := api.VisibleTypePublic
if form.Visibility != "" {
visibility = api.VisibilityModes[form.Visibility]
}

org := &models.User{
Name: form.UserName,
FullName: form.FullName,
Expand All @@ -53,7 +59,9 @@ func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
Location: form.Location,
IsActive: true,
Type: models.UserTypeOrganization,
Visibility: visibility,
}

if err := models.CreateOrganization(org, u); err != nil {
if models.IsErrUserAlreadyExist(err) ||
models.IsErrNameReserved(err) ||
Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func ToOrganization(org *models.User) *api.Organization {
Description: org.Description,
Website: org.Website,
Location: org.Location,
Visibility: org.Visibility.String(),
}
}

Expand Down
15 changes: 13 additions & 2 deletions routers/api/v1/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func Create(ctx *context.APIContext, form api.CreateOrgOption) {
// parameters:
// - name: organization
// in: body
// description: "'visibility' can be \"public\", \"limited\" or \"private\""
richmahn marked this conversation as resolved.
Show resolved Hide resolved
// required: true
// schema: { "$ref": "#/definitions/CreateOrgOption" }
// responses:
Expand All @@ -90,6 +91,11 @@ func Create(ctx *context.APIContext, form api.CreateOrgOption) {
return
}

visibility := api.VisibleTypePublic
if form.Visibility != "" {
visibility = api.VisibilityModes[form.Visibility]
}

org := &models.User{
Name: form.UserName,
FullName: form.FullName,
Expand All @@ -98,6 +104,7 @@ func Create(ctx *context.APIContext, form api.CreateOrgOption) {
Location: form.Location,
IsActive: true,
Type: models.UserTypeOrganization,
Visibility: visibility,
}
if err := models.CreateOrganization(org, ctx.User); err != nil {
if models.IsErrUserAlreadyExist(err) ||
Expand Down Expand Up @@ -153,6 +160,7 @@ func Edit(ctx *context.APIContext, form api.EditOrgOption) {
// required: true
// - name: body
// in: body
// description: "'visibility' can be \"public\", \"limited\" or \"private\""
// schema:
// "$ref": "#/definitions/EditOrgOption"
// responses:
Expand All @@ -163,8 +171,11 @@ func Edit(ctx *context.APIContext, form api.EditOrgOption) {
org.Description = form.Description
org.Website = form.Website
org.Location = form.Location
if err := models.UpdateUserCols(org, "full_name", "description", "website", "location"); err != nil {
ctx.Error(500, "UpdateUser", err)
if form.Visibility != "" {
org.Visibility = api.VisibilityModes[form.Visibility]
}
if err := models.UpdateUserCols(org, "full_name", "description", "website", "location", "visibility"); err != nil {
ctx.Error(500, "EditOrganization", err)
return
}

Expand Down
Loading