2020-01-28 06:11:26 +00:00
|
|
|
// Copyright 2020 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 gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMyUser(t *testing.T) {
|
|
|
|
log.Println("== TestMyUser ==")
|
|
|
|
c := newTestClient()
|
2020-09-14 03:37:09 +01:00
|
|
|
user, _, err := c.GetMyUserInfo()
|
2020-01-28 06:11:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.EqualValues(t, 1, user.ID)
|
|
|
|
assert.EqualValues(t, "test01", user.UserName)
|
|
|
|
assert.EqualValues(t, "test01@gitea.io", user.Email)
|
|
|
|
assert.EqualValues(t, "", user.FullName)
|
|
|
|
assert.EqualValues(t, getGiteaURL()+"/user/avatar/test01/-1", user.AvatarURL)
|
|
|
|
assert.Equal(t, true, user.IsAdmin)
|
|
|
|
}
|
2020-02-02 07:05:40 +00:00
|
|
|
|
|
|
|
func TestUserApp(t *testing.T) {
|
|
|
|
log.Println("== TestUserApp ==")
|
|
|
|
c := newTestClient()
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
result, _, err := c.ListAccessTokens(ListAccessTokensOptions{})
|
2020-02-02 07:05:40 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, result, 1)
|
|
|
|
assert.EqualValues(t, "gitea-admin", result[0].Name)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
t1, _, err := c.CreateAccessToken(CreateAccessTokenOption{Name: "TestCreateAccessToken"})
|
2020-02-02 07:05:40 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, "TestCreateAccessToken", t1.Name)
|
2020-09-14 03:37:09 +01:00
|
|
|
result, _, _ = c.ListAccessTokens(ListAccessTokensOptions{})
|
2020-02-02 07:05:40 +00:00
|
|
|
assert.Len(t, result, 2)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.DeleteAccessToken(t1.ID)
|
2020-02-02 07:05:40 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
result, _, _ = c.ListAccessTokens(ListAccessTokensOptions{})
|
2020-02-02 07:05:40 +00:00
|
|
|
assert.Len(t, result, 1)
|
|
|
|
}
|
2020-02-02 09:03:40 +00:00
|
|
|
|
2020-02-04 08:08:14 +00:00
|
|
|
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)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
ul, _, err := c.SearchUsers(SearchUsersOption{KeyWord: "other"})
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, ul, 1)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
ul, _, err = c.SearchUsers(SearchUsersOption{KeyWord: "notInTESTcase"})
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, ul, 0)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
ul, _, err = c.SearchUsers(SearchUsersOption{KeyWord: "It"})
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, ul, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserFollow(t *testing.T) {
|
|
|
|
log.Println("== TestUserFollow ==")
|
|
|
|
c := newTestClient()
|
2020-09-14 03:37:09 +01:00
|
|
|
me, _, _ := c.GetMyUserInfo()
|
2020-02-04 08:08:14 +00:00
|
|
|
|
|
|
|
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
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err := c.Follow(me.UserName)
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
c.sudo = uB
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.Follow(me.UserName)
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.Follow(uC)
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
c.sudo = uC
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.Follow(me.UserName)
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.Follow(uA)
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.Follow(uB)
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// C unfollow me
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.Unfollow(me.UserName)
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// ListMyFollowers of me
|
|
|
|
c.sudo = ""
|
2020-09-14 03:37:09 +01:00
|
|
|
f, _, err := c.ListMyFollowers(ListFollowersOptions{})
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, f, 2)
|
|
|
|
|
|
|
|
// ListFollowers of A
|
2020-09-14 03:37:09 +01:00
|
|
|
f, _, err = c.ListFollowers(uA, ListFollowersOptions{})
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, f, 1)
|
|
|
|
|
|
|
|
// ListMyFollowing of me
|
2020-09-14 03:37:09 +01:00
|
|
|
f, _, err = c.ListMyFollowing(ListFollowingOptions{})
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, f, 0)
|
|
|
|
|
|
|
|
// ListFollowing of A
|
2020-09-14 03:37:09 +01:00
|
|
|
f, _, err = c.ListFollowing(uA, ListFollowingOptions{})
|
2020-02-04 08:08:14 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, f, 1)
|
|
|
|
assert.EqualValues(t, me.ID, f[0].ID)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
isFollow, _ := c.IsFollowing(uA)
|
|
|
|
assert.False(t, isFollow)
|
|
|
|
isFollow, _ = c.IsUserFollowing(uB, uC)
|
|
|
|
assert.True(t, isFollow)
|
2020-02-04 08:08:14 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 03:23:06 +00:00
|
|
|
func TestUserEmail(t *testing.T) {
|
|
|
|
log.Println("== TestUserEmail ==")
|
|
|
|
c := newTestClient()
|
|
|
|
userN := "TestUserEmail"
|
|
|
|
createTestUser(t, userN, c)
|
|
|
|
c.sudo = userN
|
|
|
|
|
|
|
|
// ListEmails
|
2020-09-14 03:37:09 +01:00
|
|
|
el, _, err := c.ListEmails(ListEmailsOptions{})
|
2020-02-03 03:23:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, el, 1)
|
|
|
|
assert.EqualValues(t, "testuseremail@gitea.io", el[0].Email)
|
|
|
|
assert.True(t, el[0].Primary)
|
|
|
|
|
|
|
|
// AddEmail
|
|
|
|
mails := []string{"wow@mail.send", "speed@mail.me"}
|
2020-09-14 03:37:09 +01:00
|
|
|
el, _, err = c.AddEmail(CreateEmailOption{Emails: mails})
|
2020-02-03 03:23:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, el, 2)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, _, err = c.AddEmail(CreateEmailOption{Emails: []string{mails[1]}})
|
2020-02-03 03:23:06 +00:00
|
|
|
assert.Error(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
el, _, err = c.ListEmails(ListEmailsOptions{})
|
2020-02-03 03:23:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, el, 3)
|
|
|
|
|
|
|
|
// DeleteEmail
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.DeleteEmail(DeleteEmailOption{Emails: []string{mails[1]}})
|
2020-02-03 03:23:06 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.DeleteEmail(DeleteEmailOption{Emails: []string{"imaginary@e.de"}})
|
2020-02-03 03:23:06 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
el, _, err = c.ListEmails(ListEmailsOptions{})
|
2020-02-03 03:23:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, el, 2)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.DeleteEmail(DeleteEmailOption{Emails: []string{mails[0]}})
|
2020-02-03 03:23:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-04-05 04:23:50 +01:00
|
|
|
func TestGetUserByID(t *testing.T) {
|
|
|
|
log.Println("== TestGetUserByID ==")
|
|
|
|
c := newTestClient()
|
|
|
|
|
|
|
|
user1 := createTestUser(t, "user1", c)
|
|
|
|
user2 := createTestUser(t, "user2", c)
|
|
|
|
|
|
|
|
r1, _, err := c.GetUserByID(user1.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, r1)
|
|
|
|
assert.Equal(t, user1.UserName, r1.UserName)
|
|
|
|
|
|
|
|
r2, _, err := c.GetUserByID(user2.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, r2)
|
|
|
|
assert.Equal(t, user2.UserName, r2.UserName)
|
|
|
|
|
|
|
|
r3, _, err := c.GetUserByID(42)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, r3)
|
|
|
|
|
|
|
|
r4, _, err := c.GetUserByID(-1)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, r4)
|
|
|
|
}
|
|
|
|
|
2020-02-02 09:03:40 +00:00
|
|
|
func createTestUser(t *testing.T, username string, client *Client) *User {
|
2020-09-14 03:37:09 +01:00
|
|
|
user, _, _ := client.GetUserInfo(username)
|
2020-02-02 09:03:40 +00:00
|
|
|
if user.ID != 0 {
|
|
|
|
return user
|
|
|
|
}
|
2020-11-12 21:27:28 +00:00
|
|
|
user, _, err := client.AdminCreateUser(CreateUserOption{Username: username, Password: username + "!1234", Email: username + "@gitea.io", MustChangePassword: OptionalBool(false), SendNotify: false})
|
2020-02-02 09:03:40 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
return user
|
|
|
|
}
|