2017-02-02 13:33:36 +01: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 (
|
2019-08-02 18:06:28 +02:00
|
|
|
"fmt"
|
2017-02-02 13:33:36 +01:00
|
|
|
"testing"
|
|
|
|
|
2021-11-12 15:36:47 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-11 08:03:30 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2017-02-14 13:16:00 +01:00
|
|
|
|
2017-02-02 13:33:36 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-11-24 10:49:20 +01:00
|
|
|
func TestFollowUser(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-24 10:49:20 +01:00
|
|
|
|
|
|
|
testSuccess := func(followerID, followedID int64) {
|
|
|
|
assert.NoError(t, user_model.FollowUser(followerID, followedID))
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &user_model.Follow{UserID: followerID, FollowID: followedID})
|
|
|
|
}
|
|
|
|
testSuccess(4, 2)
|
|
|
|
testSuccess(5, 2)
|
|
|
|
|
|
|
|
assert.NoError(t, user_model.FollowUser(2, 2))
|
|
|
|
|
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnfollowUser(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
testSuccess := func(followerID, followedID int64) {
|
|
|
|
assert.NoError(t, user_model.UnfollowUser(followerID, followedID))
|
|
|
|
unittest.AssertNotExistsBean(t, &user_model.Follow{UserID: followerID, FollowID: followedID})
|
|
|
|
}
|
|
|
|
testSuccess(4, 2)
|
|
|
|
testSuccess(5, 2)
|
|
|
|
testSuccess(2, 2)
|
|
|
|
|
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{})
|
2021-09-24 13:32:56 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 18:06:28 +02:00
|
|
|
func TestUserIsPublicMember(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-08-02 18:06:28 +02: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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 19:52:12 +01:00
|
|
|
func testUserIsPublicMember(t *testing.T, uid, orgID int64, expected bool) {
|
2021-11-24 10:49:20 +01:00
|
|
|
user, err := user_model.GetUserByID(uid)
|
2019-08-02 18:06:28 +02:00
|
|
|
assert.NoError(t, err)
|
2021-11-22 16:21:55 +01:00
|
|
|
is, err := IsPublicMembership(orgID, user.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, is)
|
2019-08-02 18:06:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsUserOrgOwner(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-08-02 18:06:28 +02: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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 19:52:12 +01:00
|
|
|
func testIsUserOrgOwner(t *testing.T, uid, orgID int64, expected bool) {
|
2021-11-24 10:49:20 +01:00
|
|
|
user, err := user_model.GetUserByID(uid)
|
2019-08-02 18:06:28 +02:00
|
|
|
assert.NoError(t, err)
|
2021-11-22 16:21:55 +01:00
|
|
|
is, err := IsOrganizationOwner(orgID, user.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, is)
|
2019-08-02 18:06:28 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 19:55:16 +02:00
|
|
|
func TestGetOrgRepositoryIDs(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-24 10:49:20 +01:00
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
|
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
|
|
|
|
user5 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
2019-10-08 19:55:16 +02:00
|
|
|
|
2021-11-24 10:49:20 +01:00
|
|
|
accessibleRepos, err := GetOrgRepositoryIDs(user2)
|
2019-10-08 19:55:16 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// User 2's team has access to private repos 3, 5, repo 32 is a public repo of the organization
|
|
|
|
assert.Equal(t, []int64{3, 5, 23, 24, 32}, accessibleRepos)
|
|
|
|
|
2021-11-24 10:49:20 +01:00
|
|
|
accessibleRepos, err = GetOrgRepositoryIDs(user4)
|
2019-10-08 19:55:16 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// User 4's team has access to private repo 3, repo 32 is a public repo of the organization
|
|
|
|
assert.Equal(t, []int64{3, 32}, accessibleRepos)
|
|
|
|
|
2021-11-24 10:49:20 +01:00
|
|
|
accessibleRepos, err = GetOrgRepositoryIDs(user5)
|
2019-10-08 19:55:16 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// User 5's team has no access to any repo
|
|
|
|
assert.Len(t, accessibleRepos, 0)
|
|
|
|
}
|