Skip to content

Commit 1a378f4

Browse files
committed
add test
1 parent c1517bd commit 1a378f4

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

models/user/search_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package user_test
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/db"
10+
"code.gitea.io/gitea/models/unittest"
11+
"code.gitea.io/gitea/models/user"
12+
13+
"github.com/stretchr/testify/assert"
14+
"xorm.io/builder"
15+
)
16+
17+
func TestBuildCanSeeUserCondition(t *testing.T) {
18+
assert.NoError(t, unittest.PrepareTestDatabase())
19+
20+
getIDs := func(cond builder.Cond) (ids []int64) {
21+
db.GetEngine(db.DefaultContext).Select("id").Table(`user`).
22+
Where(builder.Eq{"is_active": true}.And(cond)).Asc("id").Find(&ids)
23+
return ids
24+
}
25+
26+
getUser := func(t *testing.T, id int64) *user.User {
27+
user, err := user.GetUserByID(db.DefaultContext, id)
28+
assert.NoError(t, err)
29+
if !assert.NotNil(t, user) {
30+
t.FailNow()
31+
}
32+
return user
33+
}
34+
35+
// no login
36+
cond := user.BuildCanSeeUserCondition(nil)
37+
assert.NotNil(t, cond)
38+
ids := getIDs(cond)
39+
assert.Len(t, ids, 24)
40+
assert.EqualValues(t, []int64([]int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 27, 28, 29, 30, 32, 34}), ids)
41+
42+
// normal user
43+
cond = user.BuildCanSeeUserCondition(getUser(t, 5))
44+
ids = getIDs(cond)
45+
assert.Len(t, ids, 28)
46+
assert.EqualValues(t, []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 29, 30, 32, 33, 34, 36}, ids)
47+
48+
// admin
49+
cond = user.BuildCanSeeUserCondition(getUser(t, 1))
50+
assert.Nil(t, cond)
51+
ids = getIDs(cond)
52+
assert.Len(t, ids, 30)
53+
assert.EqualValues(t, []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}, ids)
54+
55+
// private user
56+
cond = user.BuildCanSeeUserCondition(getUser(t, 31))
57+
ids = getIDs(cond)
58+
assert.Len(t, ids, 28)
59+
assert.EqualValues(t, []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 27, 28, 29, 30, 31, 32, 33, 34, 36}, ids)
60+
61+
// restricted user
62+
cond = user.BuildCanSeeUserCondition(getUser(t, 29))
63+
ids = getIDs(cond)
64+
assert.Len(t, ids, 2)
65+
assert.EqualValues(t, []int64{17, 29}, ids)
66+
}

0 commit comments

Comments
 (0)