2017-01-07 22:10:53 -05:00
|
|
|
// Copyright 2017 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"
|
2017-01-24 21:49:51 -05:00
|
|
|
|
2021-09-19 06:49:59 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 01:29:02 -05:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 06:58:28 -05:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 07:34:35 -05:00
|
|
|
|
2017-01-07 22:10:53 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccessLevel(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
|
|
user5 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
|
|
|
user29 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 29}).(*user_model.User)
|
2018-06-21 11:00:13 -05:00
|
|
|
// A public repository owned by User 2
|
2021-12-09 20:27:50 -05:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
2018-06-21 11:00:13 -05:00
|
|
|
assert.False(t, repo1.IsPrivate)
|
|
|
|
// A private repository owned by Org 3
|
2021-12-09 20:27:50 -05:00
|
|
|
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
2018-11-28 06:26:14 -05:00
|
|
|
assert.True(t, repo3.IsPrivate)
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2020-01-13 12:33:46 -05:00
|
|
|
// Another public repository
|
2021-12-09 20:27:50 -05:00
|
|
|
repo4 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}).(*repo_model.Repository)
|
2020-01-13 12:33:46 -05:00
|
|
|
assert.False(t, repo4.IsPrivate)
|
|
|
|
// org. owned private repo
|
2021-12-09 20:27:50 -05:00
|
|
|
repo24 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 24}).(*repo_model.Repository)
|
2020-01-13 12:33:46 -05:00
|
|
|
|
2018-11-28 06:26:14 -05:00
|
|
|
level, err := AccessLevel(user2, repo1)
|
2017-01-07 22:10:53 -05:00
|
|
|
assert.NoError(t, err)
|
2021-11-28 06:58:28 -05:00
|
|
|
assert.Equal(t, perm.AccessModeOwner, level)
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2018-11-28 06:26:14 -05:00
|
|
|
level, err = AccessLevel(user2, repo3)
|
2017-01-07 22:10:53 -05:00
|
|
|
assert.NoError(t, err)
|
2021-11-28 06:58:28 -05:00
|
|
|
assert.Equal(t, perm.AccessModeOwner, level)
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2018-11-28 06:26:14 -05:00
|
|
|
level, err = AccessLevel(user5, repo1)
|
2017-01-07 22:10:53 -05:00
|
|
|
assert.NoError(t, err)
|
2021-11-28 06:58:28 -05:00
|
|
|
assert.Equal(t, perm.AccessModeRead, level)
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2018-11-28 06:26:14 -05:00
|
|
|
level, err = AccessLevel(user5, repo3)
|
2017-01-07 22:10:53 -05:00
|
|
|
assert.NoError(t, err)
|
2021-11-28 06:58:28 -05:00
|
|
|
assert.Equal(t, perm.AccessModeNone, level)
|
2020-01-13 12:33:46 -05:00
|
|
|
|
|
|
|
// restricted user has no access to a public repo
|
|
|
|
level, err = AccessLevel(user29, repo1)
|
|
|
|
assert.NoError(t, err)
|
2021-11-28 06:58:28 -05:00
|
|
|
assert.Equal(t, perm.AccessModeNone, level)
|
2020-01-13 12:33:46 -05:00
|
|
|
|
|
|
|
// ... unless he's a collaborator
|
|
|
|
level, err = AccessLevel(user29, repo4)
|
|
|
|
assert.NoError(t, err)
|
2021-11-28 06:58:28 -05:00
|
|
|
assert.Equal(t, perm.AccessModeWrite, level)
|
2020-01-13 12:33:46 -05:00
|
|
|
|
|
|
|
// ... or a team member
|
|
|
|
level, err = AccessLevel(user29, repo24)
|
|
|
|
assert.NoError(t, err)
|
2021-11-28 06:58:28 -05:00
|
|
|
assert.Equal(t, perm.AccessModeRead, level)
|
2017-01-07 22:10:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasAccess(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
2018-06-21 11:00:13 -05:00
|
|
|
// A public repository owned by User 2
|
2021-12-09 20:27:50 -05:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
2018-06-21 11:00:13 -05:00
|
|
|
assert.False(t, repo1.IsPrivate)
|
|
|
|
// A private repository owned by Org 3
|
2021-12-09 20:27:50 -05:00
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
2018-06-21 11:00:13 -05:00
|
|
|
assert.True(t, repo2.IsPrivate)
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2018-11-28 06:26:14 -05:00
|
|
|
has, err := HasAccess(user1.ID, repo1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, has)
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2019-07-22 02:11:16 -05:00
|
|
|
_, err = HasAccess(user1.ID, repo2)
|
2018-11-28 06:26:14 -05:00
|
|
|
assert.NoError(t, err)
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2019-07-22 02:11:16 -05:00
|
|
|
_, err = HasAccess(user2.ID, repo1)
|
2018-11-28 06:26:14 -05:00
|
|
|
assert.NoError(t, err)
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2019-07-22 02:11:16 -05:00
|
|
|
_, err = HasAccess(user2.ID, repo2)
|
2018-11-28 06:26:14 -05:00
|
|
|
assert.NoError(t, err)
|
2017-01-07 22:10:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_RecalculateAccesses(t *testing.T) {
|
|
|
|
// test with organization repo
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-12-09 20:27:50 -05:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
|
|
|
assert.NoError(t, repo1.GetOwner(db.DefaultContext))
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2021-09-23 10:45:36 -05:00
|
|
|
_, err := db.GetEngine(db.DefaultContext).Delete(&Collaboration{UserID: 2, RepoID: 3})
|
2017-01-07 22:10:53 -05:00
|
|
|
assert.NoError(t, err)
|
2021-12-09 20:27:50 -05:00
|
|
|
assert.NoError(t, RecalculateAccesses(repo1))
|
2017-01-07 22:10:53 -05:00
|
|
|
|
|
|
|
access := &Access{UserID: 2, RepoID: 3}
|
2021-09-23 10:45:36 -05:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Get(access)
|
2017-01-07 22:10:53 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, has)
|
2021-11-28 06:58:28 -05:00
|
|
|
assert.Equal(t, perm.AccessModeOwner, access.Mode)
|
2017-01-07 22:10:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_RecalculateAccesses2(t *testing.T) {
|
|
|
|
// test with non-organization repo
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-12-09 20:27:50 -05:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}).(*repo_model.Repository)
|
|
|
|
assert.NoError(t, repo1.GetOwner(db.DefaultContext))
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2021-09-23 10:45:36 -05:00
|
|
|
_, err := db.GetEngine(db.DefaultContext).Delete(&Collaboration{UserID: 4, RepoID: 4})
|
2017-01-07 22:10:53 -05:00
|
|
|
assert.NoError(t, err)
|
2021-12-09 20:27:50 -05:00
|
|
|
assert.NoError(t, RecalculateAccesses(repo1))
|
2017-01-07 22:10:53 -05:00
|
|
|
|
2021-09-23 10:45:36 -05:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Get(&Access{UserID: 4, RepoID: 4})
|
2017-01-07 22:10:53 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, has)
|
|
|
|
}
|
2020-01-13 12:33:46 -05:00
|
|
|
|
|
|
|
func TestRepository_RecalculateAccesses3(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 01:29:02 -05:00
|
|
|
team5 := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 5}).(*organization.Team)
|
2021-11-24 04:49:20 -05:00
|
|
|
user29 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 29}).(*user_model.User)
|
2020-01-13 12:33:46 -05:00
|
|
|
|
2021-09-23 10:45:36 -05:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Get(&Access{UserID: 29, RepoID: 23})
|
2020-01-13 12:33:46 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, has)
|
|
|
|
|
|
|
|
// adding user29 to team5 should add an explicit access row for repo 23
|
|
|
|
// even though repo 23 is public
|
|
|
|
assert.NoError(t, AddTeamMember(team5, user29.ID))
|
|
|
|
|
2021-09-23 10:45:36 -05:00
|
|
|
has, err = db.GetEngine(db.DefaultContext).Get(&Access{UserID: 29, RepoID: 23})
|
2020-01-13 12:33:46 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, has)
|
|
|
|
}
|