extend SearchUsers (#248)

Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: spawn2kill <spawn2kill@noreply.gitea.io>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/248
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
This commit is contained in:
6543 2020-02-04 08:08:14 +00:00 committed by lafriks
parent 1970388f6e
commit 3c04db72f4
2 changed files with 114 additions and 3 deletions

View file

@ -4,15 +4,41 @@
package gitea package gitea
import "fmt" import (
"fmt"
"net/url"
)
type searchUsersResponse struct { type searchUsersResponse struct {
Users []*User `json:"data"` Users []*User `json:"data"`
} }
// SearchUsersOption options for SearchUsers
type SearchUsersOption struct {
ListOptions
KeyWord string
}
// QueryEncode turns options into querystring argument
func (opt *SearchUsersOption) QueryEncode() string {
query := make(url.Values)
if opt.Page > 0 {
query.Add("page", fmt.Sprintf("%d", opt.Page))
}
if opt.PageSize > 0 {
query.Add("limit", fmt.Sprintf("%d", opt.PageSize))
}
if len(opt.KeyWord) > 0 {
query.Add("q", opt.KeyWord)
}
return query.Encode()
}
// SearchUsers finds users by query // SearchUsers finds users by query
func (c *Client) SearchUsers(query string, limit int) ([]*User, error) { func (c *Client) SearchUsers(opt SearchUsersOption) ([]*User, error) {
link, _ := url.Parse("/users/search")
link.RawQuery = opt.QueryEncode()
resp := new(searchUsersResponse) resp := new(searchUsersResponse)
err := c.getParsedResponse("GET", fmt.Sprintf("/users/search?q=%s&limit=%d", query, limit), nil, nil, &resp) err := c.getParsedResponse("GET", link.String(), nil, nil, &resp)
return resp.Users, err return resp.Users, err
} }

View file

@ -46,6 +46,91 @@ func TestUserApp(t *testing.T) {
assert.Len(t, result, 1) assert.Len(t, result, 1)
} }
func TestUserSearch(t *testing.T) {
log.Println("== TestUserSearch ==")
c := newTestClient()
createTestUser(t, "tu1", c)
createTestUser(t, "eatIt_2", c)
createTestUser(t, "thirdIs3", c)
createTestUser(t, "advancedUser", c)
createTestUser(t, "1n2n3n", c)
createTestUser(t, "otherIt", c)
ul, err := c.SearchUsers(SearchUsersOption{KeyWord: "other"})
assert.NoError(t, err)
assert.Len(t, ul, 1)
ul, err = c.SearchUsers(SearchUsersOption{KeyWord: "notInTESTcase"})
assert.NoError(t, err)
assert.Len(t, ul, 0)
ul, err = c.SearchUsers(SearchUsersOption{KeyWord: "It"})
assert.NoError(t, err)
assert.Len(t, ul, 2)
}
func TestUserFollow(t *testing.T) {
log.Println("== TestUserFollow ==")
c := newTestClient()
me, _ := c.GetMyUserInfo()
uA := "uFollow_A"
uB := "uFollow_B"
uC := "uFollow_C"
createTestUser(t, uA, c)
createTestUser(t, uB, c)
createTestUser(t, uC, c)
// A follow ME
// B follow C & ME
// C follow A & B & ME
c.sudo = uA
err := c.Follow(me.UserName)
assert.NoError(t, err)
c.sudo = uB
err = c.Follow(me.UserName)
assert.NoError(t, err)
err = c.Follow(uC)
assert.NoError(t, err)
c.sudo = uC
err = c.Follow(me.UserName)
assert.NoError(t, err)
err = c.Follow(uA)
assert.NoError(t, err)
err = c.Follow(uB)
assert.NoError(t, err)
// C unfollow me
err = c.Unfollow(me.UserName)
assert.NoError(t, err)
// ListMyFollowers of me
c.sudo = ""
f, err := c.ListMyFollowers(1)
assert.NoError(t, err)
assert.Len(t, f, 2)
// ListFollowers of A
f, err = c.ListFollowers(uA, 1)
assert.NoError(t, err)
assert.Len(t, f, 1)
// ListMyFollowing of me
f, err = c.ListMyFollowing(1)
assert.NoError(t, err)
assert.Len(t, f, 0)
// ListFollowing of A
f, err = c.ListFollowing(uA, 1)
assert.NoError(t, err)
assert.Len(t, f, 1)
assert.EqualValues(t, me.ID, f[0].ID)
assert.False(t, c.IsFollowing(uA))
assert.True(t, c.IsUserFollowing(uB, uC))
}
func TestUserEmail(t *testing.T) { func TestUserEmail(t *testing.T) {
log.Println("== TestUserEmail ==") log.Println("== TestUserEmail ==")
c := newTestClient() c := newTestClient()