2018-11-28 19:26:14 +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.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 14:29:02 +08:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 19:58:28 +08:00
|
|
|
perm_model "code.gitea.io/gitea/models/perm"
|
2022-05-11 18:09:36 +08:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 03:57:58 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-12 22:36:47 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 20:34:35 +08:00
|
|
|
|
2018-11-28 19:26:14 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepoPermissionPublicNonOrgRepo(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
// public non-organization repo
|
2021-12-10 09:27:50 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}).(*repo_model.Repository)
|
|
|
|
assert.NoError(t, repo.LoadUnits(db.DefaultContext))
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
// plain user
|
2021-11-24 17:49:20 +08:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.False(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// change to collaborator
|
2021-12-10 09:27:50 +08:00
|
|
|
assert.NoError(t, AddCollaborator(repo, user))
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// collaborator
|
2021-11-24 17:49:20 +08:00
|
|
|
collaborator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, collaborator)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// owner
|
2021-11-24 17:49:20 +08:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// admin
|
2021-11-24 17:49:20 +08:00
|
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
// private non-organization repo
|
2021-12-10 09:27:50 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
|
|
|
|
assert.NoError(t, repo.LoadUnits(db.DefaultContext))
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
// plain user
|
2021-11-24 17:49:20 +08:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.False(t, perm.CanRead(unit.Type))
|
|
|
|
assert.False(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// change to collaborator to default write access
|
2021-12-10 09:27:50 +08:00
|
|
|
assert.NoError(t, AddCollaborator(repo, user))
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
2022-05-11 18:09:36 +08:00
|
|
|
assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, user.ID, perm_model.AccessModeRead))
|
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.False(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// owner
|
2021-11-24 17:49:20 +08:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// admin
|
2021-11-24 17:49:20 +08:00
|
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoPermissionPublicOrgRepo(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
// public organization repo
|
2021-12-10 09:27:50 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 32}).(*repo_model.Repository)
|
|
|
|
assert.NoError(t, repo.LoadUnits(db.DefaultContext))
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
// plain user
|
2021-11-24 17:49:20 +08:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.False(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// change to collaborator to default write access
|
2021-12-10 09:27:50 +08:00
|
|
|
assert.NoError(t, AddCollaborator(repo, user))
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
2022-05-11 18:09:36 +08:00
|
|
|
assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, user.ID, perm_model.AccessModeRead))
|
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.False(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// org member team owner
|
2021-11-24 17:49:20 +08:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// org member team tester
|
2021-11-24 17:49:20 +08:00
|
|
|
member := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, member)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
}
|
2021-11-10 03:57:58 +08:00
|
|
|
assert.True(t, perm.CanWrite(unit.TypeIssues))
|
|
|
|
assert.False(t, perm.CanWrite(unit.TypeCode))
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
// admin
|
2021-11-24 17:49:20 +08:00
|
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoPermissionPrivateOrgRepo(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
// private organization repo
|
2021-12-10 09:27:50 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 24}).(*repo_model.Repository)
|
|
|
|
assert.NoError(t, repo.LoadUnits(db.DefaultContext))
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
// plain user
|
2021-11-24 17:49:20 +08:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.False(t, perm.CanRead(unit.Type))
|
|
|
|
assert.False(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// change to collaborator to default write access
|
2021-12-10 09:27:50 +08:00
|
|
|
assert.NoError(t, AddCollaborator(repo, user))
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
2022-05-11 18:09:36 +08:00
|
|
|
assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, user.ID, perm_model.AccessModeRead))
|
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.False(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// org member team owner
|
2021-11-24 17:49:20 +08:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner)
|
2019-02-23 00:14:45 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// update team information and then check permission
|
2022-03-29 14:29:02 +08:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 5}).(*organization.Team)
|
|
|
|
err = organization.UpdateTeamUnits(team, nil)
|
2019-02-23 00:14:45 +08:00
|
|
|
assert.NoError(t, err)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
// org member team tester
|
2021-11-24 17:49:20 +08:00
|
|
|
tester := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, tester)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
2021-11-10 03:57:58 +08:00
|
|
|
assert.True(t, perm.CanWrite(unit.TypeIssues))
|
|
|
|
assert.False(t, perm.CanWrite(unit.TypeCode))
|
|
|
|
assert.False(t, perm.CanRead(unit.TypeCode))
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
// org member team reviewer
|
2021-11-24 17:49:20 +08:00
|
|
|
reviewer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 20}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, reviewer)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
2021-11-10 03:57:58 +08:00
|
|
|
assert.False(t, perm.CanRead(unit.TypeIssues))
|
|
|
|
assert.False(t, perm.CanWrite(unit.TypeCode))
|
|
|
|
assert.True(t, perm.CanRead(unit.TypeCode))
|
2018-11-28 19:26:14 +08:00
|
|
|
|
|
|
|
// admin
|
2021-11-24 17:49:20 +08:00
|
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin)
|
2018-11-28 19:26:14 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
}
|