2019-08-02 16:06:28 +00:00
|
|
|
// Copyright 2019 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 (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-29 06:29:02 +00:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-12 14:36:47 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
|
2019-08-02 16:06:28 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserListIsPublicMember(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-08-02 16:06:28 +00:00
|
|
|
tt := []struct {
|
|
|
|
orgid int64
|
|
|
|
expected map[int64]bool
|
|
|
|
}{
|
2019-11-20 11:27:49 +00:00
|
|
|
{3, map[int64]bool{2: true, 4: false, 28: true}},
|
|
|
|
{6, map[int64]bool{5: true, 28: true}},
|
2019-08-02 16:06:28 +00:00
|
|
|
{7, map[int64]bool{5: false}},
|
|
|
|
{25, map[int64]bool{24: true}},
|
|
|
|
{22, map[int64]bool{}},
|
|
|
|
}
|
|
|
|
for _, v := range tt {
|
|
|
|
t.Run(fmt.Sprintf("IsPublicMemberOfOrdIg%d", v.orgid), func(t *testing.T) {
|
|
|
|
testUserListIsPublicMember(t, v.orgid, v.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-03-14 18:52:12 +00:00
|
|
|
|
2019-08-02 16:06:28 +00:00
|
|
|
func testUserListIsPublicMember(t *testing.T, orgID int64, expected map[int64]bool) {
|
2022-03-29 06:29:02 +00:00
|
|
|
org, err := organization.GetOrgByID(orgID)
|
2019-08-02 16:06:28 +00:00
|
|
|
assert.NoError(t, err)
|
2021-11-19 11:41:40 +00:00
|
|
|
_, membersIsPublic, err := org.GetMembers()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, membersIsPublic)
|
2019-08-02 16:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserListIsUserOrgOwner(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-08-02 16:06:28 +00:00
|
|
|
tt := []struct {
|
|
|
|
orgid int64
|
|
|
|
expected map[int64]bool
|
|
|
|
}{
|
2019-11-20 11:27:49 +00:00
|
|
|
{3, map[int64]bool{2: true, 4: false, 28: false}},
|
|
|
|
{6, map[int64]bool{5: true, 28: false}},
|
2019-08-02 16:06:28 +00:00
|
|
|
{7, map[int64]bool{5: true}},
|
|
|
|
{25, map[int64]bool{24: false}}, // ErrTeamNotExist
|
|
|
|
{22, map[int64]bool{}}, // No member
|
|
|
|
}
|
|
|
|
for _, v := range tt {
|
|
|
|
t.Run(fmt.Sprintf("IsUserOrgOwnerOfOrdIg%d", v.orgid), func(t *testing.T) {
|
|
|
|
testUserListIsUserOrgOwner(t, v.orgid, v.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUserListIsUserOrgOwner(t *testing.T, orgID int64, expected map[int64]bool) {
|
2022-03-29 06:29:02 +00:00
|
|
|
org, err := organization.GetOrgByID(orgID)
|
2021-11-19 11:41:40 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
members, _, err := org.GetMembers()
|
2019-08-02 16:06:28 +00:00
|
|
|
assert.NoError(t, err)
|
2021-11-28 14:11:58 +00:00
|
|
|
assert.Equal(t, expected, IsUserOrgOwner(members, orgID))
|
2019-08-02 16:06:28 +00:00
|
|
|
}
|