forgejo-sdk/gitea/user_test.go
spawn2kill e7bf8a6842 added pagination options for listing requests (#205)
fix golint issue

small improvements

ListMyRepos now receives a value instead of a pointer

reverted makefile changes

added sleep in order to increase CI stability

resolved conflicts

Use RepositoryMeta struct on Issues (#267)

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/267
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>

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>

Implement Repo Transfer (#264)

fix test

check version

add TestRepoTransfer

add TransferRepo

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/264
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>

Add notifications (#226)

Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: spawn2kill <spawn2kill@noreply.gitea.io>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: John Olheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/226
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>

Use StateType (#265)

use StateType

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/265
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>

opt setDefaults on issue_comment

trigger CI

Trigger notification

resolved conflicts

Fix AddEmail (#260)

Merge branch 'master' into fix-AddEMail

Add ListIssueCommentOptions for optional param (#243)

Merge branch 'master' into impruve-ListXIssueComments

Remove ListUserIssues() ... (#262)

Merge branch 'master' into rm-ListUserIssues

correct version (#257)

Merge branch 'master' into targed-version

Extend Issue Struct (#258)

fix

extend IssueTest

add Repository to Issue struct

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/258
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>

Merge branch 'master' into targed-version

format code (#245)

fix if test is to fast

remove lgtm bot config (gitea handle this now)

format code

Add GetIssueComment (#216)

Merge branch 'master' into update-comment-api

Fix & Refactor UserApp Functions (#247)

Merge branch 'master' into refactor-user-app

Add SearchRepo API Call (#254)

Merge branch 'master' into search_repo

IssueSubscribtion: Check http Status responce (#242)

Merge branch 'master' into impruve-issueWatch

Refactor RepoWatch (#241)

Merge branch 'master' into refactor-repoWatch

Add ListMilestoneOption to ListRepoMilestones (#244)

use StateType

use PageSize

adjut test since gitea bug got fixed (#gitea/10047)

add TestMilestones

add optional ListMilestoneOption

format

Add ListOptions struct (#249)

same struct as in models/list_options.go

add mising license header

add ListOptions struct

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>

[README] add import path (#239)

add import path

add import path to readme

Changelog v0.11.0 (#235)

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: John Olheiser <john.olheiser@gmail.com>

Add TestMyUser (#237)

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/237
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
Reviewed-by: Andrew Thornton <art27@cantab.net>

Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/239
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: John Olheiser <john.olheiser@gmail.com>

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/244
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: Andrew Thornton <art27@cantab.net>

Merge branch 'master' into refactor-repoWatch

Add ListOptions struct (#249)

same struct as in models/list_options.go

add mising license header

add ListOptions struct

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>

add TEST

fix GetMyWatchedRepos

[README] add import path (#239)

add import path

add import path to readme

Changelog v0.11.0 (#235)

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: John Ol...

Co-authored-by: Hilário Coelho <hilario.coelho@securityside.com>
Co-authored-by: 6543 <6543@noreply.gitea.io>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/205
Reviewed-by: 6543 <6543@noreply.gitea.io>
Reviewed-by: Andrew Thornton <art27@cantab.net>
2020-02-05 07:59:55 +00:00

181 lines
4.8 KiB
Go

// 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)
}
func TestUserApp(t *testing.T) {
log.Println("== TestUserApp ==")
c := newTestClient()
result, err := c.ListAccessTokens(c.username, c.password, ListAccessTokensOptions{})
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.EqualValues(t, "gitea-admin", result[0].Name)
t1, err := c.CreateAccessToken(c.username, c.password, CreateAccessTokenOption{Name: "TestCreateAccessToken"})
assert.NoError(t, err)
assert.EqualValues(t, "TestCreateAccessToken", t1.Name)
result, _ = c.ListAccessTokens(c.username, c.password, ListAccessTokensOptions{})
assert.Len(t, result, 2)
err = c.DeleteAccessToken(c.username, c.password, t1.ID)
assert.NoError(t, err)
result, _ = c.ListAccessTokens(c.username, c.password, ListAccessTokensOptions{})
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(ListFollowersOptions{})
assert.NoError(t, err)
assert.Len(t, f, 2)
// ListFollowers of A
f, err = c.ListFollowers(uA, ListFollowersOptions{})
assert.NoError(t, err)
assert.Len(t, f, 1)
// ListMyFollowing of me
f, err = c.ListMyFollowing(ListFollowingOptions{})
assert.NoError(t, err)
assert.Len(t, f, 0)
// ListFollowing of A
f, err = c.ListFollowing(uA, ListFollowingOptions{})
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()
userN := "TestUserEmail"
createTestUser(t, userN, c)
c.sudo = userN
// ListEmails
el, err := c.ListEmails(ListEmailsOptions{})
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)
el, err = c.ListEmails(ListEmailsOptions{})
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)
el, err = c.ListEmails(ListEmailsOptions{})
assert.NoError(t, err)
assert.Len(t, el, 2)
err = c.DeleteEmail(DeleteEmailOption{Emails: []string{mails[0]}})
assert.NoError(t, err)
}
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
}