mirror of https://github.com/go-gitea/gitea
Move tests as seperate sub packages to reduce duplicated file names (#19951)
parent
b4d420d865
commit
d91d4db344
@ -0,0 +1,56 @@ |
||||
// Copyright 2019 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 db_test |
||||
|
||||
import ( |
||||
"os" |
||||
"path/filepath" |
||||
"testing" |
||||
|
||||
"code.gitea.io/gitea/models/db" |
||||
issues_model "code.gitea.io/gitea/models/issues" |
||||
"code.gitea.io/gitea/models/unittest" |
||||
"code.gitea.io/gitea/modules/setting" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestDumpDatabase(t *testing.T) { |
||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||
|
||||
dir, err := os.MkdirTemp(os.TempDir(), "dump") |
||||
assert.NoError(t, err) |
||||
|
||||
type Version struct { |
||||
ID int64 `xorm:"pk autoincr"` |
||||
Version int64 |
||||
} |
||||
assert.NoError(t, db.GetEngine(db.DefaultContext).Sync2(new(Version))) |
||||
|
||||
for _, dbType := range setting.SupportedDatabaseTypes { |
||||
assert.NoError(t, db.DumpDatabase(filepath.Join(dir, dbType+".sql"), dbType)) |
||||
} |
||||
} |
||||
|
||||
func TestDeleteOrphanedObjects(t *testing.T) { |
||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||
|
||||
countBefore, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{}) |
||||
assert.NoError(t, err) |
||||
|
||||
_, err = db.GetEngine(db.DefaultContext).Insert(&issues_model.PullRequest{IssueID: 1000}, &issues_model.PullRequest{IssueID: 1001}, &issues_model.PullRequest{IssueID: 1003}) |
||||
assert.NoError(t, err) |
||||
|
||||
orphaned, err := db.CountOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id") |
||||
assert.NoError(t, err) |
||||
assert.EqualValues(t, 3, orphaned) |
||||
|
||||
err = db.DeleteOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id") |
||||
assert.NoError(t, err) |
||||
|
||||
countAfter, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{}) |
||||
assert.NoError(t, err) |
||||
assert.EqualValues(t, countBefore, countAfter) |
||||
} |
@ -0,0 +1,21 @@ |
||||
// Copyright 2020 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 db_test |
||||
|
||||
import ( |
||||
"path/filepath" |
||||
"testing" |
||||
|
||||
"code.gitea.io/gitea/models/unittest" |
||||
|
||||
_ "code.gitea.io/gitea/models" |
||||
_ "code.gitea.io/gitea/models/repo" |
||||
) |
||||
|
||||
func TestMain(m *testing.M) { |
||||
unittest.MainTest(m, &unittest.TestOptions{ |
||||
GiteaRootPath: filepath.Join("..", ".."), |
||||
}) |
||||
} |
@ -1,34 +0,0 @@ |
||||
// Copyright 2019 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 ( |
||||
"os" |
||||
"path/filepath" |
||||
"testing" |
||||
|
||||
"code.gitea.io/gitea/models/db" |
||||
"code.gitea.io/gitea/models/unittest" |
||||
"code.gitea.io/gitea/modules/setting" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestDumpDatabase(t *testing.T) { |
||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||
|
||||
dir, err := os.MkdirTemp(os.TempDir(), "dump") |
||||
assert.NoError(t, err) |
||||
|
||||
type Version struct { |
||||
ID int64 `xorm:"pk autoincr"` |
||||
Version int64 |
||||
} |
||||
assert.NoError(t, db.GetEngine(db.DefaultContext).Sync2(new(Version))) |
||||
|
||||
for _, dbType := range setting.SupportedDatabaseTypes { |
||||
assert.NoError(t, db.DumpDatabase(filepath.Join(dir, dbType+".sql"), dbType)) |
||||
} |
||||
} |
@ -1,266 +0,0 @@ |
||||
// Copyright 2018 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" |
||||
|
||||
"code.gitea.io/gitea/models/db" |
||||
"code.gitea.io/gitea/models/organization" |
||||
perm_model "code.gitea.io/gitea/models/perm" |
||||
access_model "code.gitea.io/gitea/models/perm/access" |
||||
repo_model "code.gitea.io/gitea/models/repo" |
||||
"code.gitea.io/gitea/models/unit" |
||||
"code.gitea.io/gitea/models/unittest" |
||||
user_model "code.gitea.io/gitea/models/user" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestRepoPermissionPublicNonOrgRepo(t *testing.T) { |
||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||
|
||||
// public non-organization repo
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}).(*repo_model.Repository) |
||||
assert.NoError(t, repo.LoadUnits(db.DefaultContext)) |
||||
|
||||
// plain user
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
||||
perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.False(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// change to collaborator
|
||||
assert.NoError(t, AddCollaborator(repo, user)) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// collaborator
|
||||
collaborator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, collaborator) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// owner
|
||||
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// admin
|
||||
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
} |
||||
|
||||
func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) { |
||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||
|
||||
// private non-organization repo
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository) |
||||
assert.NoError(t, repo.LoadUnits(db.DefaultContext)) |
||||
|
||||
// plain user
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User) |
||||
perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.False(t, perm.CanRead(unit.Type)) |
||||
assert.False(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// change to collaborator to default write access
|
||||
assert.NoError(t, AddCollaborator(repo, user)) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, user.ID, perm_model.AccessModeRead)) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.False(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// owner
|
||||
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// admin
|
||||
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
} |
||||
|
||||
func TestRepoPermissionPublicOrgRepo(t *testing.T) { |
||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||
|
||||
// public organization repo
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 32}).(*repo_model.Repository) |
||||
assert.NoError(t, repo.LoadUnits(db.DefaultContext)) |
||||
|
||||
// plain user
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User) |
||||
perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.False(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// change to collaborator to default write access
|
||||
assert.NoError(t, AddCollaborator(repo, user)) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, user.ID, perm_model.AccessModeRead)) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.False(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// org member team owner
|
||||
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// org member team tester
|
||||
member := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, member) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
} |
||||
assert.True(t, perm.CanWrite(unit.TypeIssues)) |
||||
assert.False(t, perm.CanWrite(unit.TypeCode)) |
||||
|
||||
// admin
|
||||
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
} |
||||
|
||||
func TestRepoPermissionPrivateOrgRepo(t *testing.T) { |
||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||
|
||||
// private organization repo
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 24}).(*repo_model.Repository) |
||||
assert.NoError(t, repo.LoadUnits(db.DefaultContext)) |
||||
|
||||
// plain user
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User) |
||||
perm, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.False(t, perm.CanRead(unit.Type)) |
||||
assert.False(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// change to collaborator to default write access
|
||||
assert.NoError(t, AddCollaborator(repo, user)) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
assert.NoError(t, repo_model.ChangeCollaborationAccessMode(repo, user.ID, perm_model.AccessModeRead)) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, user) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.False(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// org member team owner
|
||||
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 15}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// update team information and then check permission
|
||||
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 5}).(*organization.Team) |
||||
err = organization.UpdateTeamUnits(team, nil) |
||||
assert.NoError(t, err) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, owner) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
|
||||
// org member team tester
|
||||
tester := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, tester) |
||||
assert.NoError(t, err) |
||||
assert.True(t, perm.CanWrite(unit.TypeIssues)) |
||||
assert.False(t, perm.CanWrite(unit.TypeCode)) |
||||
assert.False(t, perm.CanRead(unit.TypeCode)) |
||||
|
||||
// org member team reviewer
|
||||
reviewer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 20}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, reviewer) |
||||
assert.NoError(t, err) |
||||
assert.False(t, perm.CanRead(unit.TypeIssues)) |
||||
assert.False(t, perm.CanWrite(unit.TypeCode)) |
||||
assert.True(t, perm.CanRead(unit.TypeCode)) |
||||
|
||||
// admin
|
||||
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) |
||||
perm, err = access_model.GetUserRepoPermission(db.DefaultContext, repo, admin) |
||||
assert.NoError(t, err) |
||||
for _, unit := range repo.Units { |
||||
assert.True(t, perm.CanRead(unit.Type)) |
||||
assert.True(t, perm.CanWrite(unit.Type)) |
||||
} |
||||
} |
@ -1,43 +0,0 @@ |
||||
// 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" |
||||
|
||||
"code.gitea.io/gitea/models/unittest" |
||||
user_model "code.gitea.io/gitea/models/user" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestFollowUser(t *testing.T) { |
||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||
|
||||
testSuccess := func(followerID, followedID int64) { |
||||
assert.NoError(t, user_model.FollowUser(followerID, followedID)) |
||||
unittest.AssertExistsAndLoadBean(t, &user_model.Follow{UserID: followerID, FollowID: followedID}) |
||||
} |
||||
testSuccess(4, 2) |
||||
testSuccess(5, 2) |
||||
|
||||
assert.NoError(t, user_model.FollowUser(2, 2)) |
||||
|
||||
unittest.CheckConsistencyFor(t, &user_model.User{}) |
||||
} |
||||
|
||||
func TestUnfollowUser(t *testing.T) { |
||||
assert.NoError(t, unittest.PrepareTestDatabase()) |
||||
|
||||
testSuccess := func(followerID, followedID int64) { |
||||
assert.NoError(t, user_model.UnfollowUser(followerID, followedID)) |
||||
unittest.AssertNotExistsBean(t, &user_model.Follow{UserID: followerID, FollowID: followedID}) |
||||
} |
||||
testSuccess(4, 2) |
||||
testSuccess(5, 2) |
||||
testSuccess(2, 2) |
||||
|
||||
unittest.CheckConsistencyFor(t, &user_model.User{}) |
||||
} |
Loading…
Reference in new issue