2017-04-25 15:24:51 +08: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"
|
|
|
|
|
2017-07-02 03:48:29 +08:00
|
|
|
"code.gitea.io/gitea/models"
|
2017-12-16 05:11:02 +08:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2017-07-02 03:48:29 +08:00
|
|
|
|
|
|
|
"github.com/Unknwon/i18n"
|
2017-04-25 15:24:51 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestViewUser(t *testing.T) {
|
2017-04-28 21:23:28 +08:00
|
|
|
prepareTestEnv(t)
|
2017-04-25 15:24:51 +08:00
|
|
|
|
2017-06-10 08:41:36 +08:00
|
|
|
req := NewRequest(t, "GET", "/user2")
|
2017-07-08 03:36:47 +08:00
|
|
|
MakeRequest(t, req, http.StatusOK)
|
2017-04-25 15:24:51 +08:00
|
|
|
}
|
2017-07-02 03:48:29 +08:00
|
|
|
|
|
|
|
func TestRenameUsername(t *testing.T) {
|
|
|
|
prepareTestEnv(t)
|
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
2017-07-08 03:36:47 +08:00
|
|
|
req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
|
2018-05-05 08:28:30 +08:00
|
|
|
"_csrf": GetCSRF(t, session, "/user/settings"),
|
|
|
|
"name": "newUsername",
|
|
|
|
"email": "user2@example.com",
|
|
|
|
"language": "en-us",
|
2017-07-02 03:48:29 +08:00
|
|
|
})
|
2017-07-08 03:36:47 +08:00
|
|
|
session.MakeRequest(t, req, http.StatusFound)
|
2017-07-02 03:48:29 +08:00
|
|
|
|
|
|
|
models.AssertExistsAndLoadBean(t, &models.User{Name: "newUsername"})
|
|
|
|
models.AssertNotExistsBean(t, &models.User{Name: "user2"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameInvalidUsername(t *testing.T) {
|
|
|
|
prepareTestEnv(t)
|
|
|
|
|
|
|
|
invalidUsernames := []string{
|
|
|
|
"%2f*",
|
|
|
|
"%2f.",
|
|
|
|
"%2f..",
|
|
|
|
"%00",
|
|
|
|
"thisHas ASpace",
|
|
|
|
}
|
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
for _, invalidUsername := range invalidUsernames {
|
|
|
|
t.Logf("Testing username %s", invalidUsername)
|
|
|
|
|
2017-07-08 03:36:47 +08:00
|
|
|
req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, session, "/user/settings"),
|
2017-07-02 03:48:29 +08:00
|
|
|
"name": invalidUsername,
|
|
|
|
"email": "user2@example.com",
|
|
|
|
})
|
2017-07-08 03:36:47 +08:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
2017-07-02 03:48:29 +08:00
|
|
|
assert.Contains(t,
|
|
|
|
htmlDoc.doc.Find(".ui.negative.message").Text(),
|
|
|
|
i18n.Tr("en", "form.alpha_dash_dot_error"),
|
|
|
|
)
|
|
|
|
|
|
|
|
models.AssertNotExistsBean(t, &models.User{Name: invalidUsername})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameReservedUsername(t *testing.T) {
|
|
|
|
prepareTestEnv(t)
|
|
|
|
|
|
|
|
reservedUsernames := []string{
|
|
|
|
"help",
|
|
|
|
"user",
|
|
|
|
"template",
|
|
|
|
}
|
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
for _, reservedUsername := range reservedUsernames {
|
|
|
|
t.Logf("Testing username %s", reservedUsername)
|
2017-07-08 03:36:47 +08:00
|
|
|
req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
|
2018-05-05 08:28:30 +08:00
|
|
|
"_csrf": GetCSRF(t, session, "/user/settings"),
|
|
|
|
"name": reservedUsername,
|
|
|
|
"email": "user2@example.com",
|
|
|
|
"language": "en-us",
|
2017-07-02 03:48:29 +08:00
|
|
|
})
|
2017-07-08 03:36:47 +08:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusFound)
|
2017-07-02 03:48:29 +08:00
|
|
|
|
2017-12-16 05:11:02 +08:00
|
|
|
req = NewRequest(t, "GET", test.RedirectURL(resp))
|
2017-07-08 03:36:47 +08:00
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
2017-07-02 03:48:29 +08:00
|
|
|
assert.Contains(t,
|
|
|
|
htmlDoc.doc.Find(".ui.negative.message").Text(),
|
2018-02-25 21:56:25 +08:00
|
|
|
i18n.Tr("en", "user.form.name_reserved", reservedUsername),
|
2017-07-02 03:48:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
models.AssertNotExistsBean(t, &models.User{Name: reservedUsername})
|
|
|
|
}
|
|
|
|
}
|