mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
407 lines
9.4 KiB
407 lines
9.4 KiB
7 years ago
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||
2 years ago
|
// SPDX-License-Identifier: MIT
|
||
7 years ago
|
|
||
1 year ago
|
package setting
|
||
7 years ago
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"testing"
|
||
|
|
||
|
"code.gitea.io/gitea/models"
|
||
3 years ago
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
||
3 years ago
|
"code.gitea.io/gitea/models/organization"
|
||
3 years ago
|
"code.gitea.io/gitea/models/perm"
|
||
3 years ago
|
repo_model "code.gitea.io/gitea/models/repo"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
6 years ago
|
"code.gitea.io/gitea/modules/context"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
7 years ago
|
"code.gitea.io/gitea/modules/test"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/web"
|
||
4 years ago
|
"code.gitea.io/gitea/services/forms"
|
||
7 years ago
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
5 years ago
|
func createSSHAuthorizedKeysTmpPath(t *testing.T) func() {
|
||
2 years ago
|
tmpDir := t.TempDir()
|
||
5 years ago
|
|
||
|
oldPath := setting.SSH.RootPath
|
||
|
setting.SSH.RootPath = tmpDir
|
||
|
|
||
|
return func() {
|
||
|
setting.SSH.RootPath = oldPath
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
func TestAddReadOnlyDeployKey(t *testing.T) {
|
||
5 years ago
|
if deferable := createSSHAuthorizedKeysTmpPath(t); deferable != nil {
|
||
|
defer deferable()
|
||
|
} else {
|
||
|
return
|
||
|
}
|
||
3 years ago
|
unittest.PrepareTestEnv(t)
|
||
7 years ago
|
|
||
1 year ago
|
ctx, _ := test.MockContext(t, "user2/repo1/settings/keys")
|
||
7 years ago
|
|
||
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadRepo(t, ctx, 2)
|
||
|
|
||
4 years ago
|
addKeyForm := forms.AddKeyForm{
|
||
7 years ago
|
Title: "read-only",
|
||
4 years ago
|
Content: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1FgI/8Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE/jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ+K2/ioKgXqnXvltu0A9R8/LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM= nocomment\n",
|
||
7 years ago
|
}
|
||
4 years ago
|
web.SetForm(ctx, &addKeyForm)
|
||
|
DeployKeysPost(ctx)
|
||
3 years ago
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
||
7 years ago
|
|
||
3 years ago
|
unittest.AssertExistsAndLoadBean(t, &asymkey_model.DeployKey{
|
||
7 years ago
|
Name: addKeyForm.Title,
|
||
|
Content: addKeyForm.Content,
|
||
3 years ago
|
Mode: perm.AccessModeRead,
|
||
7 years ago
|
})
|
||
|
}
|
||
|
|
||
|
func TestAddReadWriteOnlyDeployKey(t *testing.T) {
|
||
5 years ago
|
if deferable := createSSHAuthorizedKeysTmpPath(t); deferable != nil {
|
||
|
defer deferable()
|
||
|
} else {
|
||
|
return
|
||
|
}
|
||
|
|
||
3 years ago
|
unittest.PrepareTestEnv(t)
|
||
7 years ago
|
|
||
1 year ago
|
ctx, _ := test.MockContext(t, "user2/repo1/settings/keys")
|
||
7 years ago
|
|
||
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadRepo(t, ctx, 2)
|
||
|
|
||
4 years ago
|
addKeyForm := forms.AddKeyForm{
|
||
7 years ago
|
Title: "read-write",
|
||
4 years ago
|
Content: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1FgI/8Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE/jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ+K2/ioKgXqnXvltu0A9R8/LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM= nocomment\n",
|
||
7 years ago
|
IsWritable: true,
|
||
|
}
|
||
4 years ago
|
web.SetForm(ctx, &addKeyForm)
|
||
|
DeployKeysPost(ctx)
|
||
3 years ago
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
||
7 years ago
|
|
||
3 years ago
|
unittest.AssertExistsAndLoadBean(t, &asymkey_model.DeployKey{
|
||
7 years ago
|
Name: addKeyForm.Title,
|
||
|
Content: addKeyForm.Content,
|
||
3 years ago
|
Mode: perm.AccessModeWrite,
|
||
7 years ago
|
})
|
||
|
}
|
||
6 years ago
|
|
||
|
func TestCollaborationPost(t *testing.T) {
|
||
3 years ago
|
unittest.PrepareTestEnv(t)
|
||
1 year ago
|
ctx, _ := test.MockContext(t, "user2/repo1/issues/labels")
|
||
6 years ago
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadUser(t, ctx, 4)
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
|
|
||
|
ctx.Req.Form.Set("collaborator", "user4")
|
||
|
|
||
3 years ago
|
u := &user_model.User{
|
||
6 years ago
|
LowerName: "user2",
|
||
3 years ago
|
Type: user_model.UserTypeIndividual,
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
re := &repo_model.Repository{
|
||
6 years ago
|
ID: 2,
|
||
|
Owner: u,
|
||
|
}
|
||
|
|
||
|
repo := &context.Repository{
|
||
|
Owner: u,
|
||
|
Repository: re,
|
||
|
}
|
||
|
|
||
|
ctx.Repo = repo
|
||
|
|
||
|
CollaborationPost(ctx)
|
||
|
|
||
3 years ago
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
||
6 years ago
|
|
||
3 years ago
|
exists, err := repo_model.IsCollaborator(ctx, re.ID, 4)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
|
assert.True(t, exists)
|
||
|
}
|
||
|
|
||
6 years ago
|
func TestCollaborationPost_InactiveUser(t *testing.T) {
|
||
3 years ago
|
unittest.PrepareTestEnv(t)
|
||
1 year ago
|
ctx, _ := test.MockContext(t, "user2/repo1/issues/labels")
|
||
6 years ago
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadUser(t, ctx, 9)
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
|
|
||
|
ctx.Req.Form.Set("collaborator", "user9")
|
||
|
|
||
|
repo := &context.Repository{
|
||
3 years ago
|
Owner: &user_model.User{
|
||
6 years ago
|
LowerName: "user2",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
ctx.Repo = repo
|
||
|
|
||
|
CollaborationPost(ctx)
|
||
|
|
||
3 years ago
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
||
6 years ago
|
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
|
||
|
}
|
||
|
|
||
6 years ago
|
func TestCollaborationPost_AddCollaboratorTwice(t *testing.T) {
|
||
3 years ago
|
unittest.PrepareTestEnv(t)
|
||
1 year ago
|
ctx, _ := test.MockContext(t, "user2/repo1/issues/labels")
|
||
6 years ago
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadUser(t, ctx, 4)
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
|
|
||
|
ctx.Req.Form.Set("collaborator", "user4")
|
||
|
|
||
3 years ago
|
u := &user_model.User{
|
||
6 years ago
|
LowerName: "user2",
|
||
3 years ago
|
Type: user_model.UserTypeIndividual,
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
re := &repo_model.Repository{
|
||
6 years ago
|
ID: 2,
|
||
|
Owner: u,
|
||
|
}
|
||
|
|
||
|
repo := &context.Repository{
|
||
|
Owner: u,
|
||
|
Repository: re,
|
||
|
}
|
||
|
|
||
|
ctx.Repo = repo
|
||
|
|
||
|
CollaborationPost(ctx)
|
||
|
|
||
3 years ago
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
||
6 years ago
|
|
||
3 years ago
|
exists, err := repo_model.IsCollaborator(ctx, re.ID, 4)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
|
assert.True(t, exists)
|
||
|
|
||
|
// Try adding the same collaborator again
|
||
|
CollaborationPost(ctx)
|
||
|
|
||
3 years ago
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
||
6 years ago
|
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
|
||
|
}
|
||
|
|
||
|
func TestCollaborationPost_NonExistentUser(t *testing.T) {
|
||
3 years ago
|
unittest.PrepareTestEnv(t)
|
||
1 year ago
|
ctx, _ := test.MockContext(t, "user2/repo1/issues/labels")
|
||
6 years ago
|
test.LoadUser(t, ctx, 2)
|
||
|
test.LoadRepo(t, ctx, 1)
|
||
|
|
||
|
ctx.Req.Form.Set("collaborator", "user34")
|
||
|
|
||
|
repo := &context.Repository{
|
||
3 years ago
|
Owner: &user_model.User{
|
||
6 years ago
|
LowerName: "user2",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
ctx.Repo = repo
|
||
|
|
||
|
CollaborationPost(ctx)
|
||
|
|
||
3 years ago
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
||
6 years ago
|
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
|
||
|
}
|
||
5 years ago
|
|
||
|
func TestAddTeamPost(t *testing.T) {
|
||
3 years ago
|
unittest.PrepareTestEnv(t)
|
||
1 year ago
|
ctx, _ := test.MockContext(t, "org26/repo43")
|
||
5 years ago
|
|
||
|
ctx.Req.Form.Set("team", "team11")
|
||
|
|
||
3 years ago
|
org := &user_model.User{
|
||
5 years ago
|
LowerName: "org26",
|
||
3 years ago
|
Type: user_model.UserTypeOrganization,
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
team := &organization.Team{
|
||
5 years ago
|
ID: 11,
|
||
|
OrgID: 26,
|
||
|
}
|
||
|
|
||
3 years ago
|
re := &repo_model.Repository{
|
||
5 years ago
|
ID: 43,
|
||
|
Owner: org,
|
||
|
OwnerID: 26,
|
||
|
}
|
||
|
|
||
|
repo := &context.Repository{
|
||
3 years ago
|
Owner: &user_model.User{
|
||
5 years ago
|
ID: 26,
|
||
|
LowerName: "org26",
|
||
|
RepoAdminChangeTeamAccess: true,
|
||
|
},
|
||
|
Repository: re,
|
||
|
}
|
||
|
|
||
|
ctx.Repo = repo
|
||
|
|
||
|
AddTeamPost(ctx)
|
||
|
|
||
3 years ago
|
assert.True(t, models.HasRepository(team, re.ID))
|
||
3 years ago
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
||
5 years ago
|
assert.Empty(t, ctx.Flash.ErrorMsg)
|
||
|
}
|
||
|
|
||
|
func TestAddTeamPost_NotAllowed(t *testing.T) {
|
||
3 years ago
|
unittest.PrepareTestEnv(t)
|
||
1 year ago
|
ctx, _ := test.MockContext(t, "org26/repo43")
|
||
5 years ago
|
|
||
|
ctx.Req.Form.Set("team", "team11")
|
||
|
|
||
3 years ago
|
org := &user_model.User{
|
||
5 years ago
|
LowerName: "org26",
|
||
3 years ago
|
Type: user_model.UserTypeOrganization,
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
team := &organization.Team{
|
||
5 years ago
|
ID: 11,
|
||
|
OrgID: 26,
|
||
|
}
|
||
|
|
||
3 years ago
|
re := &repo_model.Repository{
|
||
5 years ago
|
ID: 43,
|
||
|
Owner: org,
|
||
|
OwnerID: 26,
|
||
|
}
|
||
|
|
||
|
repo := &context.Repository{
|
||
3 years ago
|
Owner: &user_model.User{
|
||
5 years ago
|
ID: 26,
|
||
|
LowerName: "org26",
|
||
|
RepoAdminChangeTeamAccess: false,
|
||
|
},
|
||
|
Repository: re,
|
||
|
}
|
||
|
|
||
|
ctx.Repo = repo
|
||
|
|
||
|
AddTeamPost(ctx)
|
||
|
|
||
3 years ago
|
assert.False(t, models.HasRepository(team, re.ID))
|
||
3 years ago
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
||
5 years ago
|
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
|
||
|
}
|
||
|
|
||
|
func TestAddTeamPost_AddTeamTwice(t *testing.T) {
|
||
3 years ago
|
unittest.PrepareTestEnv(t)
|
||
1 year ago
|
ctx, _ := test.MockContext(t, "org26/repo43")
|
||
5 years ago
|
|
||
|
ctx.Req.Form.Set("team", "team11")
|
||
|
|
||
3 years ago
|
org := &user_model.User{
|
||
5 years ago
|
LowerName: "org26",
|
||
3 years ago
|
Type: user_model.UserTypeOrganization,
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
team := &organization.Team{
|
||
5 years ago
|
ID: 11,
|
||
|
OrgID: 26,
|
||
|
}
|
||
|
|
||
3 years ago
|
re := &repo_model.Repository{
|
||
5 years ago
|
ID: 43,
|
||
|
Owner: org,
|
||
|
OwnerID: 26,
|
||
|
}
|
||
|
|
||
|
repo := &context.Repository{
|
||
3 years ago
|
Owner: &user_model.User{
|
||
5 years ago
|
ID: 26,
|
||
|
LowerName: "org26",
|
||
|
RepoAdminChangeTeamAccess: true,
|
||
|
},
|
||
|
Repository: re,
|
||
|
}
|
||
|
|
||
|
ctx.Repo = repo
|
||
|
|
||
|
AddTeamPost(ctx)
|
||
|
|
||
|
AddTeamPost(ctx)
|
||
3 years ago
|
assert.True(t, models.HasRepository(team, re.ID))
|
||
3 years ago
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
||
5 years ago
|
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
|
||
|
}
|
||
|
|
||
|
func TestAddTeamPost_NonExistentTeam(t *testing.T) {
|
||
3 years ago
|
unittest.PrepareTestEnv(t)
|
||
1 year ago
|
ctx, _ := test.MockContext(t, "org26/repo43")
|
||
5 years ago
|
|
||
|
ctx.Req.Form.Set("team", "team-non-existent")
|
||
|
|
||
3 years ago
|
org := &user_model.User{
|
||
5 years ago
|
LowerName: "org26",
|
||
3 years ago
|
Type: user_model.UserTypeOrganization,
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
re := &repo_model.Repository{
|
||
5 years ago
|
ID: 43,
|
||
|
Owner: org,
|
||
|
OwnerID: 26,
|
||
|
}
|
||
|
|
||
|
repo := &context.Repository{
|
||
3 years ago
|
Owner: &user_model.User{
|
||
5 years ago
|
ID: 26,
|
||
|
LowerName: "org26",
|
||
|
RepoAdminChangeTeamAccess: true,
|
||
|
},
|
||
|
Repository: re,
|
||
|
}
|
||
|
|
||
|
ctx.Repo = repo
|
||
|
|
||
|
AddTeamPost(ctx)
|
||
3 years ago
|
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
|
||
5 years ago
|
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
|
||
|
}
|
||
|
|
||
|
func TestDeleteTeam(t *testing.T) {
|
||
3 years ago
|
unittest.PrepareTestEnv(t)
|
||
1 year ago
|
ctx, _ := test.MockContext(t, "org3/team1/repo3")
|
||
5 years ago
|
|
||
|
ctx.Req.Form.Set("id", "2")
|
||
|
|
||
3 years ago
|
org := &user_model.User{
|
||
5 years ago
|
LowerName: "org3",
|
||
3 years ago
|
Type: user_model.UserTypeOrganization,
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
team := &organization.Team{
|
||
5 years ago
|
ID: 2,
|
||
|
OrgID: 3,
|
||
|
}
|
||
|
|
||
3 years ago
|
re := &repo_model.Repository{
|
||
5 years ago
|
ID: 3,
|
||
|
Owner: org,
|
||
|
OwnerID: 3,
|
||
|
}
|
||
|
|
||
|
repo := &context.Repository{
|
||
3 years ago
|
Owner: &user_model.User{
|
||
5 years ago
|
ID: 3,
|
||
|
LowerName: "org3",
|
||
|
RepoAdminChangeTeamAccess: true,
|
||
|
},
|
||
|
Repository: re,
|
||
|
}
|
||
|
|
||
|
ctx.Repo = repo
|
||
|
|
||
|
DeleteTeam(ctx)
|
||
|
|
||
3 years ago
|
assert.False(t, models.HasRepository(team, re.ID))
|
||
5 years ago
|
}
|