mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-14 02:24:03 +00:00
[TESTS] add unit test for repository collaboration (#825)
- Add a few extra test cases and test functions for the collaboration model to get everything covered by tests (except for error handling, as we cannot suddenly mock errors from the database). ``` -> % go tool cover -func=coverage.out | grep "code.gitea.io/gitea/models/repo/collaboration.go" ``` Before: ``` code.gitea.io/gitea/models/repo/collaboration.go:28: init 100.0% code.gitea.io/gitea/models/repo/collaboration.go:39: GetCollaborators 61.5% code.gitea.io/gitea/models/repo/collaboration.go:65: CountCollaborators 0.0% code.gitea.io/gitea/models/repo/collaboration.go:70: GetCollaboration 0.0% code.gitea.io/gitea/models/repo/collaboration.go:83: IsCollaborator 100.0% code.gitea.io/gitea/models/repo/collaboration.go:87: getCollaborations 42.9% code.gitea.io/gitea/models/repo/collaboration.go:102: ChangeCollaborationAccessMode 77.8% code.gitea.io/gitea/models/repo/collaboration.go:141: IsOwnerMemberCollaborator 0.0% ``` After: ``` code.gitea.io/gitea/models/repo/collaboration.go:28: init 100.0% code.gitea.io/gitea/models/repo/collaboration.go:39: GetCollaborators 61.5% code.gitea.io/gitea/models/repo/collaboration.go:65: CountCollaborators 100.0% code.gitea.io/gitea/models/repo/collaboration.go:70: GetCollaboration 100.0% code.gitea.io/gitea/models/repo/collaboration.go:83: IsCollaborator 100.0% code.gitea.io/gitea/models/repo/collaboration.go:87: getCollaborations 100.0% code.gitea.io/gitea/models/repo/collaboration.go:102: ChangeCollaborationAccessMode 83.3% code.gitea.io/gitea/models/repo/collaboration.go:141: IsOwnerMemberCollaborator 87.5% ``` Co-authored-by: Gusted <postmaster@gusted.xyz> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/825 (cherry picked from commit9de2b618a4
) (cherry picked from commite3a6ca67e4
) (cherry picked from commitae57e1b6d7
) (cherry picked from commit5e37ea475d
)
This commit is contained in:
parent
c39ae0bf8a
commit
ef9494ddef
|
@ -33,6 +33,19 @@ func TestRepository_GetCollaborators(t *testing.T) {
|
||||||
test(2)
|
test(2)
|
||||||
test(3)
|
test(3)
|
||||||
test(4)
|
test(4)
|
||||||
|
|
||||||
|
// Test db.ListOptions
|
||||||
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 22})
|
||||||
|
|
||||||
|
collaborators1, err := repo_model.GetCollaborators(db.DefaultContext, repo.ID, db.ListOptions{PageSize: 1, Page: 1})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, collaborators1, 1)
|
||||||
|
|
||||||
|
collaborators2, err := repo_model.GetCollaborators(db.DefaultContext, repo.ID, db.ListOptions{PageSize: 1, Page: 2})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, collaborators2, 1)
|
||||||
|
|
||||||
|
assert.NotEqualValues(t, collaborators1[0].ID, collaborators2[0].ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRepository_IsCollaborator(t *testing.T) {
|
func TestRepository_IsCollaborator(t *testing.T) {
|
||||||
|
@ -66,5 +79,80 @@ func TestRepository_ChangeCollaborationAccessMode(t *testing.T) {
|
||||||
|
|
||||||
assert.NoError(t, repo_model.ChangeCollaborationAccessMode(db.DefaultContext, repo, unittest.NonexistentID, perm.AccessModeAdmin))
|
assert.NoError(t, repo_model.ChangeCollaborationAccessMode(db.DefaultContext, repo, unittest.NonexistentID, perm.AccessModeAdmin))
|
||||||
|
|
||||||
|
// Disvard invalid input.
|
||||||
|
assert.NoError(t, repo_model.ChangeCollaborationAccessMode(db.DefaultContext, repo, 4, perm.AccessMode(unittest.NonexistentID)))
|
||||||
|
|
||||||
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repo.ID})
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repo.ID})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRepository_CountCollaborators(t *testing.T) {
|
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
|
||||||
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
|
||||||
|
count, err := repo_model.CountCollaborators(repo1.ID)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 2, count)
|
||||||
|
|
||||||
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 22})
|
||||||
|
count, err = repo_model.CountCollaborators(repo2.ID)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 2, count)
|
||||||
|
|
||||||
|
// Non-existent repository.
|
||||||
|
count, err = repo_model.CountCollaborators(unittest.NonexistentID)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 0, count)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRepository_IsOwnerMemberCollaborator(t *testing.T) {
|
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
|
||||||
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
||||||
|
|
||||||
|
// Organisation owner.
|
||||||
|
actual, err := repo_model.IsOwnerMemberCollaborator(repo1, 2)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, actual)
|
||||||
|
|
||||||
|
// Team member.
|
||||||
|
actual, err = repo_model.IsOwnerMemberCollaborator(repo1, 4)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, actual)
|
||||||
|
|
||||||
|
// Normal user.
|
||||||
|
actual, err = repo_model.IsOwnerMemberCollaborator(repo1, 1)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.False(t, actual)
|
||||||
|
|
||||||
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
|
||||||
|
|
||||||
|
// Collaborator.
|
||||||
|
actual, err = repo_model.IsOwnerMemberCollaborator(repo2, 4)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, actual)
|
||||||
|
|
||||||
|
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 15})
|
||||||
|
|
||||||
|
// Repository owner.
|
||||||
|
actual, err = repo_model.IsOwnerMemberCollaborator(repo3, 2)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRepo_GetCollaboration(t *testing.T) {
|
||||||
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
|
||||||
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
|
||||||
|
|
||||||
|
// Existing collaboration.
|
||||||
|
collab, err := repo_model.GetCollaboration(db.DefaultContext, repo.ID, 4)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, collab)
|
||||||
|
assert.EqualValues(t, 4, collab.UserID)
|
||||||
|
assert.EqualValues(t, 4, collab.RepoID)
|
||||||
|
|
||||||
|
// Non-existing collaboration.
|
||||||
|
collab, err = repo_model.GetCollaboration(db.DefaultContext, repo.ID, 1)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Nil(t, collab)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue