2017-01-08 08:40:53 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-06-15 12:32:00 +05:30
|
|
|
package access_test
|
2017-01-08 08:40:53 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-01-25 08:19:51 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-15 12:32:00 +05:30
|
|
|
perm_model "code.gitea.io/gitea/models/perm"
|
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 18:04:35 +05:30
|
|
|
|
2017-01-08 08:40:53 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccessLevel(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
user5 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
|
|
|
|
user29 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 29})
|
2018-06-21 21:30:13 +05:30
|
|
|
// A public repository owned by User 2
|
2022-08-16 07:52:25 +05:30
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2018-06-21 21:30:13 +05:30
|
|
|
assert.False(t, repo1.IsPrivate)
|
|
|
|
// A private repository owned by Org 3
|
2022-08-16 07:52:25 +05:30
|
|
|
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
2018-11-28 16:56:14 +05:30
|
|
|
assert.True(t, repo3.IsPrivate)
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2020-01-13 23:03:46 +05:30
|
|
|
// Another public repository
|
2022-08-16 07:52:25 +05:30
|
|
|
repo4 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
|
2020-01-13 23:03:46 +05:30
|
|
|
assert.False(t, repo4.IsPrivate)
|
|
|
|
// org. owned private repo
|
2022-08-16 07:52:25 +05:30
|
|
|
repo24 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 24})
|
2020-01-13 23:03:46 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
level, err := access_model.AccessLevel(db.DefaultContext, user2, repo1)
|
2017-01-08 08:40:53 +05:30
|
|
|
assert.NoError(t, err)
|
2022-06-15 12:32:00 +05:30
|
|
|
assert.Equal(t, perm_model.AccessModeOwner, level)
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
level, err = access_model.AccessLevel(db.DefaultContext, user2, repo3)
|
2017-01-08 08:40:53 +05:30
|
|
|
assert.NoError(t, err)
|
2022-06-15 12:32:00 +05:30
|
|
|
assert.Equal(t, perm_model.AccessModeOwner, level)
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
level, err = access_model.AccessLevel(db.DefaultContext, user5, repo1)
|
2017-01-08 08:40:53 +05:30
|
|
|
assert.NoError(t, err)
|
2022-06-15 12:32:00 +05:30
|
|
|
assert.Equal(t, perm_model.AccessModeRead, level)
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
level, err = access_model.AccessLevel(db.DefaultContext, user5, repo3)
|
2017-01-08 08:40:53 +05:30
|
|
|
assert.NoError(t, err)
|
2022-06-15 12:32:00 +05:30
|
|
|
assert.Equal(t, perm_model.AccessModeNone, level)
|
2020-01-13 23:03:46 +05:30
|
|
|
|
|
|
|
// restricted user has no access to a public repo
|
2022-11-19 13:42:33 +05:30
|
|
|
level, err = access_model.AccessLevel(db.DefaultContext, user29, repo1)
|
2020-01-13 23:03:46 +05:30
|
|
|
assert.NoError(t, err)
|
2022-06-15 12:32:00 +05:30
|
|
|
assert.Equal(t, perm_model.AccessModeNone, level)
|
2020-01-13 23:03:46 +05:30
|
|
|
|
|
|
|
// ... unless he's a collaborator
|
2022-11-19 13:42:33 +05:30
|
|
|
level, err = access_model.AccessLevel(db.DefaultContext, user29, repo4)
|
2020-01-13 23:03:46 +05:30
|
|
|
assert.NoError(t, err)
|
2022-06-15 12:32:00 +05:30
|
|
|
assert.Equal(t, perm_model.AccessModeWrite, level)
|
2020-01-13 23:03:46 +05:30
|
|
|
|
|
|
|
// ... or a team member
|
2022-11-19 13:42:33 +05:30
|
|
|
level, err = access_model.AccessLevel(db.DefaultContext, user29, repo24)
|
2020-01-13 23:03:46 +05:30
|
|
|
assert.NoError(t, err)
|
2022-06-15 12:32:00 +05:30
|
|
|
assert.Equal(t, perm_model.AccessModeRead, level)
|
2017-01-08 08:40:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasAccess(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
|
2018-06-21 21:30:13 +05:30
|
|
|
// A public repository owned by User 2
|
2022-08-16 07:52:25 +05:30
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2018-06-21 21:30:13 +05:30
|
|
|
assert.False(t, repo1.IsPrivate)
|
|
|
|
// A private repository owned by Org 3
|
2022-08-16 07:52:25 +05:30
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
2018-06-21 21:30:13 +05:30
|
|
|
assert.True(t, repo2.IsPrivate)
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-06-15 12:32:00 +05:30
|
|
|
has, err := access_model.HasAccess(db.DefaultContext, user1.ID, repo1)
|
2018-11-28 16:56:14 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, has)
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-06-15 12:32:00 +05:30
|
|
|
_, err = access_model.HasAccess(db.DefaultContext, user1.ID, repo2)
|
2018-11-28 16:56:14 +05:30
|
|
|
assert.NoError(t, err)
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-06-15 12:32:00 +05:30
|
|
|
_, err = access_model.HasAccess(db.DefaultContext, user2.ID, repo1)
|
2018-11-28 16:56:14 +05:30
|
|
|
assert.NoError(t, err)
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-06-15 12:32:00 +05:30
|
|
|
_, err = access_model.HasAccess(db.DefaultContext, user2.ID, repo2)
|
2018-11-28 16:56:14 +05:30
|
|
|
assert.NoError(t, err)
|
2017-01-08 08:40:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_RecalculateAccesses(t *testing.T) {
|
|
|
|
// test with organization repo
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 07:52:25 +05:30
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
2023-02-18 17:41:03 +05:30
|
|
|
assert.NoError(t, repo1.LoadOwner(db.DefaultContext))
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-05-11 15:39:36 +05:30
|
|
|
_, err := db.GetEngine(db.DefaultContext).Delete(&repo_model.Collaboration{UserID: 2, RepoID: 3})
|
2017-01-08 08:40:53 +05:30
|
|
|
assert.NoError(t, err)
|
2022-06-15 12:32:00 +05:30
|
|
|
assert.NoError(t, access_model.RecalculateAccesses(db.DefaultContext, repo1))
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-06-15 12:32:00 +05:30
|
|
|
access := &access_model.Access{UserID: 2, RepoID: 3}
|
2021-09-23 21:15:36 +05:30
|
|
|
has, err := db.GetEngine(db.DefaultContext).Get(access)
|
2017-01-08 08:40:53 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, has)
|
2022-06-15 12:32:00 +05:30
|
|
|
assert.Equal(t, perm_model.AccessModeOwner, access.Mode)
|
2017-01-08 08:40:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_RecalculateAccesses2(t *testing.T) {
|
|
|
|
// test with non-organization repo
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 07:52:25 +05:30
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
|
2023-02-18 17:41:03 +05:30
|
|
|
assert.NoError(t, repo1.LoadOwner(db.DefaultContext))
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-05-11 15:39:36 +05:30
|
|
|
_, err := db.GetEngine(db.DefaultContext).Delete(&repo_model.Collaboration{UserID: 4, RepoID: 4})
|
2017-01-08 08:40:53 +05:30
|
|
|
assert.NoError(t, err)
|
2022-06-15 12:32:00 +05:30
|
|
|
assert.NoError(t, access_model.RecalculateAccesses(db.DefaultContext, repo1))
|
2017-01-08 08:40:53 +05:30
|
|
|
|
2022-06-15 12:32:00 +05:30
|
|
|
has, err := db.GetEngine(db.DefaultContext).Get(&access_model.Access{UserID: 4, RepoID: 4})
|
2017-01-08 08:40:53 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, has)
|
|
|
|
}
|