code.gitea.io/gitea@v1.21.7/models/organization/team_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package organization_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	"code.gitea.io/gitea/models/organization"
    11  	"code.gitea.io/gitea/models/unittest"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestTeam_IsOwnerTeam(t *testing.T) {
    17  	assert.NoError(t, unittest.PrepareTestDatabase())
    18  
    19  	team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1})
    20  	assert.True(t, team.IsOwnerTeam())
    21  
    22  	team = unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2})
    23  	assert.False(t, team.IsOwnerTeam())
    24  }
    25  
    26  func TestTeam_IsMember(t *testing.T) {
    27  	assert.NoError(t, unittest.PrepareTestDatabase())
    28  
    29  	team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1})
    30  	assert.True(t, team.IsMember(2))
    31  	assert.False(t, team.IsMember(4))
    32  	assert.False(t, team.IsMember(unittest.NonexistentID))
    33  
    34  	team = unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2})
    35  	assert.True(t, team.IsMember(2))
    36  	assert.True(t, team.IsMember(4))
    37  	assert.False(t, team.IsMember(unittest.NonexistentID))
    38  }
    39  
    40  func TestTeam_GetRepositories(t *testing.T) {
    41  	assert.NoError(t, unittest.PrepareTestDatabase())
    42  
    43  	test := func(teamID int64) {
    44  		team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
    45  		assert.NoError(t, team.LoadRepositories(db.DefaultContext))
    46  		assert.Len(t, team.Repos, team.NumRepos)
    47  		for _, repo := range team.Repos {
    48  			unittest.AssertExistsAndLoadBean(t, &organization.TeamRepo{TeamID: teamID, RepoID: repo.ID})
    49  		}
    50  	}
    51  	test(1)
    52  	test(3)
    53  }
    54  
    55  func TestTeam_GetMembers(t *testing.T) {
    56  	assert.NoError(t, unittest.PrepareTestDatabase())
    57  
    58  	test := func(teamID int64) {
    59  		team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
    60  		assert.NoError(t, team.LoadMembers(db.DefaultContext))
    61  		assert.Len(t, team.Members, team.NumMembers)
    62  		for _, member := range team.Members {
    63  			unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{UID: member.ID, TeamID: teamID})
    64  		}
    65  	}
    66  	test(1)
    67  	test(3)
    68  }
    69  
    70  func TestGetTeam(t *testing.T) {
    71  	assert.NoError(t, unittest.PrepareTestDatabase())
    72  
    73  	testSuccess := func(orgID int64, name string) {
    74  		team, err := organization.GetTeam(db.DefaultContext, orgID, name)
    75  		assert.NoError(t, err)
    76  		assert.EqualValues(t, orgID, team.OrgID)
    77  		assert.Equal(t, name, team.Name)
    78  	}
    79  	testSuccess(3, "Owners")
    80  	testSuccess(3, "team1")
    81  
    82  	_, err := organization.GetTeam(db.DefaultContext, 3, "nonexistent")
    83  	assert.Error(t, err)
    84  	_, err = organization.GetTeam(db.DefaultContext, unittest.NonexistentID, "Owners")
    85  	assert.Error(t, err)
    86  }
    87  
    88  func TestGetTeamByID(t *testing.T) {
    89  	assert.NoError(t, unittest.PrepareTestDatabase())
    90  
    91  	testSuccess := func(teamID int64) {
    92  		team, err := organization.GetTeamByID(db.DefaultContext, teamID)
    93  		assert.NoError(t, err)
    94  		assert.EqualValues(t, teamID, team.ID)
    95  	}
    96  	testSuccess(1)
    97  	testSuccess(2)
    98  	testSuccess(3)
    99  	testSuccess(4)
   100  
   101  	_, err := organization.GetTeamByID(db.DefaultContext, unittest.NonexistentID)
   102  	assert.Error(t, err)
   103  }
   104  
   105  func TestIsTeamMember(t *testing.T) {
   106  	assert.NoError(t, unittest.PrepareTestDatabase())
   107  	test := func(orgID, teamID, userID int64, expected bool) {
   108  		isMember, err := organization.IsTeamMember(db.DefaultContext, orgID, teamID, userID)
   109  		assert.NoError(t, err)
   110  		assert.Equal(t, expected, isMember)
   111  	}
   112  
   113  	test(3, 1, 2, true)
   114  	test(3, 1, 4, false)
   115  	test(3, 1, unittest.NonexistentID, false)
   116  
   117  	test(3, 2, 2, true)
   118  	test(3, 2, 4, true)
   119  
   120  	test(3, unittest.NonexistentID, unittest.NonexistentID, false)
   121  	test(unittest.NonexistentID, unittest.NonexistentID, unittest.NonexistentID, false)
   122  }
   123  
   124  func TestGetTeamMembers(t *testing.T) {
   125  	assert.NoError(t, unittest.PrepareTestDatabase())
   126  
   127  	test := func(teamID int64) {
   128  		team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
   129  		members, err := organization.GetTeamMembers(db.DefaultContext, &organization.SearchMembersOptions{
   130  			TeamID: teamID,
   131  		})
   132  		assert.NoError(t, err)
   133  		assert.Len(t, members, team.NumMembers)
   134  		for _, member := range members {
   135  			unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{UID: member.ID, TeamID: teamID})
   136  		}
   137  	}
   138  	test(1)
   139  	test(3)
   140  }
   141  
   142  func TestGetUserTeams(t *testing.T) {
   143  	assert.NoError(t, unittest.PrepareTestDatabase())
   144  	test := func(userID int64) {
   145  		teams, _, err := organization.SearchTeam(db.DefaultContext, &organization.SearchTeamOptions{UserID: userID})
   146  		assert.NoError(t, err)
   147  		for _, team := range teams {
   148  			unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{TeamID: team.ID, UID: userID})
   149  		}
   150  	}
   151  	test(2)
   152  	test(5)
   153  	test(unittest.NonexistentID)
   154  }
   155  
   156  func TestGetUserOrgTeams(t *testing.T) {
   157  	assert.NoError(t, unittest.PrepareTestDatabase())
   158  	test := func(orgID, userID int64) {
   159  		teams, err := organization.GetUserOrgTeams(db.DefaultContext, orgID, userID)
   160  		assert.NoError(t, err)
   161  		for _, team := range teams {
   162  			assert.EqualValues(t, orgID, team.OrgID)
   163  			unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{TeamID: team.ID, UID: userID})
   164  		}
   165  	}
   166  	test(3, 2)
   167  	test(3, 4)
   168  	test(3, unittest.NonexistentID)
   169  }
   170  
   171  func TestHasTeamRepo(t *testing.T) {
   172  	assert.NoError(t, unittest.PrepareTestDatabase())
   173  
   174  	test := func(teamID, repoID int64, expected bool) {
   175  		team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
   176  		assert.Equal(t, expected, organization.HasTeamRepo(db.DefaultContext, team.OrgID, teamID, repoID))
   177  	}
   178  	test(1, 1, false)
   179  	test(1, 3, true)
   180  	test(1, 5, true)
   181  	test(1, unittest.NonexistentID, false)
   182  
   183  	test(2, 3, true)
   184  	test(2, 5, false)
   185  }
   186  
   187  func TestUsersInTeamsCount(t *testing.T) {
   188  	assert.NoError(t, unittest.PrepareTestDatabase())
   189  
   190  	test := func(teamIDs, userIDs []int64, expected int64) {
   191  		count, err := organization.UsersInTeamsCount(teamIDs, userIDs)
   192  		assert.NoError(t, err)
   193  		assert.Equal(t, expected, count)
   194  	}
   195  
   196  	test([]int64{2}, []int64{1, 2, 3, 4}, 1)          // only userid 2
   197  	test([]int64{1, 2, 3, 4, 5}, []int64{2, 5}, 2)    // userid 2,4
   198  	test([]int64{1, 2, 3, 4, 5}, []int64{2, 3, 5}, 3) // userid 2,4,5
   199  }