mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.7 KiB
72 lines
1.7 KiB
// Copyright 2022 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 organization
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestUserIsPublicMember(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
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) {
|
|
user, err := user_model.GetUserByID(uid)
|
|
assert.NoError(t, err)
|
|
is, err := IsPublicMembership(orgID, user.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expected, is)
|
|
}
|
|
|
|
func TestIsUserOrgOwner(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
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) {
|
|
user, err := user_model.GetUserByID(uid)
|
|
assert.NoError(t, err)
|
|
is, err := IsOrganizationOwner(db.DefaultContext, orgID, user.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expected, is)
|
|
}
|
|
|