You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
4.5 KiB
127 lines
4.5 KiB
8 years ago
|
// 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.
|
||
|
|
||
3 years ago
|
package access
|
||
8 years ago
|
|
||
|
import (
|
||
|
"testing"
|
||
8 years ago
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
"code.gitea.io/gitea/models/perm"
|
||
3 years ago
|
repo_model "code.gitea.io/gitea/models/repo"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
3 years ago
|
|
||
8 years ago
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestAccessLevel(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
8 years ago
|
|
||
3 years ago
|
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)
|
||
7 years ago
|
// A public repository owned by User 2
|
||
3 years ago
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
||
7 years ago
|
assert.False(t, repo1.IsPrivate)
|
||
|
// A private repository owned by Org 3
|
||
3 years ago
|
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
||
6 years ago
|
assert.True(t, repo3.IsPrivate)
|
||
8 years ago
|
|
||
5 years ago
|
// Another public repository
|
||
3 years ago
|
repo4 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}).(*repo_model.Repository)
|
||
5 years ago
|
assert.False(t, repo4.IsPrivate)
|
||
|
// org. owned private repo
|
||
3 years ago
|
repo24 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 24}).(*repo_model.Repository)
|
||
5 years ago
|
|
||
6 years ago
|
level, err := AccessLevel(user2, repo1)
|
||
8 years ago
|
assert.NoError(t, err)
|
||
3 years ago
|
assert.Equal(t, perm.AccessModeOwner, level)
|
||
8 years ago
|
|
||
6 years ago
|
level, err = AccessLevel(user2, repo3)
|
||
8 years ago
|
assert.NoError(t, err)
|
||
3 years ago
|
assert.Equal(t, perm.AccessModeOwner, level)
|
||
8 years ago
|
|
||
6 years ago
|
level, err = AccessLevel(user5, repo1)
|
||
8 years ago
|
assert.NoError(t, err)
|
||
3 years ago
|
assert.Equal(t, perm.AccessModeRead, level)
|
||
8 years ago
|
|
||
6 years ago
|
level, err = AccessLevel(user5, repo3)
|
||
8 years ago
|
assert.NoError(t, err)
|
||
3 years ago
|
assert.Equal(t, perm.AccessModeNone, level)
|
||
5 years ago
|
|
||
|
// restricted user has no access to a public repo
|
||
|
level, err = AccessLevel(user29, repo1)
|
||
|
assert.NoError(t, err)
|
||
3 years ago
|
assert.Equal(t, perm.AccessModeNone, level)
|
||
5 years ago
|
|
||
|
// ... unless he's a collaborator
|
||
|
level, err = AccessLevel(user29, repo4)
|
||
|
assert.NoError(t, err)
|
||
3 years ago
|
assert.Equal(t, perm.AccessModeWrite, level)
|
||
5 years ago
|
|
||
|
// ... or a team member
|
||
|
level, err = AccessLevel(user29, repo24)
|
||
|
assert.NoError(t, err)
|
||
3 years ago
|
assert.Equal(t, perm.AccessModeRead, level)
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestHasAccess(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
8 years ago
|
|
||
3 years ago
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
||
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
||
7 years ago
|
// A public repository owned by User 2
|
||
3 years ago
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
||
7 years ago
|
assert.False(t, repo1.IsPrivate)
|
||
|
// A private repository owned by Org 3
|
||
3 years ago
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
||
7 years ago
|
assert.True(t, repo2.IsPrivate)
|
||
8 years ago
|
|
||
3 years ago
|
has, err := HasAccess(db.DefaultContext, user1.ID, repo1)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
|
assert.True(t, has)
|
||
8 years ago
|
|
||
3 years ago
|
_, err = HasAccess(db.DefaultContext, user1.ID, repo2)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
8 years ago
|
|
||
3 years ago
|
_, err = HasAccess(db.DefaultContext, user2.ID, repo1)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
8 years ago
|
|
||
3 years ago
|
_, err = HasAccess(db.DefaultContext, user2.ID, repo2)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestRepository_RecalculateAccesses(t *testing.T) {
|
||
|
// test with organization repo
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
3 years ago
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
||
|
assert.NoError(t, repo1.GetOwner(db.DefaultContext))
|
||
8 years ago
|
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).Delete(&repo_model.Collaboration{UserID: 2, RepoID: 3})
|
||
8 years ago
|
assert.NoError(t, err)
|
||
3 years ago
|
assert.NoError(t, RecalculateAccesses(db.DefaultContext, repo1))
|
||
8 years ago
|
|
||
|
access := &Access{UserID: 2, RepoID: 3}
|
||
3 years ago
|
has, err := db.GetEngine(db.DefaultContext).Get(access)
|
||
8 years ago
|
assert.NoError(t, err)
|
||
|
assert.True(t, has)
|
||
3 years ago
|
assert.Equal(t, perm.AccessModeOwner, access.Mode)
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestRepository_RecalculateAccesses2(t *testing.T) {
|
||
|
// test with non-organization repo
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
3 years ago
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}).(*repo_model.Repository)
|
||
|
assert.NoError(t, repo1.GetOwner(db.DefaultContext))
|
||
8 years ago
|
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).Delete(&repo_model.Collaboration{UserID: 4, RepoID: 4})
|
||
8 years ago
|
assert.NoError(t, err)
|
||
3 years ago
|
assert.NoError(t, RecalculateAccesses(db.DefaultContext, repo1))
|
||
8 years ago
|
|
||
3 years ago
|
has, err := db.GetEngine(db.DefaultContext).Get(&Access{UserID: 4, RepoID: 4})
|
||
8 years ago
|
assert.NoError(t, err)
|
||
|
assert.False(t, has)
|
||
|
}
|