2018-11-21 01:31:30 +08:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2022-09-03 03:18:23 +08:00
|
|
|
package integration
|
2018-11-21 01:31:30 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-04-25 00:16:14 +08:00
|
|
|
"net/url"
|
2018-11-21 01:31:30 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-16 16:53:21 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-04-25 00:16:14 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 18:21:34 +08:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2022-09-03 03:18:23 +08:00
|
|
|
"code.gitea.io/gitea/tests"
|
2018-11-21 01:31:30 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-05-31 01:57:55 +08:00
|
|
|
func TestAPIOrgCreate(t *testing.T) {
|
2019-04-25 00:16:14 +08:00
|
|
|
onGiteaRun(t, func(*testing.T, *url.URL) {
|
2022-04-08 12:22:10 +08:00
|
|
|
token := getUserToken(t, "user1")
|
2019-04-25 00:16:14 +08:00
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
org := api.CreateOrgOption{
|
2019-04-25 00:16:14 +08:00
|
|
|
UserName: "user1_org",
|
|
|
|
FullName: "User1's organization",
|
|
|
|
Description: "This organization created by user1",
|
|
|
|
Website: "https://try.gitea.io",
|
|
|
|
Location: "Shanghai",
|
2019-05-31 01:57:55 +08:00
|
|
|
Visibility: "limited",
|
2019-04-25 00:16:14 +08:00
|
|
|
}
|
|
|
|
req := NewRequestWithJSON(t, "POST", "/api/v1/orgs?token="+token, &org)
|
2022-04-08 12:22:10 +08:00
|
|
|
resp := MakeRequest(t, req, http.StatusCreated)
|
2019-04-25 00:16:14 +08:00
|
|
|
|
|
|
|
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)
|
2019-05-31 01:57:55 +08:00
|
|
|
assert.Equal(t, org.Visibility, apiOrg.Visibility)
|
2019-04-25 00:16:14 +08:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &user_model.User{
|
2019-04-25 00:16:14 +08:00
|
|
|
Name: org.UserName,
|
|
|
|
LowerName: strings.ToLower(org.UserName),
|
|
|
|
FullName: org.FullName,
|
|
|
|
})
|
|
|
|
|
2022-04-08 12:22:10 +08:00
|
|
|
req = NewRequestf(t, "GET", "/api/v1/orgs/%s?token=%s", org.UserName, token)
|
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2019-04-25 00:16:14 +08:00
|
|
|
DecodeJSON(t, resp, &apiOrg)
|
|
|
|
assert.EqualValues(t, org.UserName, apiOrg.UserName)
|
|
|
|
|
2022-04-08 12:22:10 +08:00
|
|
|
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos?token=%s", org.UserName, token)
|
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2019-04-25 00:16:14 +08:00
|
|
|
|
|
|
|
var repos []*api.Repository
|
|
|
|
DecodeJSON(t, resp, &repos)
|
|
|
|
for _, repo := range repos {
|
|
|
|
assert.False(t, repo.Private)
|
|
|
|
}
|
|
|
|
|
2022-04-08 12:22:10 +08:00
|
|
|
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members?token=%s", org.UserName, token)
|
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2019-04-25 00:16:14 +08:00
|
|
|
|
|
|
|
// user1 on this org is public
|
|
|
|
var users []*api.User
|
|
|
|
DecodeJSON(t, resp, &users)
|
2021-06-07 13:27:09 +08:00
|
|
|
assert.Len(t, users, 1)
|
2019-04-25 00:16:14 +08:00
|
|
|
assert.EqualValues(t, "user1", users[0].UserName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:57:55 +08:00
|
|
|
func TestAPIOrgEdit(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(*testing.T, *url.URL) {
|
|
|
|
session := loginUser(t, "user1")
|
|
|
|
|
|
|
|
token := getTokenForLoggedInUser(t, session)
|
2022-01-21 01:46:10 +08:00
|
|
|
org := api.EditOrgOption{
|
2019-05-31 01:57:55 +08:00
|
|
|
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)
|
2022-01-21 01:46:10 +08:00
|
|
|
org := api.EditOrgOption{
|
2019-05-31 01:57:55 +08:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-25 00:16:14 +08:00
|
|
|
func TestAPIOrgDeny(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(*testing.T, *url.URL) {
|
|
|
|
setting.Service.RequireSignInView = true
|
|
|
|
defer func() {
|
|
|
|
setting.Service.RequireSignInView = false
|
|
|
|
}()
|
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
orgName := "user1_org"
|
2019-04-25 00:16:14 +08:00
|
|
|
req := NewRequestf(t, "GET", "/api/v1/orgs/%s", orgName)
|
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
|
|
|
|
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", orgName)
|
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
|
|
|
|
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", orgName)
|
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
2018-11-21 01:31:30 +08:00
|
|
|
})
|
|
|
|
}
|
2020-01-12 23:43:44 +08:00
|
|
|
|
|
|
|
func TestAPIGetAll(t *testing.T) {
|
2022-09-03 03:18:23 +08:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2020-01-12 23:43:44 +08:00
|
|
|
|
|
|
|
req := NewRequestf(t, "GET", "/api/v1/orgs")
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
var apiOrgList []*api.Organization
|
|
|
|
DecodeJSON(t, resp, &apiOrgList)
|
|
|
|
|
|
|
|
assert.Len(t, apiOrgList, 7)
|
|
|
|
assert.Equal(t, "org25", apiOrgList[0].FullName)
|
|
|
|
assert.Equal(t, "public", apiOrgList[0].Visibility)
|
|
|
|
}
|