2017-02-23 02:36:15 +01:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-02-23 02:36:15 +01:00
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-24 10:49:20 +01:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 08:29:02 +02:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 12:58:28 +01:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 12:09:36 +02:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 15:36:47 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 13:34:35 +01:00
|
|
|
|
2017-02-23 02:36:15 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:41:27 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-02-23 02:36:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTeam_AddMember(t *testing.T) {
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 02:36:15 +01:00
|
|
|
|
|
|
|
test := func(teamID, userID int64) {
|
2022-08-16 04:22:25 +02:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, AddTeamMember(db.DefaultContext, team, userID))
|
2022-03-29 08:29:02 +02: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 02:36:15 +01:00
|
|
|
}
|
|
|
|
test(1, 2)
|
|
|
|
test(1, 4)
|
|
|
|
test(3, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTeam_RemoveMember(t *testing.T) {
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 02:36:15 +01:00
|
|
|
|
|
|
|
testSuccess := func(teamID, userID int64) {
|
2022-08-16 04:22:25 +02:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, RemoveTeamMember(db.DefaultContext, team, userID))
|
2022-03-29 08:29:02 +02:00
|
|
|
unittest.AssertNotExistsBean(t, &organization.TeamUser{UID: userID, TeamID: teamID})
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID})
|
2017-02-23 02:36:15 +01:00
|
|
|
}
|
|
|
|
testSuccess(1, 4)
|
|
|
|
testSuccess(2, 2)
|
|
|
|
testSuccess(3, 2)
|
2021-11-16 09:53:21 +01:00
|
|
|
testSuccess(3, unittest.NonexistentID)
|
2017-02-23 02:36:15 +01:00
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1})
|
2023-09-14 19:09:32 +02:00
|
|
|
err := RemoveTeamMember(db.DefaultContext, team, 2)
|
2022-03-29 08:29:02 +02:00
|
|
|
assert.True(t, organization.IsErrLastOrgOwner(err))
|
2017-02-23 02:36:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsUsableTeamName(t *testing.T) {
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, organization.IsUsableTeamName("usable"))
|
2022-03-29 08:29:02 +02:00
|
|
|
assert.True(t, db.IsErrNameReserved(organization.IsUsableTeamName("new")))
|
2017-02-23 02:36:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewTeam(t *testing.T) {
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 02:36:15 +01:00
|
|
|
|
|
|
|
const teamName = "newTeamName"
|
2022-03-29 08:29:02 +02:00
|
|
|
team := &organization.Team{Name: teamName, OrgID: 3}
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, NewTeam(db.DefaultContext, team))
|
2022-03-29 08:29:02 +02:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.Team{Name: teamName})
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{}, &user_model.User{ID: team.OrgID})
|
2017-02-23 02:36:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateTeam(t *testing.T) {
|
|
|
|
// successful update
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 02:36:15 +01:00
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2})
|
2017-02-23 02:36:15 +01:00
|
|
|
team.LowerName = "newname"
|
|
|
|
team.Name = "newName"
|
|
|
|
team.Description = strings.Repeat("A long description!", 100)
|
2022-01-05 04:37:00 +01:00
|
|
|
team.AccessMode = perm.AccessModeAdmin
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, UpdateTeam(db.DefaultContext, team, true, false))
|
2017-02-23 02:36:15 +01:00
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
team = unittest.AssertExistsAndLoadBean(t, &organization.Team{Name: "newName"})
|
2017-02-23 02:36:15 +01:00
|
|
|
assert.True(t, strings.HasPrefix(team.Description, "A long description!"))
|
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
access := unittest.AssertExistsAndLoadBean(t, &access_model.Access{UserID: 4, RepoID: 3})
|
2021-11-28 12:58:28 +01:00
|
|
|
assert.EqualValues(t, perm.AccessModeAdmin, access.Mode)
|
2017-02-23 02:36:15 +01:00
|
|
|
|
2022-03-29 08:29:02 +02:00
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: team.ID})
|
2017-02-23 02:36:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateTeam2(t *testing.T) {
|
|
|
|
// update to already-existing team
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 02:36:15 +01:00
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2})
|
2017-02-23 02:36:15 +01:00
|
|
|
team.LowerName = "owners"
|
|
|
|
team.Name = "Owners"
|
|
|
|
team.Description = strings.Repeat("A long description!", 100)
|
2023-09-14 19:09:32 +02:00
|
|
|
err := UpdateTeam(db.DefaultContext, team, true, false)
|
2022-03-29 08:29:02 +02:00
|
|
|
assert.True(t, organization.IsErrTeamAlreadyExist(err))
|
2017-02-23 02:36:15 +01:00
|
|
|
|
2022-03-29 08:29:02 +02:00
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: team.ID})
|
2017-02-23 02:36:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteTeam(t *testing.T) {
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 02:36:15 +01:00
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 2})
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, DeleteTeam(db.DefaultContext, team))
|
2022-03-29 08:29:02 +02: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 02:36:15 +01:00
|
|
|
|
|
|
|
// check that team members don't have "leftover" access to repos
|
2022-08-16 04:22:25 +02:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
2022-11-19 09:12:33 +01:00
|
|
|
accessMode, err := access_model.AccessLevel(db.DefaultContext, user, repo)
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Less(t, accessMode, perm.AccessModeWrite)
|
2017-02-23 02:36:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddTeamMember(t *testing.T) {
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 02:36:15 +01:00
|
|
|
|
|
|
|
test := func(teamID, userID int64) {
|
2022-08-16 04:22:25 +02:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, AddTeamMember(db.DefaultContext, team, userID))
|
2022-03-29 08:29:02 +02: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 02:36:15 +01:00
|
|
|
}
|
|
|
|
test(1, 2)
|
|
|
|
test(1, 4)
|
|
|
|
test(3, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveTeamMember(t *testing.T) {
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-23 02:36:15 +01:00
|
|
|
|
|
|
|
testSuccess := func(teamID, userID int64) {
|
2022-08-16 04:22:25 +02:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, RemoveTeamMember(db.DefaultContext, team, userID))
|
2022-03-29 08:29:02 +02:00
|
|
|
unittest.AssertNotExistsBean(t, &organization.TeamUser{UID: userID, TeamID: teamID})
|
|
|
|
unittest.CheckConsistencyFor(t, &organization.Team{ID: teamID})
|
2017-02-23 02:36:15 +01:00
|
|
|
}
|
|
|
|
testSuccess(1, 4)
|
|
|
|
testSuccess(2, 2)
|
|
|
|
testSuccess(3, 2)
|
2021-11-16 09:53:21 +01:00
|
|
|
testSuccess(3, unittest.NonexistentID)
|
2017-02-23 02:36:15 +01:00
|
|
|
|
2022-08-16 04:22:25 +02:00
|
|
|
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 1})
|
2023-09-14 19:09:32 +02:00
|
|
|
err := RemoveTeamMember(db.DefaultContext, team, 2)
|
2022-03-29 08:29:02 +02:00
|
|
|
assert.True(t, organization.IsErrLastOrgOwner(err))
|
2018-12-11 12:28:37 +01:00
|
|
|
}
|
2022-05-11 12:09:36 +02:00
|
|
|
|
|
|
|
func TestRepository_RecalculateAccesses3(t *testing.T) {
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 04:22:25 +02:00
|
|
|
team5 := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: 5})
|
|
|
|
user29 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 29})
|
2022-05-11 12:09:36 +02:00
|
|
|
|
|
|
|
has, err := db.GetEngine(db.DefaultContext).Get(&access_model.Access{UserID: 29, RepoID: 23})
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, err)
|
2022-05-11 12:09:36 +02:00
|
|
|
assert.False(t, has)
|
|
|
|
|
|
|
|
// adding user29 to team5 should add an explicit access row for repo 23
|
|
|
|
// even though repo 23 is public
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, AddTeamMember(db.DefaultContext, team5, user29.ID))
|
2022-05-11 12:09:36 +02:00
|
|
|
|
|
|
|
has, err = db.GetEngine(db.DefaultContext).Get(&access_model.Access{UserID: 29, RepoID: 23})
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, err)
|
2022-05-11 12:09:36 +02:00
|
|
|
assert.True(t, has)
|
|
|
|
}
|