mirror of
https://github.com/go-gitea/gitea.git
synced 2024-11-30 13:14:39 +08:00
b06b9a056c
* Move organization related structs into sub package * Fix test * Fix lint * Move more functions into sub packages * Fix bug * Fix test * Update models/organization/team_repo.go Co-authored-by: KN4CK3R <admin@oldschoolhack.me> * Apply suggestions from code review Co-authored-by: KN4CK3R <admin@oldschoolhack.me> * Fix fmt * Follow suggestion from @Gusted * Fix test * Fix test * Fix bug * Use ctx but db.DefaultContext on routers * Fix bug * Fix bug * fix bug * Update models/organization/team_user.go * Fix bug Co-authored-by: KN4CK3R <admin@oldschoolhack.me> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
266 lines
8.9 KiB
Go
266 lines
8.9 KiB
Go
// 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"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/models/organization"
|
|
perm_model "code.gitea.io/gitea/models/perm"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
"code.gitea.io/gitea/models/unit"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestRepoPermissionPublicNonOrgRepo(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// public non-organization repo
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}).(*repo_model.Repository)
|
|
assert.NoError(t, repo.LoadUnits(db.DefaultContext))
|
|
|
|
// plain user
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
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, AddCollaborator(repo, 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
|
|
collaborator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
|
|
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
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
|
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
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
|
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) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// private non-organization repo
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
|
|
assert.NoError(t, repo.LoadUnits(db.DefaultContext))
|
|
|
|
// plain user
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
|
|
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, AddCollaborator(repo, 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, ChangeCollaborationAccessMode(repo, user.ID, perm_model.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
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
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
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
|
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) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// public organization repo
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 32}).(*repo_model.Repository)
|
|
assert.NoError(t, repo.LoadUnits(db.DefaultContext))
|
|
|
|
// plain user
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
|
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, AddCollaborator(repo, 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, ChangeCollaborationAccessMode(repo, user.ID, perm_model.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
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
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
|
|
member := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15}).(*user_model.User)
|
|
perm, err = GetUserRepoPermission(repo, member)
|
|
assert.NoError(t, err)
|
|
for _, unit := range repo.Units {
|
|
assert.True(t, perm.CanRead(unit.Type))
|
|
}
|
|
assert.True(t, perm.CanWrite(unit.TypeIssues))
|
|
assert.False(t, perm.CanWrite(unit.TypeCode))
|
|
|
|
// admin
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
|
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) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// private organization repo
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 24}).(*repo_model.Repository)
|
|
assert.NoError(t, repo.LoadUnits(db.DefaultContext))
|
|
|
|
// plain user
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
|
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, AddCollaborator(repo, 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, ChangeCollaborationAccessMode(repo, user.ID, perm_model.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
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15}).(*user_model.User)
|
|
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))
|
|
}
|
|
|
|
// update team information and then check permission
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 5}).(*organization.Team)
|
|
err = organization.UpdateTeamUnits(team, nil)
|
|
assert.NoError(t, err)
|
|
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
|
|
tester := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
perm, err = GetUserRepoPermission(repo, tester)
|
|
assert.NoError(t, err)
|
|
assert.True(t, perm.CanWrite(unit.TypeIssues))
|
|
assert.False(t, perm.CanWrite(unit.TypeCode))
|
|
assert.False(t, perm.CanRead(unit.TypeCode))
|
|
|
|
// org member team reviewer
|
|
reviewer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 20}).(*user_model.User)
|
|
perm, err = GetUserRepoPermission(repo, reviewer)
|
|
assert.NoError(t, err)
|
|
assert.False(t, perm.CanRead(unit.TypeIssues))
|
|
assert.False(t, perm.CanWrite(unit.TypeCode))
|
|
assert.True(t, perm.CanRead(unit.TypeCode))
|
|
|
|
// admin
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
|
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))
|
|
}
|
|
}
|