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()
|
|
|
|
user, err := c.GetMyUserInfo()
|
|
|
|
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-05-19 23:16:37 +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-05-19 23:16:37 +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-05-19 23:16:37 +01:00
|
|
|
result, _ = c.ListAccessTokens(ListAccessTokensOptions{})
|
2020-02-02 07:05:40 +00:00
|
|
|
assert.Len(t, result, 2)
|
|
|
|
|
2020-05-19 23:16:37 +01:00
|
|
|
err = c.DeleteAccessToken(t1.ID)
|
2020-02-02 07:05:40 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-19 23:16:37 +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)
|
|
|
|
|
|
|
|
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 = ""
|
2020-02-05 07:59:55 +00: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-02-05 07:59:55 +00: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-02-05 07:59:55 +00: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-02-05 07:59:55 +00: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)
|
|
|
|
|
|
|
|
assert.False(t, c.IsFollowing(uA))
|
|
|
|
assert.True(t, c.IsUserFollowing(uB, uC))
|
|
|
|
}
|
|
|
|
|
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-02-05 07:59:55 +00: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"}
|
|
|
|
el, err = c.AddEmail(CreateEmailOption{Emails: mails})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, el, 2)
|
|
|
|
_, err = c.AddEmail(CreateEmailOption{Emails: []string{mails[1]}})
|
|
|
|
assert.Error(t, err)
|
2020-02-05 07:59:55 +00:00
|
|
|
el, err = c.ListEmails(ListEmailsOptions{})
|
2020-02-03 03:23:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, el, 3)
|
|
|
|
|
|
|
|
// DeleteEmail
|
|
|
|
err = c.DeleteEmail(DeleteEmailOption{Emails: []string{mails[1]}})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = c.DeleteEmail(DeleteEmailOption{Emails: []string{"imaginary@e.de"}})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2020-02-05 07:59:55 +00:00
|
|
|
el, err = c.ListEmails(ListEmailsOptions{})
|
2020-02-03 03:23:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, el, 2)
|
|
|
|
err = c.DeleteEmail(DeleteEmailOption{Emails: []string{mails[0]}})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-02-02 09:03:40 +00:00
|
|
|
func createTestUser(t *testing.T, username string, client *Client) *User {
|
|
|
|
bFalse := false
|
|
|
|
user, _ := client.GetUserInfo(username)
|
|
|
|
if user.ID != 0 {
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
user, err := client.AdminCreateUser(CreateUserOption{Username: username, Password: username + "!1234", Email: username + "@gitea.io", MustChangePassword: &bFalse, SendNotify: bFalse})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
return user
|
|
|
|
}
|