2017-01-08 12:10:53 +09: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-25 11:49:51 +09:00
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 23:36:47 +09:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2017-01-08 12:10:53 +09:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccessLevel(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-08 12:10:53 +09:00
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
|
|
|
user5 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
|
|
|
|
user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
|
2018-06-22 01:00:13 +09:00
|
|
|
// A public repository owned by User 2
|
2021-09-19 20:49:59 +09:00
|
|
|
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
2018-06-22 01:00:13 +09:00
|
|
|
assert.False(t, repo1.IsPrivate)
|
|
|
|
// A private repository owned by Org 3
|
2021-09-19 20:49:59 +09:00
|
|
|
repo3 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
|
2018-11-28 20:26:14 +09:00
|
|
|
assert.True(t, repo3.IsPrivate)
|
2017-01-08 12:10:53 +09:00
|
|
|
|
2020-01-14 02:33:46 +09:00
|
|
|
// Another public repository
|
2021-09-19 20:49:59 +09:00
|
|
|
repo4 := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
|
2020-01-14 02:33:46 +09:00
|
|
|
assert.False(t, repo4.IsPrivate)
|
|
|
|
// org. owned private repo
|
2021-09-19 20:49:59 +09:00
|
|
|
repo24 := db.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository)
|
2020-01-14 02:33:46 +09:00
|
|
|
|
2018-11-28 20:26:14 +09:00
|
|
|
level, err := AccessLevel(user2, repo1)
|
2017-01-08 12:10:53 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, AccessModeOwner, level)
|
|
|
|
|
2018-11-28 20:26:14 +09:00
|
|
|
level, err = AccessLevel(user2, repo3)
|
2017-01-08 12:10:53 +09:00
|
|
|
assert.NoError(t, err)
|
2018-11-28 20:26:14 +09:00
|
|
|
assert.Equal(t, AccessModeOwner, level)
|
2017-01-08 12:10:53 +09:00
|
|
|
|
2018-11-28 20:26:14 +09:00
|
|
|
level, err = AccessLevel(user5, repo1)
|
2017-01-08 12:10:53 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, AccessModeRead, level)
|
|
|
|
|
2018-11-28 20:26:14 +09:00
|
|
|
level, err = AccessLevel(user5, repo3)
|
2017-01-08 12:10:53 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, AccessModeNone, level)
|
2020-01-14 02:33:46 +09:00
|
|
|
|
|
|
|
// restricted user has no access to a public repo
|
|
|
|
level, err = AccessLevel(user29, repo1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, AccessModeNone, level)
|
|
|
|
|
|
|
|
// ... unless he's a collaborator
|
|
|
|
level, err = AccessLevel(user29, repo4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, AccessModeWrite, level)
|
|
|
|
|
|
|
|
// ... or a team member
|
|
|
|
level, err = AccessLevel(user29, repo24)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, AccessModeRead, level)
|
2017-01-08 12:10:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasAccess(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-08 12:10:53 +09:00
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
|
|
|
user2 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
|
2018-06-22 01:00:13 +09:00
|
|
|
// A public repository owned by User 2
|
2021-09-19 20:49:59 +09:00
|
|
|
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
2018-06-22 01:00:13 +09:00
|
|
|
assert.False(t, repo1.IsPrivate)
|
|
|
|
// A private repository owned by Org 3
|
2021-09-19 20:49:59 +09:00
|
|
|
repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
|
2018-06-22 01:00:13 +09:00
|
|
|
assert.True(t, repo2.IsPrivate)
|
2017-01-08 12:10:53 +09:00
|
|
|
|
2018-11-28 20:26:14 +09:00
|
|
|
has, err := HasAccess(user1.ID, repo1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, has)
|
2017-01-08 12:10:53 +09:00
|
|
|
|
2019-07-22 16:11:16 +09:00
|
|
|
_, err = HasAccess(user1.ID, repo2)
|
2018-11-28 20:26:14 +09:00
|
|
|
assert.NoError(t, err)
|
2017-01-08 12:10:53 +09:00
|
|
|
|
2019-07-22 16:11:16 +09:00
|
|
|
_, err = HasAccess(user2.ID, repo1)
|
2018-11-28 20:26:14 +09:00
|
|
|
assert.NoError(t, err)
|
2017-01-08 12:10:53 +09:00
|
|
|
|
2019-07-22 16:11:16 +09:00
|
|
|
_, err = HasAccess(user2.ID, repo2)
|
2018-11-28 20:26:14 +09:00
|
|
|
assert.NoError(t, err)
|
2017-01-08 12:10:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetRepositoryAccesses(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-08 12:10:53 +09:00
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
|
2017-01-08 12:10:53 +09:00
|
|
|
accesses, err := user1.GetRepositoryAccesses()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, accesses, 0)
|
2020-01-14 02:33:46 +09:00
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
|
2020-01-14 02:33:46 +09:00
|
|
|
accesses, err = user29.GetRepositoryAccesses()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, accesses, 2)
|
2017-01-08 12:10:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser_GetAccessibleRepositories(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-08 12:10:53 +09:00
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
|
2017-01-08 12:10:53 +09:00
|
|
|
repos, err := user1.GetAccessibleRepositories(0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, repos, 0)
|
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
2017-01-08 12:10:53 +09:00
|
|
|
repos, err = user2.GetAccessibleRepositories(0)
|
|
|
|
assert.NoError(t, err)
|
2020-02-15 13:29:06 +09:00
|
|
|
assert.Len(t, repos, 4)
|
2020-01-14 02:33:46 +09:00
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
|
2020-01-14 02:33:46 +09:00
|
|
|
repos, err = user29.GetAccessibleRepositories(0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, repos, 2)
|
2017-01-08 12:10:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_RecalculateAccesses(t *testing.T) {
|
|
|
|
// test with organization repo
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-09-19 20:49:59 +09:00
|
|
|
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
|
2017-01-08 12:10:53 +09:00
|
|
|
assert.NoError(t, repo1.GetOwner())
|
|
|
|
|
2021-09-24 00:45:36 +09:00
|
|
|
_, err := db.GetEngine(db.DefaultContext).Delete(&Collaboration{UserID: 2, RepoID: 3})
|
2017-01-08 12:10:53 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, repo1.RecalculateAccesses())
|
|
|
|
|
|
|
|
access := &Access{UserID: 2, RepoID: 3}
|
2021-09-24 00:45:36 +09:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Get(access)
|
2017-01-08 12:10:53 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, has)
|
2017-01-25 01:16:36 +09:00
|
|
|
assert.Equal(t, AccessModeOwner, access.Mode)
|
2017-01-08 12:10:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_RecalculateAccesses2(t *testing.T) {
|
|
|
|
// test with non-organization repo
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-09-19 20:49:59 +09:00
|
|
|
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
|
2017-01-08 12:10:53 +09:00
|
|
|
assert.NoError(t, repo1.GetOwner())
|
|
|
|
|
2021-09-24 00:45:36 +09:00
|
|
|
_, err := db.GetEngine(db.DefaultContext).Delete(&Collaboration{UserID: 4, RepoID: 4})
|
2017-01-08 12:10:53 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, repo1.RecalculateAccesses())
|
|
|
|
|
2021-09-24 00:45:36 +09:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Get(&Access{UserID: 4, RepoID: 4})
|
2017-01-08 12:10:53 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, has)
|
|
|
|
}
|
2020-01-14 02:33:46 +09:00
|
|
|
|
|
|
|
func TestRepository_RecalculateAccesses3(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-09-19 20:49:59 +09:00
|
|
|
team5 := db.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team)
|
|
|
|
user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
|
2020-01-14 02:33:46 +09:00
|
|
|
|
2021-09-24 00:45:36 +09:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Get(&Access{UserID: 29, RepoID: 23})
|
2020-01-14 02:33:46 +09: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-24 00:45:36 +09:00
|
|
|
has, err = db.GetEngine(db.DefaultContext).Get(&Access{UserID: 29, RepoID: 23})
|
2020-01-14 02:33:46 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, has)
|
|
|
|
}
|