This repository has been archived on 2024-01-04. You can view files and clone it, but cannot push or open issues or pull requests.
forgejo/models/org_test.go

661 lines
20 KiB
Go
Raw Normal View History

2017-01-24 16:16:36 +00: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 (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
2017-01-24 16:16:36 +00:00
"github.com/stretchr/testify/assert"
)
func TestUser_IsOwnedBy(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
for _, testCase := range []struct {
OrgID int64
UserID int64
ExpectedOwner bool
}{
{3, 2, true},
{3, 1, false},
{3, 3, false},
{3, 4, false},
{2, 2, false}, // user2 is not an organization
{2, 3, false},
} {
org := db.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User)
isOwner, err := org.IsOwnedBy(testCase.UserID)
assert.NoError(t, err)
assert.Equal(t, testCase.ExpectedOwner, isOwner)
}
2017-01-24 16:16:36 +00:00
}
func TestUser_IsOrgMember(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
for _, testCase := range []struct {
OrgID int64
UserID int64
ExpectedMember bool
}{
{3, 2, true},
{3, 4, true},
{3, 1, false},
{3, 3, false},
{2, 2, false}, // user2 is not an organization
{2, 3, false},
} {
org := db.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User)
isMember, err := org.IsOrgMember(testCase.UserID)
assert.NoError(t, err)
assert.Equal(t, testCase.ExpectedMember, isMember)
}
2017-01-24 16:16:36 +00:00
}
func TestUser_GetTeam(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
team, err := org.GetTeam("team1")
assert.NoError(t, err)
assert.Equal(t, org.ID, team.OrgID)
assert.Equal(t, "team1", team.LowerName)
_, err = org.GetTeam("does not exist")
assert.True(t, IsErrTeamNotExist(err))
2017-01-24 16:16:36 +00:00
nonOrg := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
2017-01-24 16:16:36 +00:00
_, err = nonOrg.GetTeam("team")
assert.True(t, IsErrTeamNotExist(err))
2017-01-24 16:16:36 +00:00
}
func TestUser_GetOwnerTeam(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
team, err := org.GetOwnerTeam()
assert.NoError(t, err)
assert.Equal(t, org.ID, team.OrgID)
nonOrg := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
2017-01-24 16:16:36 +00:00
_, err = nonOrg.GetOwnerTeam()
assert.True(t, IsErrTeamNotExist(err))
2017-01-24 16:16:36 +00:00
}
func TestUser_GetTeams(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
assert.NoError(t, org.LoadTeams())
if assert.Len(t, org.Teams, 4) {
assert.Equal(t, int64(1), org.Teams[0].ID)
assert.Equal(t, int64(2), org.Teams[1].ID)
assert.Equal(t, int64(12), org.Teams[2].ID)
assert.Equal(t, int64(7), org.Teams[3].ID)
}
2017-01-24 16:16:36 +00:00
}
func TestUser_GetMembers(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
assert.NoError(t, org.GetMembers())
if assert.Len(t, org.Members, 3) {
assert.Equal(t, int64(2), org.Members[0].ID)
assert.Equal(t, int64(28), org.Members[1].ID)
assert.Equal(t, int64(4), org.Members[2].ID)
}
2017-01-24 16:16:36 +00:00
}
func TestUser_AddMember(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
// add a user that is not a member
db.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
2017-01-24 16:16:36 +00:00
prevNumMembers := org.NumMembers
assert.NoError(t, org.AddMember(5))
db.AssertExistsAndLoadBean(t, &OrgUser{UID: 5, OrgID: 3})
org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
assert.Equal(t, prevNumMembers+1, org.NumMembers)
// add a user that is already a member
db.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
2017-01-24 16:16:36 +00:00
prevNumMembers = org.NumMembers
assert.NoError(t, org.AddMember(4))
db.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
assert.Equal(t, prevNumMembers, org.NumMembers)
CheckConsistencyFor(t, &User{})
2017-01-24 16:16:36 +00:00
}
func TestUser_RemoveMember(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
// remove a user that is a member
db.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
2017-01-24 16:16:36 +00:00
prevNumMembers := org.NumMembers
assert.NoError(t, org.RemoveMember(4))
db.AssertNotExistsBean(t, &OrgUser{UID: 4, OrgID: 3})
org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
assert.Equal(t, prevNumMembers-1, org.NumMembers)
// remove a user that is not a member
db.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
2017-01-24 16:16:36 +00:00
prevNumMembers = org.NumMembers
assert.NoError(t, org.RemoveMember(5))
db.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
assert.Equal(t, prevNumMembers, org.NumMembers)
CheckConsistencyFor(t, &User{}, &Team{})
2017-01-24 16:16:36 +00:00
}
func TestUser_RemoveOrgRepo(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
repo := db.AssertExistsAndLoadBean(t, &Repository{OwnerID: org.ID}).(*Repository)
2017-01-24 16:16:36 +00:00
// remove a repo that does belong to org
db.AssertExistsAndLoadBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
2017-01-24 16:16:36 +00:00
assert.NoError(t, org.RemoveOrgRepo(repo.ID))
db.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
db.AssertExistsAndLoadBean(t, &Repository{ID: repo.ID}) // repo should still exist
2017-01-24 16:16:36 +00:00
// remove a repo that does not belong to org
assert.NoError(t, org.RemoveOrgRepo(repo.ID))
db.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
2017-01-24 16:16:36 +00:00
assert.NoError(t, org.RemoveOrgRepo(db.NonexistentID))
CheckConsistencyFor(t,
&User{ID: org.ID},
&Team{OrgID: org.ID},
&Repository{ID: repo.ID})
2017-01-24 16:16:36 +00:00
}
func TestCreateOrganization(t *testing.T) {
// successful creation of org
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
2017-01-24 16:16:36 +00:00
const newOrgName = "neworg"
org := &User{
Name: newOrgName,
}
db.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
2017-01-24 16:16:36 +00:00
assert.NoError(t, CreateOrganization(org, owner))
org = db.AssertExistsAndLoadBean(t,
2017-01-24 16:16:36 +00:00
&User{Name: newOrgName, Type: UserTypeOrganization}).(*User)
ownerTeam := db.AssertExistsAndLoadBean(t,
2017-01-24 16:16:36 +00:00
&Team{Name: ownerTeamName, OrgID: org.ID}).(*Team)
db.AssertExistsAndLoadBean(t, &TeamUser{UID: owner.ID, TeamID: ownerTeam.ID})
CheckConsistencyFor(t, &User{}, &Team{})
2017-01-24 16:16:36 +00:00
}
func TestCreateOrganization2(t *testing.T) {
// unauthorized creation of org
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
owner := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
2017-01-24 16:16:36 +00:00
const newOrgName = "neworg"
org := &User{
Name: newOrgName,
}
db.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
2017-01-24 16:16:36 +00:00
err := CreateOrganization(org, owner)
assert.Error(t, err)
assert.True(t, IsErrUserNotAllowedCreateOrg(err))
db.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
CheckConsistencyFor(t, &User{}, &Team{})
2017-01-24 16:16:36 +00:00
}
func TestCreateOrganization3(t *testing.T) {
// create org with same name as existent org
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
org := &User{Name: "user3"} // should already exist
db.AssertExistsAndLoadBean(t, &User{Name: org.Name}) // sanity check
2017-01-24 16:16:36 +00:00
err := CreateOrganization(org, owner)
assert.Error(t, err)
assert.True(t, IsErrUserAlreadyExist(err))
CheckConsistencyFor(t, &User{}, &Team{})
2017-01-24 16:16:36 +00:00
}
func TestCreateOrganization4(t *testing.T) {
// create org with unusable name
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
2017-01-24 16:16:36 +00:00
err := CreateOrganization(&User{Name: "assets"}, owner)
assert.Error(t, err)
assert.True(t, IsErrNameReserved(err))
CheckConsistencyFor(t, &User{}, &Team{})
2017-01-24 16:16:36 +00:00
}
func TestGetOrgByName(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
org, err := GetOrgByName("user3")
assert.NoError(t, err)
assert.EqualValues(t, 3, org.ID)
assert.Equal(t, "user3", org.Name)
2019-06-12 19:41:28 +00:00
_, err = GetOrgByName("user2") // user2 is an individual
assert.True(t, IsErrOrgNotExist(err))
2017-01-24 16:16:36 +00:00
2019-06-12 19:41:28 +00:00
_, err = GetOrgByName("") // corner case
assert.True(t, IsErrOrgNotExist(err))
2017-01-24 16:16:36 +00:00
}
func TestCountOrganizations(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
expected, err := db.GetEngine(db.DefaultContext).Where("type=?", UserTypeOrganization).Count(&User{})
2017-01-24 16:16:36 +00:00
assert.NoError(t, err)
assert.Equal(t, expected, CountOrganizations())
}
func TestDeleteOrganization(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 6}).(*User)
2017-01-24 16:16:36 +00:00
assert.NoError(t, DeleteOrganization(org))
db.AssertNotExistsBean(t, &User{ID: 6})
db.AssertNotExistsBean(t, &OrgUser{OrgID: 6})
db.AssertNotExistsBean(t, &Team{OrgID: 6})
2017-01-24 16:16:36 +00:00
org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
err := DeleteOrganization(org)
assert.Error(t, err)
assert.True(t, IsErrUserOwnRepos(err))
user := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
assert.Error(t, DeleteOrganization(user))
CheckConsistencyFor(t, &User{}, &Team{})
2017-01-24 16:16:36 +00:00
}
func TestIsOrganizationOwner(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
test := func(orgID, userID int64, expected bool) {
isOwner, err := IsOrganizationOwner(orgID, userID)
assert.NoError(t, err)
assert.EqualValues(t, expected, isOwner)
}
test(3, 2, true)
test(3, 3, false)
test(6, 5, true)
test(6, 4, false)
test(db.NonexistentID, db.NonexistentID, false)
2017-01-24 16:16:36 +00:00
}
func TestIsOrganizationMember(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
test := func(orgID, userID int64, expected bool) {
isMember, err := IsOrganizationMember(orgID, userID)
assert.NoError(t, err)
assert.EqualValues(t, expected, isMember)
}
test(3, 2, true)
test(3, 3, false)
test(3, 4, true)
test(6, 5, true)
test(6, 4, false)
test(db.NonexistentID, db.NonexistentID, false)
2017-01-24 16:16:36 +00:00
}
func TestIsPublicMembership(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
test := func(orgID, userID int64, expected bool) {
isMember, err := IsPublicMembership(orgID, userID)
assert.NoError(t, err)
assert.EqualValues(t, expected, isMember)
}
test(3, 2, true)
test(3, 3, false)
test(3, 4, false)
test(6, 5, true)
test(6, 4, false)
test(db.NonexistentID, db.NonexistentID, false)
2017-01-24 16:16:36 +00:00
}
func TestGetOrgsByUserID(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
orgs, err := GetOrgsByUserID(4, true)
assert.NoError(t, err)
if assert.Len(t, orgs, 1) {
assert.EqualValues(t, 3, orgs[0].ID)
}
2017-01-24 16:16:36 +00:00
orgs, err = GetOrgsByUserID(4, false)
assert.NoError(t, err)
assert.Len(t, orgs, 0)
}
func TestGetOwnedOrgsByUserID(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
orgs, err := GetOwnedOrgsByUserID(2)
assert.NoError(t, err)
if assert.Len(t, orgs, 1) {
assert.EqualValues(t, 3, orgs[0].ID)
}
2017-01-24 16:16:36 +00:00
orgs, err = GetOwnedOrgsByUserID(4)
assert.NoError(t, err)
assert.Len(t, orgs, 0)
}
func TestGetOwnedOrgsByUserIDDesc(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
orgs, err := GetOwnedOrgsByUserIDDesc(5, "id")
assert.NoError(t, err)
if assert.Len(t, orgs, 2) {
assert.EqualValues(t, 7, orgs[0].ID)
assert.EqualValues(t, 6, orgs[1].ID)
}
2017-01-24 16:16:36 +00:00
orgs, err = GetOwnedOrgsByUserIDDesc(4, "id")
assert.NoError(t, err)
assert.Len(t, orgs, 0)
}
func TestGetOrgUsersByUserID(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
2020-01-24 19:00:29 +00:00
orgUsers, err := GetOrgUsersByUserID(5, &SearchOrganizationsOptions{All: true})
2017-01-24 16:16:36 +00:00
assert.NoError(t, err)
if assert.Len(t, orgUsers, 2) {
assert.Equal(t, OrgUser{
ID: orgUsers[0].ID,
OrgID: 6,
UID: 5,
IsPublic: true,
}, *orgUsers[0])
assert.Equal(t, OrgUser{
ID: orgUsers[1].ID,
OrgID: 7,
UID: 5,
IsPublic: false,
}, *orgUsers[1])
}
2017-01-24 16:16:36 +00:00
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
2020-01-24 19:00:29 +00:00
publicOrgUsers, err := GetOrgUsersByUserID(5, &SearchOrganizationsOptions{All: false})
2017-01-24 16:16:36 +00:00
assert.NoError(t, err)
assert.Len(t, publicOrgUsers, 1)
assert.Equal(t, *orgUsers[0], *publicOrgUsers[0])
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
2020-01-24 19:00:29 +00:00
orgUsers, err = GetOrgUsersByUserID(1, &SearchOrganizationsOptions{All: true})
2017-01-24 16:16:36 +00:00
assert.NoError(t, err)
assert.Len(t, orgUsers, 0)
}
func TestGetOrgUsersByOrgID(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
2020-01-24 19:00:29 +00:00
orgUsers, err := GetOrgUsersByOrgID(&FindOrgMembersOpts{
ListOptions: db.ListOptions{},
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
2020-01-24 19:00:29 +00:00
OrgID: 3,
PublicOnly: false,
})
2017-01-24 16:16:36 +00:00
assert.NoError(t, err)
if assert.Len(t, orgUsers, 3) {
assert.Equal(t, OrgUser{
ID: orgUsers[0].ID,
OrgID: 3,
UID: 2,
IsPublic: true,
}, *orgUsers[0])
assert.Equal(t, OrgUser{
ID: orgUsers[1].ID,
OrgID: 3,
UID: 4,
IsPublic: false,
}, *orgUsers[1])
}
2017-01-24 16:16:36 +00:00
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
2020-01-24 19:00:29 +00:00
orgUsers, err = GetOrgUsersByOrgID(&FindOrgMembersOpts{
ListOptions: db.ListOptions{},
OrgID: db.NonexistentID,
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
2020-01-24 19:00:29 +00:00
PublicOnly: false,
})
2017-01-24 16:16:36 +00:00
assert.NoError(t, err)
assert.Len(t, orgUsers, 0)
}
func TestChangeOrgUserStatus(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
testSuccess := func(orgID, userID int64, public bool) {
assert.NoError(t, ChangeOrgUserStatus(orgID, userID, public))
orgUser := db.AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser)
2017-01-24 16:16:36 +00:00
assert.Equal(t, public, orgUser.IsPublic)
}
testSuccess(3, 2, false)
testSuccess(3, 2, false)
testSuccess(3, 4, true)
assert.NoError(t, ChangeOrgUserStatus(db.NonexistentID, db.NonexistentID, true))
2017-01-24 16:16:36 +00:00
}
func TestAddOrgUser(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
testSuccess := func(orgID, userID int64, isPublic bool) {
org := db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
2017-01-24 16:16:36 +00:00
expectedNumMembers := org.NumMembers
if !db.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
2017-01-24 16:16:36 +00:00
expectedNumMembers++
}
assert.NoError(t, AddOrgUser(orgID, userID))
ou := &OrgUser{OrgID: orgID, UID: userID}
db.AssertExistsAndLoadBean(t, ou)
2021-06-07 05:27:09 +00:00
assert.Equal(t, isPublic, ou.IsPublic)
org = db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
2017-01-24 16:16:36 +00: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)
CheckConsistencyFor(t, &User{}, &Team{})
2017-01-24 16:16:36 +00:00
}
func TestRemoveOrgUser(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
2017-01-24 16:16:36 +00:00
testSuccess := func(orgID, userID int64) {
org := db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
2017-01-24 16:16:36 +00:00
expectedNumMembers := org.NumMembers
if db.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
2017-01-24 16:16:36 +00:00
expectedNumMembers--
}
assert.NoError(t, RemoveOrgUser(orgID, userID))
db.AssertNotExistsBean(t, &OrgUser{OrgID: orgID, UID: userID})
org = db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
2017-01-24 16:16:36 +00:00
assert.EqualValues(t, expectedNumMembers, org.NumMembers)
}
testSuccess(3, 4)
testSuccess(3, 4)
err := RemoveOrgUser(7, 5)
assert.Error(t, err)
assert.True(t, IsErrLastOrgOwner(err))
db.AssertExistsAndLoadBean(t, &OrgUser{OrgID: 7, UID: 5})
CheckConsistencyFor(t, &User{}, &Team{})
2017-01-24 16:16:36 +00:00
}
func TestUser_GetUserTeamIDs(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
testSuccess := func(userID int64, expected []int64) {
teamIDs, err := org.GetUserTeamIDs(userID)
assert.NoError(t, err)
assert.Equal(t, expected, teamIDs)
}
testSuccess(2, []int64{1, 2})
testSuccess(4, []int64{2})
testSuccess(db.NonexistentID, []int64{})
2017-01-24 16:16:36 +00:00
}
func TestAccessibleReposEnv_CountRepos(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
testSuccess := func(userID, expectedCount int64) {
env, err := org.AccessibleReposEnv(userID)
assert.NoError(t, err)
count, err := env.CountRepos()
assert.NoError(t, err)
assert.EqualValues(t, expectedCount, count)
}
testSuccess(2, 3)
testSuccess(4, 2)
2017-01-24 16:16:36 +00:00
}
func TestAccessibleReposEnv_RepoIDs(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2019-06-12 19:41:28 +00:00
testSuccess := func(userID, _, pageSize int64, expectedRepoIDs []int64) {
2017-01-24 16:16:36 +00:00
env, err := org.AccessibleReposEnv(userID)
assert.NoError(t, err)
repoIDs, err := env.RepoIDs(1, 100)
assert.NoError(t, err)
assert.Equal(t, expectedRepoIDs, repoIDs)
}
testSuccess(2, 1, 100, []int64{3, 5, 32})
testSuccess(4, 0, 100, []int64{3, 32})
2017-01-24 16:16:36 +00:00
}
func TestAccessibleReposEnv_Repos(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
testSuccess := func(userID int64, expectedRepoIDs []int64) {
env, err := org.AccessibleReposEnv(userID)
assert.NoError(t, err)
repos, err := env.Repos(1, 100)
assert.NoError(t, err)
expectedRepos := make([]*Repository, len(expectedRepoIDs))
for i, repoID := range expectedRepoIDs {
expectedRepos[i] = db.AssertExistsAndLoadBean(t,
2017-01-24 16:16:36 +00:00
&Repository{ID: repoID}).(*Repository)
}
assert.Equal(t, expectedRepos, repos)
}
testSuccess(2, []int64{3, 5, 32})
testSuccess(4, []int64{3, 32})
2017-01-24 16:16:36 +00:00
}
func TestAccessibleReposEnv_MirrorRepos(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
2017-01-24 16:16:36 +00:00
testSuccess := func(userID int64, expectedRepoIDs []int64) {
env, err := org.AccessibleReposEnv(userID)
assert.NoError(t, err)
repos, err := env.MirrorRepos()
assert.NoError(t, err)
expectedRepos := make([]*Repository, len(expectedRepoIDs))
for i, repoID := range expectedRepoIDs {
expectedRepos[i] = db.AssertExistsAndLoadBean(t,
2017-01-24 16:16:36 +00:00
&Repository{ID: repoID}).(*Repository)
}
assert.Equal(t, expectedRepos, repos)
}
testSuccess(2, []int64{5})
testSuccess(4, []int64{})
}
func TestHasOrgVisibleTypePublic(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
const newOrgName = "test-org-public"
org := &User{
Name: newOrgName,
Visibility: structs.VisibleTypePublic,
}
db.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization})
assert.NoError(t, CreateOrganization(org, owner))
org = db.AssertExistsAndLoadBean(t,
&User{Name: org.Name, Type: UserTypeOrganization}).(*User)
test1 := HasOrgOrUserVisible(org, owner)
test2 := HasOrgOrUserVisible(org, user3)
test3 := HasOrgOrUserVisible(org, nil)
2021-06-07 05:27:09 +00:00
assert.True(t, test1) // owner of org
assert.True(t, test2) // user not a part of org
assert.True(t, test3) // logged out user
}
func TestHasOrgVisibleTypeLimited(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
const newOrgName = "test-org-limited"
org := &User{
Name: newOrgName,
Visibility: structs.VisibleTypeLimited,
}
db.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization})
assert.NoError(t, CreateOrganization(org, owner))
org = db.AssertExistsAndLoadBean(t,
&User{Name: org.Name, Type: UserTypeOrganization}).(*User)
test1 := HasOrgOrUserVisible(org, owner)
test2 := HasOrgOrUserVisible(org, user3)
test3 := HasOrgOrUserVisible(org, nil)
2021-06-07 05:27:09 +00:00
assert.True(t, test1) // owner of org
assert.True(t, test2) // user not a part of org
assert.False(t, test3) // logged out user
}
func TestHasOrgVisibleTypePrivate(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
const newOrgName = "test-org-private"
org := &User{
Name: newOrgName,
Visibility: structs.VisibleTypePrivate,
}
db.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization})
assert.NoError(t, CreateOrganization(org, owner))
org = db.AssertExistsAndLoadBean(t,
&User{Name: org.Name, Type: UserTypeOrganization}).(*User)
test1 := HasOrgOrUserVisible(org, owner)
test2 := HasOrgOrUserVisible(org, user3)
test3 := HasOrgOrUserVisible(org, nil)
2021-06-07 05:27:09 +00:00
assert.True(t, test1) // owner of org
assert.False(t, test2) // user not a part of org
assert.False(t, test3) // logged out user
}
func TestGetUsersWhoCanCreateOrgRepo(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
users, err := GetUsersWhoCanCreateOrgRepo(3)
assert.NoError(t, err)
assert.Len(t, users, 2)
var ids []int64
for i := range users {
ids = append(ids, users[i].ID)
}
assert.ElementsMatch(t, ids, []int64{2, 28})
users, err = GetUsersWhoCanCreateOrgRepo(7)
assert.NoError(t, err)
assert.Len(t, users, 1)
assert.EqualValues(t, 5, users[0].ID)
}