aboutsummaryrefslogtreecommitdiffstats
path: root/models/access_test.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-09-19 19:49:59 +0800
committerGitHub <noreply@github.com>2021-09-19 19:49:59 +0800
commita4bfef265d9e512830350635a0489c2cdcd6508f (patch)
tree1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/access_test.go
parent462306e263db5a809dbe2cdf62e99307aeff28de (diff)
downloadgitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz
gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db * Fix lint * Fix lint * Fix test * Fix lint * Fix lint * revert unnecessary change * Fix test * Fix wrong replace string * Use *Context * Correct committer spelling and fix wrong replaced words Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/access_test.go')
-rw-r--r--models/access_test.go67
1 files changed, 34 insertions, 33 deletions
diff --git a/models/access_test.go b/models/access_test.go
index c134ab98d8..875b2a0c1a 100644
--- a/models/access_test.go
+++ b/models/access_test.go
@@ -7,27 +7,28 @@ package models
import (
"testing"
+ "code.gitea.io/gitea/models/db"
"github.com/stretchr/testify/assert"
)
func TestAccessLevel(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
- user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
- user5 := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
- user29 := AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
+ user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
+ user5 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
+ user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
// A public repository owned by User 2
- repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
+ repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
assert.False(t, repo1.IsPrivate)
// A private repository owned by Org 3
- repo3 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
+ repo3 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
assert.True(t, repo3.IsPrivate)
// Another public repository
- repo4 := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
+ repo4 := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
assert.False(t, repo4.IsPrivate)
// org. owned private repo
- repo24 := AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository)
+ repo24 := db.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository)
level, err := AccessLevel(user2, repo1)
assert.NoError(t, err)
@@ -62,15 +63,15 @@ func TestAccessLevel(t *testing.T) {
}
func TestHasAccess(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
- user1 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
- user2 := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
+ user1 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
+ user2 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
// A public repository owned by User 2
- repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
+ repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
assert.False(t, repo1.IsPrivate)
// A private repository owned by Org 3
- repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
+ repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
assert.True(t, repo2.IsPrivate)
has, err := HasAccess(user1.ID, repo1)
@@ -88,33 +89,33 @@ func TestHasAccess(t *testing.T) {
}
func TestUser_GetRepositoryAccesses(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
- user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
+ user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
accesses, err := user1.GetRepositoryAccesses()
assert.NoError(t, err)
assert.Len(t, accesses, 0)
- user29 := AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
+ user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
accesses, err = user29.GetRepositoryAccesses()
assert.NoError(t, err)
assert.Len(t, accesses, 2)
}
func TestUser_GetAccessibleRepositories(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
- user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
+ user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
repos, err := user1.GetAccessibleRepositories(0)
assert.NoError(t, err)
assert.Len(t, repos, 0)
- user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
+ user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
repos, err = user2.GetAccessibleRepositories(0)
assert.NoError(t, err)
assert.Len(t, repos, 4)
- user29 := AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
+ user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
repos, err = user29.GetAccessibleRepositories(0)
assert.NoError(t, err)
assert.Len(t, repos, 2)
@@ -122,16 +123,16 @@ func TestUser_GetAccessibleRepositories(t *testing.T) {
func TestRepository_RecalculateAccesses(t *testing.T) {
// test with organization repo
- assert.NoError(t, PrepareTestDatabase())
- repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
+ assert.NoError(t, db.PrepareTestDatabase())
+ repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
assert.NoError(t, repo1.GetOwner())
- _, err := x.Delete(&Collaboration{UserID: 2, RepoID: 3})
+ _, err := db.DefaultContext().Engine().Delete(&Collaboration{UserID: 2, RepoID: 3})
assert.NoError(t, err)
assert.NoError(t, repo1.RecalculateAccesses())
access := &Access{UserID: 2, RepoID: 3}
- has, err := x.Get(access)
+ has, err := db.DefaultContext().Engine().Get(access)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, AccessModeOwner, access.Mode)
@@ -139,25 +140,25 @@ func TestRepository_RecalculateAccesses(t *testing.T) {
func TestRepository_RecalculateAccesses2(t *testing.T) {
// test with non-organization repo
- assert.NoError(t, PrepareTestDatabase())
- repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
+ assert.NoError(t, db.PrepareTestDatabase())
+ repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
assert.NoError(t, repo1.GetOwner())
- _, err := x.Delete(&Collaboration{UserID: 4, RepoID: 4})
+ _, err := db.DefaultContext().Engine().Delete(&Collaboration{UserID: 4, RepoID: 4})
assert.NoError(t, err)
assert.NoError(t, repo1.RecalculateAccesses())
- has, err := x.Get(&Access{UserID: 4, RepoID: 4})
+ has, err := db.DefaultContext().Engine().Get(&Access{UserID: 4, RepoID: 4})
assert.NoError(t, err)
assert.False(t, has)
}
func TestRepository_RecalculateAccesses3(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
- team5 := AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team)
- user29 := AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
+ assert.NoError(t, db.PrepareTestDatabase())
+ team5 := db.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team)
+ user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
- has, err := x.Get(&Access{UserID: 29, RepoID: 23})
+ has, err := db.DefaultContext().Engine().Get(&Access{UserID: 29, RepoID: 23})
assert.NoError(t, err)
assert.False(t, has)
@@ -165,7 +166,7 @@ func TestRepository_RecalculateAccesses3(t *testing.T) {
// even though repo 23 is public
assert.NoError(t, AddTeamMember(team5, user29.ID))
- has, err = x.Get(&Access{UserID: 29, RepoID: 23})
+ has, err = db.DefaultContext().Engine().Get(&Access{UserID: 29, RepoID: 23})
assert.NoError(t, err)
assert.True(t, has)
}