2022-03-29 09:29:02 +03:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-29 09:29:02 +03:00
|
|
|
|
2022-06-15 10:02:00 +03:00
|
|
|
package organization_test
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-15 10:02:00 +03:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-03-29 09:29:02 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-06-15 10:02:00 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 22:41:10 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-03-29 09:29:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserIsPublicMember(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
uid int64
|
|
|
|
orgid int64
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{2, 3, true},
|
|
|
|
{4, 3, false},
|
|
|
|
{5, 6, true},
|
|
|
|
{5, 7, false},
|
|
|
|
}
|
|
|
|
for _, v := range tt {
|
|
|
|
t.Run(fmt.Sprintf("UserId%dIsPublicMemberOf%d", v.uid, v.orgid), func(t *testing.T) {
|
|
|
|
testUserIsPublicMember(t, v.uid, v.orgid, v.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUserIsPublicMember(t *testing.T, uid, orgID int64, expected bool) {
|
2022-12-03 05:48:26 +03:00
|
|
|
user, err := user_model.GetUserByID(db.DefaultContext, uid)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2023-10-03 13:30:41 +03:00
|
|
|
is, err := organization.IsPublicMembership(db.DefaultContext, orgID, user.ID)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-29 09:29:02 +03:00
|
|
|
assert.Equal(t, expected, is)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsUserOrgOwner(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-03-29 09:29:02 +03:00
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
uid int64
|
|
|
|
orgid int64
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{2, 3, true},
|
|
|
|
{4, 3, false},
|
|
|
|
{5, 6, true},
|
|
|
|
{5, 7, true},
|
|
|
|
}
|
|
|
|
for _, v := range tt {
|
|
|
|
t.Run(fmt.Sprintf("UserId%dIsOrgOwnerOf%d", v.uid, v.orgid), func(t *testing.T) {
|
|
|
|
testIsUserOrgOwner(t, v.uid, v.orgid, v.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIsUserOrgOwner(t *testing.T, uid, orgID int64, expected bool) {
|
2022-12-03 05:48:26 +03:00
|
|
|
user, err := user_model.GetUserByID(db.DefaultContext, uid)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-15 10:02:00 +03:00
|
|
|
is, err := organization.IsOrganizationOwner(db.DefaultContext, orgID, user.ID)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-29 09:29:02 +03:00
|
|
|
assert.Equal(t, expected, is)
|
|
|
|
}
|
2022-05-20 13:20:51 +03:00
|
|
|
|
|
|
|
func TestUserListIsPublicMember(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-05-20 13:20:51 +03:00
|
|
|
tt := []struct {
|
|
|
|
orgid int64
|
|
|
|
expected map[int64]bool
|
|
|
|
}{
|
|
|
|
{3, map[int64]bool{2: true, 4: false, 28: true}},
|
|
|
|
{6, map[int64]bool{5: true, 28: true}},
|
|
|
|
{7, map[int64]bool{5: false}},
|
|
|
|
{25, map[int64]bool{24: true}},
|
|
|
|
{22, map[int64]bool{}},
|
|
|
|
}
|
|
|
|
for _, v := range tt {
|
2023-05-24 22:06:04 +03:00
|
|
|
t.Run(fmt.Sprintf("IsPublicMemberOfOrgId%d", v.orgid), func(t *testing.T) {
|
2022-05-20 13:20:51 +03:00
|
|
|
testUserListIsPublicMember(t, v.orgid, v.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUserListIsPublicMember(t *testing.T, orgID int64, expected map[int64]bool) {
|
2022-06-15 10:02:00 +03:00
|
|
|
org, err := organization.GetOrgByID(db.DefaultContext, orgID)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2024-11-11 03:38:30 +03:00
|
|
|
_, membersIsPublic, err := org.GetMembers(db.DefaultContext, &user_model.User{IsAdmin: true})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-05-20 13:20:51 +03:00
|
|
|
assert.Equal(t, expected, membersIsPublic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserListIsUserOrgOwner(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-05-20 13:20:51 +03:00
|
|
|
tt := []struct {
|
|
|
|
orgid int64
|
|
|
|
expected map[int64]bool
|
|
|
|
}{
|
|
|
|
{3, map[int64]bool{2: true, 4: false, 28: false}},
|
|
|
|
{6, map[int64]bool{5: true, 28: false}},
|
|
|
|
{7, map[int64]bool{5: true}},
|
|
|
|
{25, map[int64]bool{24: false}}, // ErrTeamNotExist
|
|
|
|
{22, map[int64]bool{}}, // No member
|
|
|
|
}
|
|
|
|
for _, v := range tt {
|
2023-05-24 22:06:04 +03:00
|
|
|
t.Run(fmt.Sprintf("IsUserOrgOwnerOfOrgId%d", v.orgid), func(t *testing.T) {
|
2022-05-20 13:20:51 +03:00
|
|
|
testUserListIsUserOrgOwner(t, v.orgid, v.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUserListIsUserOrgOwner(t *testing.T, orgID int64, expected map[int64]bool) {
|
2022-06-15 10:02:00 +03:00
|
|
|
org, err := organization.GetOrgByID(db.DefaultContext, orgID)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2024-11-11 03:38:30 +03:00
|
|
|
members, _, err := org.GetMembers(db.DefaultContext, &user_model.User{IsAdmin: true})
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2023-10-03 13:30:41 +03:00
|
|
|
assert.Equal(t, expected, organization.IsUserOrgOwner(db.DefaultContext, members, orgID))
|
2022-06-15 10:02:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddOrgUser(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-15 10:02:00 +03:00
|
|
|
testSuccess := func(orgID, userID int64, isPublic bool) {
|
2022-08-16 05:22:25 +03:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID})
|
2022-06-15 10:02:00 +03:00
|
|
|
expectedNumMembers := org.NumMembers
|
|
|
|
if !unittest.BeanExists(t, &organization.OrgUser{OrgID: orgID, UID: userID}) {
|
|
|
|
expectedNumMembers++
|
|
|
|
}
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, organization.AddOrgUser(db.DefaultContext, orgID, userID))
|
2022-06-15 10:02:00 +03:00
|
|
|
ou := &organization.OrgUser{OrgID: orgID, UID: userID}
|
|
|
|
unittest.AssertExistsAndLoadBean(t, ou)
|
|
|
|
assert.Equal(t, isPublic, ou.IsPublic)
|
2022-08-16 05:22:25 +03:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID})
|
2022-06-15 10:02:00 +03:00
|
|
|
assert.EqualValues(t, expectedNumMembers, org.NumMembers)
|
|
|
|
}
|
|
|
|
|
|
|
|
setting.Service.DefaultOrgMemberVisible = false
|
|
|
|
testSuccess(3, 5, false)
|
|
|
|
testSuccess(3, 5, false)
|
|
|
|
testSuccess(6, 2, false)
|
|
|
|
|
|
|
|
setting.Service.DefaultOrgMemberVisible = true
|
|
|
|
testSuccess(6, 3, true)
|
|
|
|
|
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
|
2022-05-20 13:20:51 +03:00
|
|
|
}
|