2018-11-28 14:26:14 +03: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 14:49:59 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-09 22:57:58 +03:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-12 17:36:47 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 12:49:20 +03:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 15:34:35 +03:00
|
|
|
|
2018-11-28 14:26:14 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepoPermissionPublicNonOrgRepo(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-11-28 14:26:14 +03:00
|
|
|
|
|
|
|
// public non-organization repo
|
2021-11-16 11:53:21 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
|
2021-09-23 18:45:36 +03:00
|
|
|
assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext)))
|
2018-11-28 14:26:14 +03:00
|
|
|
|
|
|
|
// plain user
|
2021-11-24 12:49:20 +03:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err := GetUserRepoPermission(repo, user)
|
|
|
|
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
|
|
|
|
assert.NoError(t, repo.AddCollaborator(user))
|
|
|
|
perm, err = GetUserRepoPermission(repo, user)
|
|
|
|
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 12:49:20 +03:00
|
|
|
collaborator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, collaborator)
|
|
|
|
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 12:49:20 +03:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, owner)
|
|
|
|
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 12:49:20 +03:00
|
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, admin)
|
|
|
|
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 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-11-28 14:26:14 +03:00
|
|
|
|
|
|
|
// private non-organization repo
|
2021-11-16 11:53:21 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
|
2021-09-23 18:45:36 +03:00
|
|
|
assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext)))
|
2018-11-28 14:26:14 +03:00
|
|
|
|
|
|
|
// plain user
|
2021-11-24 12:49:20 +03:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err := GetUserRepoPermission(repo, user)
|
|
|
|
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
|
|
|
|
assert.NoError(t, repo.AddCollaborator(user))
|
|
|
|
perm, err = GetUserRepoPermission(repo, user)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, repo.ChangeCollaborationAccessMode(user.ID, AccessModeRead))
|
|
|
|
perm, err = GetUserRepoPermission(repo, user)
|
|
|
|
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 12:49:20 +03:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, owner)
|
|
|
|
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 12:49:20 +03:00
|
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, admin)
|
|
|
|
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 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-11-28 14:26:14 +03:00
|
|
|
|
|
|
|
// public organization repo
|
2021-11-16 11:53:21 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 32}).(*Repository)
|
2021-09-23 18:45:36 +03:00
|
|
|
assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext)))
|
2018-11-28 14:26:14 +03:00
|
|
|
|
|
|
|
// plain user
|
2021-11-24 12:49:20 +03:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err := GetUserRepoPermission(repo, user)
|
|
|
|
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
|
|
|
|
assert.NoError(t, repo.AddCollaborator(user))
|
|
|
|
perm, err = GetUserRepoPermission(repo, user)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, repo.ChangeCollaborationAccessMode(user.ID, AccessModeRead))
|
|
|
|
perm, err = GetUserRepoPermission(repo, user)
|
|
|
|
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 12:49:20 +03:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, owner)
|
|
|
|
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 12:49:20 +03:00
|
|
|
member := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, member)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
}
|
2021-11-09 22:57:58 +03:00
|
|
|
assert.True(t, perm.CanWrite(unit.TypeIssues))
|
|
|
|
assert.False(t, perm.CanWrite(unit.TypeCode))
|
2018-11-28 14:26:14 +03:00
|
|
|
|
|
|
|
// admin
|
2021-11-24 12:49:20 +03:00
|
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, admin)
|
|
|
|
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 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-11-28 14:26:14 +03:00
|
|
|
|
|
|
|
// private organization repo
|
2021-11-16 11:53:21 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository)
|
2021-09-23 18:45:36 +03:00
|
|
|
assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext)))
|
2018-11-28 14:26:14 +03:00
|
|
|
|
|
|
|
// plain user
|
2021-11-24 12:49:20 +03:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err := GetUserRepoPermission(repo, user)
|
|
|
|
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
|
|
|
|
assert.NoError(t, repo.AddCollaborator(user))
|
|
|
|
perm, err = GetUserRepoPermission(repo, user)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, repo.ChangeCollaborationAccessMode(user.ID, AccessModeRead))
|
|
|
|
perm, err = GetUserRepoPermission(repo, user)
|
|
|
|
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 12:49:20 +03:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, owner)
|
2019-02-22 19:14:45 +03: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
|
2021-11-16 11:53:21 +03:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team)
|
2019-02-22 19:14:45 +03:00
|
|
|
err = UpdateTeamUnits(team, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
perm, err = GetUserRepoPermission(repo, owner)
|
2018-11-28 14:26:14 +03: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 12:49:20 +03:00
|
|
|
tester := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, tester)
|
|
|
|
assert.NoError(t, err)
|
2021-11-09 22:57:58 +03: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 14:26:14 +03:00
|
|
|
|
|
|
|
// org member team reviewer
|
2021-11-24 12:49:20 +03:00
|
|
|
reviewer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 20}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, reviewer)
|
|
|
|
assert.NoError(t, err)
|
2021-11-09 22:57:58 +03: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 14:26:14 +03:00
|
|
|
|
|
|
|
// admin
|
2021-11-24 12:49:20 +03:00
|
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2018-11-28 14:26:14 +03:00
|
|
|
perm, err = GetUserRepoPermission(repo, admin)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, unit := range repo.Units {
|
|
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
|
|
assert.True(t, perm.CanWrite(unit.Type))
|
|
|
|
}
|
|
|
|
}
|