2017-02-23 10:36:15 +09:00
|
|
|
// 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 (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-24 18:49:20 +09:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 15:29:02 +09:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 20:58:28 +09:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-12-10 10:27:50 +09:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 23:36:47 +09:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 18:49:20 +09:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 21:34:35 +09:00
|
|
|
|
2017-02-23 10:36:15 +09:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTeam_AddMember(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 10:36:15 +09:00
|
|
|
|
|
|
|
test := func(teamID, userID int64) {
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
|
|
|
|
assert.NoError(t, AddTeamMember(team, userID))
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{UID: userID, TeamID: teamID})
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID}, &user_model.User{ID: team.OrgID})
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
test(1, 2)
|
|
|
|
test(1, 4)
|
|
|
|
test(3, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTeam_RemoveMember(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 10:36:15 +09:00
|
|
|
|
|
|
|
testSuccess := func(teamID, userID int64) {
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
|
|
|
|
assert.NoError(t, RemoveTeamMember(team, userID))
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.TeamUser{UID: userID, TeamID: teamID})
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID})
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
testSuccess(1, 4)
|
|
|
|
testSuccess(2, 2)
|
|
|
|
testSuccess(3, 2)
|
2021-11-16 17:53:21 +09:00
|
|
|
testSuccess(3, unittest.NonexistentID)
|
2017-02-23 10:36:15 +09:00
|
|
|
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1}).(*organization.Team)
|
|
|
|
err := RemoveTeamMember(team, 2)
|
|
|
|
assert.True(t, organization.IsErrLastOrgOwner(err))
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTeam_HasRepository(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 10:36:15 +09:00
|
|
|
|
|
|
|
test := func(teamID, repoID int64, expected bool) {
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
|
|
|
|
assert.Equal(t, expected, HasRepository(team, repoID))
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
test(1, 1, false)
|
|
|
|
test(1, 3, true)
|
|
|
|
test(1, 5, true)
|
2021-11-16 17:53:21 +09:00
|
|
|
test(1, unittest.NonexistentID, false)
|
2017-02-23 10:36:15 +09:00
|
|
|
|
|
|
|
test(2, 3, true)
|
|
|
|
test(2, 5, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTeam_AddRepository(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 10:36:15 +09:00
|
|
|
|
2017-03-15 09:52:01 +09:00
|
|
|
testSuccess := func(teamID, repoID int64) {
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
|
2021-12-10 10:27:50 +09:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository)
|
2022-03-29 15:29:02 +09:00
|
|
|
assert.NoError(t, AddRepository(team, repo))
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamRepo{TeamID: teamID, RepoID: repoID})
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID}, &repo_model.Repository{ID: repoID})
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
2017-03-15 09:52:01 +09:00
|
|
|
testSuccess(2, 3)
|
|
|
|
testSuccess(2, 5)
|
2017-02-23 10:36:15 +09:00
|
|
|
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1}).(*organization.Team)
|
2021-12-10 10:27:50 +09:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
2022-03-29 15:29:02 +09:00
|
|
|
assert.Error(t, AddRepository(team, repo))
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: 1}, &repo_model.Repository{ID: 1})
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTeam_RemoveRepository(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 10:36:15 +09:00
|
|
|
|
|
|
|
testSuccess := func(teamID, repoID int64) {
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
|
|
|
|
assert.NoError(t, RemoveRepository(team, repoID))
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.TeamRepo{TeamID: teamID, RepoID: repoID})
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID}, &repo_model.Repository{ID: repoID})
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
testSuccess(2, 3)
|
|
|
|
testSuccess(2, 5)
|
2021-11-16 17:53:21 +09:00
|
|
|
testSuccess(1, unittest.NonexistentID)
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsUsableTeamName(t *testing.T) {
|
2022-03-29 15:29:02 +09:00
|
|
|
assert.NoError(t, organization.IsUsableTeamName("usable"))
|
|
|
|
assert.True(t, db.IsErrNameReserved(organization.IsUsableTeamName("new")))
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewTeam(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 10:36:15 +09:00
|
|
|
|
|
|
|
const teamName = "newTeamName"
|
2022-03-29 15:29:02 +09:00
|
|
|
team := &organization.Team{Name: teamName, OrgID: 3}
|
2017-02-23 10:36:15 +09:00
|
|
|
assert.NoError(t, NewTeam(team))
|
2022-03-29 15:29:02 +09:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.Team{Name: teamName})
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{}, &user_model.User{ID: team.OrgID})
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateTeam(t *testing.T) {
|
|
|
|
// successful update
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 10:36:15 +09:00
|
|
|
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2}).(*organization.Team)
|
2017-02-23 10:36:15 +09:00
|
|
|
team.LowerName = "newname"
|
|
|
|
team.Name = "newName"
|
|
|
|
team.Description = strings.Repeat("A long description!", 100)
|
2022-01-05 12:37:00 +09:00
|
|
|
team.AccessMode = perm.AccessModeAdmin
|
2019-11-06 18:37:14 +09:00
|
|
|
assert.NoError(t, UpdateTeam(team, true, false))
|
2017-02-23 10:36:15 +09:00
|
|
|
|
2022-03-29 15:29:02 +09:00
|
|
|
team = unittest.AssertExistsAndLoadBean(t, &organization.Team{Name: "newName"}).(*organization.Team)
|
2017-02-23 10:36:15 +09:00
|
|
|
assert.True(t, strings.HasPrefix(team.Description, "A long description!"))
|
|
|
|
|
2021-11-16 17:53:21 +09:00
|
|
|
access := unittest.AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: 3}).(*Access)
|
2021-11-28 20:58:28 +09:00
|
|
|
assert.EqualValues(t, perm.AccessModeAdmin, access.Mode)
|
2017-02-23 10:36:15 +09:00
|
|
|
|
2022-03-29 15:29:02 +09:00
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: team.ID})
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateTeam2(t *testing.T) {
|
|
|
|
// update to already-existing team
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 10:36:15 +09:00
|
|
|
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2}).(*organization.Team)
|
2017-02-23 10:36:15 +09:00
|
|
|
team.LowerName = "owners"
|
|
|
|
team.Name = "Owners"
|
|
|
|
team.Description = strings.Repeat("A long description!", 100)
|
2019-11-06 18:37:14 +09:00
|
|
|
err := UpdateTeam(team, true, false)
|
2022-03-29 15:29:02 +09:00
|
|
|
assert.True(t, organization.IsErrTeamAlreadyExist(err))
|
2017-02-23 10:36:15 +09:00
|
|
|
|
2022-03-29 15:29:02 +09:00
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: team.ID})
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteTeam(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 10:36:15 +09:00
|
|
|
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2}).(*organization.Team)
|
2017-02-23 10:36:15 +09:00
|
|
|
assert.NoError(t, DeleteTeam(team))
|
2022-03-29 15:29:02 +09:00
|
|
|
unittest.AssertNotExistsBean(t, &organization.Team{ID: team.ID})
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.TeamRepo{TeamID: team.ID})
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.TeamUser{TeamID: team.ID})
|
2017-02-23 10:36:15 +09:00
|
|
|
|
|
|
|
// check that team members don't have "leftover" access to repos
|
2021-11-24 18:49:20 +09:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
|
2021-12-10 10:27:50 +09:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
2018-11-28 20:26:14 +09:00
|
|
|
accessMode, err := AccessLevel(user, repo)
|
2017-02-23 10:36:15 +09:00
|
|
|
assert.NoError(t, err)
|
2021-11-28 20:58:28 +09:00
|
|
|
assert.True(t, accessMode < perm.AccessModeWrite)
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddTeamMember(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 10:36:15 +09:00
|
|
|
|
|
|
|
test := func(teamID, userID int64) {
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
|
2017-02-24 15:25:09 +09:00
|
|
|
assert.NoError(t, AddTeamMember(team, userID))
|
2022-03-29 15:29:02 +09:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.TeamUser{UID: userID, TeamID: teamID})
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID}, &user_model.User{ID: team.OrgID})
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
test(1, 2)
|
|
|
|
test(1, 4)
|
|
|
|
test(3, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveTeamMember(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 10:36:15 +09:00
|
|
|
|
|
|
|
testSuccess := func(teamID, userID int64) {
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID}).(*organization.Team)
|
2017-02-24 15:25:09 +09:00
|
|
|
assert.NoError(t, RemoveTeamMember(team, userID))
|
2022-03-29 15:29:02 +09:00
|
|
|
unittest.AssertNotExistsBean(t, &organization.TeamUser{UID: userID, TeamID: teamID})
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID})
|
2017-02-23 10:36:15 +09:00
|
|
|
}
|
|
|
|
testSuccess(1, 4)
|
|
|
|
testSuccess(2, 2)
|
|
|
|
testSuccess(3, 2)
|
2021-11-16 17:53:21 +09:00
|
|
|
testSuccess(3, unittest.NonexistentID)
|
2017-02-23 10:36:15 +09:00
|
|
|
|
2022-03-29 15:29:02 +09:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1}).(*organization.Team)
|
2017-02-24 15:25:09 +09:00
|
|
|
err := RemoveTeamMember(team, 2)
|
2022-03-29 15:29:02 +09:00
|
|
|
assert.True(t, organization.IsErrLastOrgOwner(err))
|
2018-12-11 20:28:37 +09:00
|
|
|
}
|