2017-04-25 09:24:51 +02:00
|
|
|
// Copyright 2017 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 integrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-04-07 20:59:56 +02:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-16 09:53:21 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-04-07 20:59:56 +02:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2017-12-15 22:11:02 +01:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2022-04-03 11:46:48 +02:00
|
|
|
"code.gitea.io/gitea/modules/translation/i18n"
|
2017-07-01 21:48:29 +02:00
|
|
|
|
2017-04-25 09:24:51 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestViewUser(t *testing.T) {
|
2019-11-26 00:21:37 +01:00
|
|
|
defer prepareTestEnv(t)()
|
2017-04-25 09:24:51 +02:00
|
|
|
|
2017-06-10 02:41:36 +02:00
|
|
|
req := NewRequest(t, "GET", "/user2")
|
2017-07-07 21:36:47 +02:00
|
|
|
MakeRequest(t, req, http.StatusOK)
|
2017-04-25 09:24:51 +02:00
|
|
|
}
|
2017-07-01 21:48:29 +02:00
|
|
|
|
|
|
|
func TestRenameUsername(t *testing.T) {
|
2019-11-26 00:21:37 +01:00
|
|
|
defer prepareTestEnv(t)()
|
2017-07-01 21:48:29 +02:00
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
2017-07-07 21:36:47 +02:00
|
|
|
req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
|
2018-05-05 02:28:30 +02:00
|
|
|
"_csrf": GetCSRF(t, session, "/user/settings"),
|
|
|
|
"name": "newUsername",
|
|
|
|
"email": "user2@example.com",
|
2020-12-04 07:20:30 +01:00
|
|
|
"language": "en-US",
|
2017-07-01 21:48:29 +02:00
|
|
|
})
|
2022-03-23 05:54:07 +01:00
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
2017-07-01 21:48:29 +02:00
|
|
|
|
2021-11-24 10:49:20 +01:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "newUsername"})
|
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: "user2"})
|
2017-07-01 21:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameInvalidUsername(t *testing.T) {
|
2019-11-26 00:21:37 +01:00
|
|
|
defer prepareTestEnv(t)()
|
2017-07-01 21:48:29 +02:00
|
|
|
|
|
|
|
invalidUsernames := []string{
|
|
|
|
"%2f*",
|
|
|
|
"%2f.",
|
|
|
|
"%2f..",
|
|
|
|
"%00",
|
|
|
|
"thisHas ASpace",
|
2019-01-24 15:12:17 +01:00
|
|
|
"p<A>tho>lo<gical",
|
2017-07-01 21:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
for _, invalidUsername := range invalidUsernames {
|
|
|
|
t.Logf("Testing username %s", invalidUsername)
|
|
|
|
|
2017-07-07 21:36:47 +02:00
|
|
|
req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, session, "/user/settings"),
|
2017-07-01 21:48:29 +02:00
|
|
|
"name": invalidUsername,
|
|
|
|
"email": "user2@example.com",
|
|
|
|
})
|
2017-07-07 21:36:47 +02:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
2017-07-01 21:48:29 +02:00
|
|
|
assert.Contains(t,
|
|
|
|
htmlDoc.doc.Find(".ui.negative.message").Text(),
|
|
|
|
i18n.Tr("en", "form.alpha_dash_dot_error"),
|
|
|
|
)
|
|
|
|
|
2021-11-24 10:49:20 +01:00
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: invalidUsername})
|
2017-07-01 21:48:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameReservedUsername(t *testing.T) {
|
2019-11-26 00:21:37 +01:00
|
|
|
defer prepareTestEnv(t)()
|
2017-07-01 21:48:29 +02:00
|
|
|
|
|
|
|
reservedUsernames := []string{
|
2022-03-31 06:02:13 +02:00
|
|
|
".",
|
|
|
|
"..",
|
|
|
|
".well-known",
|
2019-09-24 19:12:56 +02:00
|
|
|
"admin",
|
|
|
|
"api",
|
2022-03-31 06:02:13 +02:00
|
|
|
"assets",
|
2019-09-24 19:12:56 +02:00
|
|
|
"attachments",
|
2022-03-31 06:02:13 +02:00
|
|
|
"avatar",
|
2019-09-24 19:12:56 +02:00
|
|
|
"avatars",
|
2022-03-31 06:02:13 +02:00
|
|
|
"captcha",
|
|
|
|
"commits",
|
|
|
|
"debug",
|
|
|
|
"error",
|
2019-09-24 19:12:56 +02:00
|
|
|
"explore",
|
2022-03-31 06:02:13 +02:00
|
|
|
"favicon.ico",
|
|
|
|
"ghost",
|
2019-09-24 19:12:56 +02:00
|
|
|
"issues",
|
|
|
|
"login",
|
2022-03-31 06:02:13 +02:00
|
|
|
"manifest.json",
|
2019-09-24 19:12:56 +02:00
|
|
|
"metrics",
|
2022-03-31 06:02:13 +02:00
|
|
|
"milestones",
|
|
|
|
"new",
|
2019-09-24 19:12:56 +02:00
|
|
|
"notifications",
|
|
|
|
"org",
|
|
|
|
"pulls",
|
2022-03-31 06:02:13 +02:00
|
|
|
"raw",
|
2019-09-24 19:12:56 +02:00
|
|
|
"repo",
|
2022-03-31 06:02:13 +02:00
|
|
|
"repo-avatars",
|
|
|
|
"robots.txt",
|
2019-11-18 21:03:25 +01:00
|
|
|
"search",
|
2022-03-31 06:02:13 +02:00
|
|
|
"serviceworker.js",
|
|
|
|
"ssh_info",
|
|
|
|
"swagger.v1.json",
|
|
|
|
"user",
|
|
|
|
"v2",
|
2017-07-01 21:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
for _, reservedUsername := range reservedUsernames {
|
|
|
|
t.Logf("Testing username %s", reservedUsername)
|
2017-07-07 21:36:47 +02:00
|
|
|
req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
|
2018-05-05 02:28:30 +02:00
|
|
|
"_csrf": GetCSRF(t, session, "/user/settings"),
|
|
|
|
"name": reservedUsername,
|
|
|
|
"email": "user2@example.com",
|
2020-12-04 07:20:30 +01:00
|
|
|
"language": "en-US",
|
2017-07-01 21:48:29 +02:00
|
|
|
})
|
2022-03-23 05:54:07 +01:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusSeeOther)
|
2017-07-01 21:48:29 +02:00
|
|
|
|
2017-12-15 22:11:02 +01:00
|
|
|
req = NewRequest(t, "GET", test.RedirectURL(resp))
|
2017-07-07 21:36:47 +02:00
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
2017-07-01 21:48:29 +02:00
|
|
|
assert.Contains(t,
|
|
|
|
htmlDoc.doc.Find(".ui.negative.message").Text(),
|
2018-02-25 14:56:25 +01:00
|
|
|
i18n.Tr("en", "user.form.name_reserved", reservedUsername),
|
2017-07-01 21:48:29 +02:00
|
|
|
)
|
|
|
|
|
2021-11-24 10:49:20 +01:00
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{Name: reservedUsername})
|
2017-07-01 21:48:29 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-14 18:43:56 +02:00
|
|
|
|
|
|
|
func TestExportUserGPGKeys(t *testing.T) {
|
2019-11-26 00:21:37 +01:00
|
|
|
defer prepareTestEnv(t)()
|
2022-01-20 18:46:10 +01:00
|
|
|
// Export empty key list
|
2019-04-14 18:43:56 +02:00
|
|
|
testExportUserGPGKeys(t, "user1", `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
2022-03-02 17:32:18 +01:00
|
|
|
Note: This user hasn't uploaded any GPG keys.
|
2019-04-14 18:43:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
=twTO
|
|
|
|
-----END PGP PUBLIC KEY BLOCK-----
|
|
|
|
`)
|
2022-01-20 18:46:10 +01:00
|
|
|
// Import key
|
|
|
|
// User1 <user1@example.com>
|
2019-04-14 18:43:56 +02:00
|
|
|
session := loginUser(t, "user1")
|
|
|
|
token := getTokenForLoggedInUser(t, session)
|
|
|
|
testCreateGPGKey(t, session.MakeRequest, token, http.StatusCreated, `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
|
|
|
|
|
|
mQENBFyy/VUBCADJ7zbM20Z1RWmFoVgp5WkQfI2rU1Vj9cQHes9i42wVLLtcbPeo
|
|
|
|
QzubgzvMPITDy7nfWxgSf83E23DoHQ1ACFbQh/6eFSRrjsusp3YQ/08NSfPPbcu8
|
|
|
|
0M5G+VGwSfzS5uEcwBVQmHyKdcOZIERTNMtYZx1C3bjLD1XVJHvWz9D72Uq4qeO3
|
|
|
|
8SR+lzp5n6ppUakcmRnxt3nGRBj1+hEGkdgzyPo93iy+WioegY2lwCA9xMEo5dah
|
|
|
|
BmYxWx51zyiXYlReTaxlyb3/nuSUt8IcW3Q8zjdtJj4Nu8U1SpV8EdaA1I9IPbHW
|
|
|
|
510OSLmD3XhqHH5m6mIxL1YoWxk3V7gpDROtABEBAAG0GVVzZXIxIDx1c2VyMUBl
|
|
|
|
eGFtcGxlLmNvbT6JAU4EEwEIADgWIQTQEbrYxmXsp1z3j7z9+v0I6RSEHwUCXLL9
|
|
|
|
VQIbAwULCQgHAgYVCgkICwIEFgIDAQIeAQIXgAAKCRD9+v0I6RSEH22YCACFqL5+
|
|
|
|
6M0m18AMC/pumcpnnmvAS1GrrKTF8nOROA1augZwp1WCNuKw2R6uOJIHANrYEC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0F1SuzREUBH
|
|
|
|
=WFf5
|
|
|
|
-----END PGP PUBLIC KEY BLOCK-----
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testExportUserGPGKeys(t *testing.T, user, expected string) {
|
|
|
|
session := loginUser(t, user)
|
|
|
|
t.Logf("Testing username %s export gpg keys", user)
|
|
|
|
req := NewRequest(t, "GET", "/"+user+".gpg")
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
2022-01-20 18:46:10 +01:00
|
|
|
// t.Log(resp.Body.String())
|
2019-04-14 18:43:56 +02:00
|
|
|
assert.Equal(t, expected, resp.Body.String())
|
|
|
|
}
|
2022-04-07 20:59:56 +02:00
|
|
|
|
|
|
|
func TestListStopWatches(t *testing.T) {
|
|
|
|
defer prepareTestEnv(t)()
|
|
|
|
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
|
|
|
|
|
|
|
|
session := loginUser(t, owner.Name)
|
|
|
|
req := NewRequestf(t, "GET", "/user/stopwatches")
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
var apiWatches []*api.StopWatch
|
|
|
|
DecodeJSON(t, resp, &apiWatches)
|
2022-06-13 11:37:59 +02:00
|
|
|
stopwatch := unittest.AssertExistsAndLoadBean(t, &issues_model.Stopwatch{UserID: owner.ID}).(*issues_model.Stopwatch)
|
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: stopwatch.IssueID}).(*issues_model.Issue)
|
2022-04-07 20:59:56 +02:00
|
|
|
if assert.Len(t, apiWatches, 1) {
|
|
|
|
assert.EqualValues(t, stopwatch.CreatedUnix.AsTime().Unix(), apiWatches[0].Created.Unix())
|
|
|
|
assert.EqualValues(t, issue.Index, apiWatches[0].IssueIndex)
|
|
|
|
assert.EqualValues(t, issue.Title, apiWatches[0].IssueTitle)
|
|
|
|
assert.EqualValues(t, repo.Name, apiWatches[0].RepoName)
|
|
|
|
assert.EqualValues(t, repo.OwnerName, apiWatches[0].RepoOwnerName)
|
|
|
|
assert.Greater(t, int64(apiWatches[0].Seconds), int64(0))
|
|
|
|
}
|
|
|
|
}
|