0
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-12-13 23:00:34 +01:00
gitea/models/userlist_test.go
Lunny Xiao a4bfef265d
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db

* Fix lint

* Fix lint

* Fix test

* Fix lint

* Fix lint

* revert unnecessary change

* Fix test

* Fix wrong replace string

* Use *Context

* Correct committer spelling and fix wrong replaced words

Co-authored-by: zeripath <art27@cantab.net>
2021-09-19 19:49:59 +08:00

92 lines
2.5 KiB
Go

// 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"
"code.gitea.io/gitea/models/db"
"github.com/stretchr/testify/assert"
)
func TestUserListIsPublicMember(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
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 {
t.Run(fmt.Sprintf("IsPublicMemberOfOrdIg%d", v.orgid), func(t *testing.T) {
testUserListIsPublicMember(t, v.orgid, v.expected)
})
}
}
func testUserListIsPublicMember(t *testing.T, orgID int64, expected map[int64]bool) {
org, err := GetUserByID(orgID)
assert.NoError(t, err)
assert.NoError(t, org.GetMembers())
assert.Equal(t, expected, org.MembersIsPublic)
}
func TestUserListIsUserOrgOwner(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
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 {
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) {
org, err := GetUserByID(orgID)
assert.NoError(t, err)
assert.NoError(t, org.GetMembers())
assert.Equal(t, expected, org.Members.IsUserOrgOwner(orgID))
}
func TestUserListIsTwoFaEnrolled(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
tt := []struct {
orgid int64
expected map[int64]bool
}{
{3, map[int64]bool{2: false, 4: false, 28: false}},
{6, map[int64]bool{5: false, 28: false}},
{7, map[int64]bool{5: false}},
{25, map[int64]bool{24: true}},
{22, map[int64]bool{}},
}
for _, v := range tt {
t.Run(fmt.Sprintf("IsTwoFaEnrolledOfOrdIg%d", v.orgid), func(t *testing.T) {
testUserListIsTwoFaEnrolled(t, v.orgid, v.expected)
})
}
}
func testUserListIsTwoFaEnrolled(t *testing.T, orgID int64, expected map[int64]bool) {
org, err := GetUserByID(orgID)
assert.NoError(t, err)
assert.NoError(t, org.GetMembers())
assert.Equal(t, expected, org.Members.GetTwoFaStatus())
}