code.gitea.io/gitea@v1.21.7/models/repo/collaboration_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	"code.gitea.io/gitea/models/perm"
    11  	access_model "code.gitea.io/gitea/models/perm/access"
    12  	repo_model "code.gitea.io/gitea/models/repo"
    13  	"code.gitea.io/gitea/models/unittest"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestRepository_GetCollaborators(t *testing.T) {
    19  	assert.NoError(t, unittest.PrepareTestDatabase())
    20  	test := func(repoID int64) {
    21  		repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID})
    22  		collaborators, err := repo_model.GetCollaborators(db.DefaultContext, repo.ID, db.ListOptions{})
    23  		assert.NoError(t, err)
    24  		expectedLen, err := db.GetEngine(db.DefaultContext).Count(&repo_model.Collaboration{RepoID: repoID})
    25  		assert.NoError(t, err)
    26  		assert.Len(t, collaborators, int(expectedLen))
    27  		for _, collaborator := range collaborators {
    28  			assert.EqualValues(t, collaborator.User.ID, collaborator.Collaboration.UserID)
    29  			assert.EqualValues(t, repoID, collaborator.Collaboration.RepoID)
    30  		}
    31  	}
    32  	test(1)
    33  	test(2)
    34  	test(3)
    35  	test(4)
    36  
    37  	// Test db.ListOptions
    38  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 22})
    39  
    40  	collaborators1, err := repo_model.GetCollaborators(db.DefaultContext, repo.ID, db.ListOptions{PageSize: 1, Page: 1})
    41  	assert.NoError(t, err)
    42  	assert.Len(t, collaborators1, 1)
    43  
    44  	collaborators2, err := repo_model.GetCollaborators(db.DefaultContext, repo.ID, db.ListOptions{PageSize: 1, Page: 2})
    45  	assert.NoError(t, err)
    46  	assert.Len(t, collaborators2, 1)
    47  
    48  	assert.NotEqualValues(t, collaborators1[0].ID, collaborators2[0].ID)
    49  }
    50  
    51  func TestRepository_IsCollaborator(t *testing.T) {
    52  	assert.NoError(t, unittest.PrepareTestDatabase())
    53  
    54  	test := func(repoID, userID int64, expected bool) {
    55  		repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID})
    56  		actual, err := repo_model.IsCollaborator(db.DefaultContext, repo.ID, userID)
    57  		assert.NoError(t, err)
    58  		assert.Equal(t, expected, actual)
    59  	}
    60  	test(3, 2, true)
    61  	test(3, unittest.NonexistentID, false)
    62  	test(4, 2, false)
    63  	test(4, 4, true)
    64  }
    65  
    66  func TestRepository_ChangeCollaborationAccessMode(t *testing.T) {
    67  	assert.NoError(t, unittest.PrepareTestDatabase())
    68  
    69  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
    70  	assert.NoError(t, repo_model.ChangeCollaborationAccessMode(db.DefaultContext, repo, 4, perm.AccessModeAdmin))
    71  
    72  	collaboration := unittest.AssertExistsAndLoadBean(t, &repo_model.Collaboration{RepoID: repo.ID, UserID: 4})
    73  	assert.EqualValues(t, perm.AccessModeAdmin, collaboration.Mode)
    74  
    75  	access := unittest.AssertExistsAndLoadBean(t, &access_model.Access{UserID: 4, RepoID: repo.ID})
    76  	assert.EqualValues(t, perm.AccessModeAdmin, access.Mode)
    77  
    78  	assert.NoError(t, repo_model.ChangeCollaborationAccessMode(db.DefaultContext, repo, 4, perm.AccessModeAdmin))
    79  
    80  	assert.NoError(t, repo_model.ChangeCollaborationAccessMode(db.DefaultContext, repo, unittest.NonexistentID, perm.AccessModeAdmin))
    81  
    82  	// Disvard invalid input.
    83  	assert.NoError(t, repo_model.ChangeCollaborationAccessMode(db.DefaultContext, repo, 4, perm.AccessMode(unittest.NonexistentID)))
    84  
    85  	unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repo.ID})
    86  }
    87  
    88  func TestRepository_CountCollaborators(t *testing.T) {
    89  	assert.NoError(t, unittest.PrepareTestDatabase())
    90  
    91  	repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
    92  	count, err := repo_model.CountCollaborators(db.DefaultContext, repo1.ID)
    93  	assert.NoError(t, err)
    94  	assert.EqualValues(t, 2, count)
    95  
    96  	repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 22})
    97  	count, err = repo_model.CountCollaborators(db.DefaultContext, repo2.ID)
    98  	assert.NoError(t, err)
    99  	assert.EqualValues(t, 2, count)
   100  
   101  	// Non-existent repository.
   102  	count, err = repo_model.CountCollaborators(db.DefaultContext, unittest.NonexistentID)
   103  	assert.NoError(t, err)
   104  	assert.EqualValues(t, 0, count)
   105  }
   106  
   107  func TestRepository_IsOwnerMemberCollaborator(t *testing.T) {
   108  	assert.NoError(t, unittest.PrepareTestDatabase())
   109  
   110  	repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
   111  
   112  	// Organisation owner.
   113  	actual, err := repo_model.IsOwnerMemberCollaborator(db.DefaultContext, repo1, 2)
   114  	assert.NoError(t, err)
   115  	assert.True(t, actual)
   116  
   117  	// Team member.
   118  	actual, err = repo_model.IsOwnerMemberCollaborator(db.DefaultContext, repo1, 4)
   119  	assert.NoError(t, err)
   120  	assert.True(t, actual)
   121  
   122  	// Normal user.
   123  	actual, err = repo_model.IsOwnerMemberCollaborator(db.DefaultContext, repo1, 1)
   124  	assert.NoError(t, err)
   125  	assert.False(t, actual)
   126  
   127  	repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
   128  
   129  	// Collaborator.
   130  	actual, err = repo_model.IsOwnerMemberCollaborator(db.DefaultContext, repo2, 4)
   131  	assert.NoError(t, err)
   132  	assert.True(t, actual)
   133  
   134  	repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 15})
   135  
   136  	// Repository owner.
   137  	actual, err = repo_model.IsOwnerMemberCollaborator(db.DefaultContext, repo3, 2)
   138  	assert.NoError(t, err)
   139  	assert.True(t, actual)
   140  }
   141  
   142  func TestRepo_GetCollaboration(t *testing.T) {
   143  	assert.NoError(t, unittest.PrepareTestDatabase())
   144  
   145  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
   146  
   147  	// Existing collaboration.
   148  	collab, err := repo_model.GetCollaboration(db.DefaultContext, repo.ID, 4)
   149  	assert.NoError(t, err)
   150  	assert.NotNil(t, collab)
   151  	assert.EqualValues(t, 4, collab.UserID)
   152  	assert.EqualValues(t, 4, collab.RepoID)
   153  
   154  	// Non-existing collaboration.
   155  	collab, err = repo_model.GetCollaboration(db.DefaultContext, repo.ID, 1)
   156  	assert.NoError(t, err)
   157  	assert.Nil(t, collab)
   158  }