From 3c04db72f4030a2f81dfd30ff077e5061ce5c757 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Tue, 4 Feb 2020 08:08:14 +0000 Subject: [PATCH] extend SearchUsers (#248) Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lunny Xiao Co-authored-by: spawn2kill Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/248 Reviewed-by: Lunny Xiao Reviewed-by: lafriks --- gitea/user_search.go | 32 +++++++++++++++-- gitea/user_test.go | 85 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 3 deletions(-) diff --git a/gitea/user_search.go b/gitea/user_search.go index 5785bd7..5e09821 100644 --- a/gitea/user_search.go +++ b/gitea/user_search.go @@ -4,15 +4,41 @@ package gitea -import "fmt" +import ( + "fmt" + "net/url" +) type searchUsersResponse struct { 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 -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) - 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 } diff --git a/gitea/user_test.go b/gitea/user_test.go index 3978b03..c9ca328 100644 --- a/gitea/user_test.go +++ b/gitea/user_test.go @@ -46,6 +46,91 @@ func TestUserApp(t *testing.T) { 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) { log.Println("== TestUserEmail ==") c := newTestClient()