Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for valid renamed usernames #2077

Merged
merged 5 commits into from
Jul 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions integrations/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"net/http"
"testing"

"code.gitea.io/gitea/models"

"github.com/Unknwon/i18n"
"github.com/stretchr/testify/assert"
)

Expand All @@ -18,3 +21,99 @@ func TestViewUser(t *testing.T) {
resp := MakeRequest(req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
}

func TestRenameUsername(t *testing.T) {
prepareTestEnv(t)

session := loginUser(t, "user2")

req := NewRequest(t, "GET", "/user/settings")
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

htmlDoc := NewHTMLParser(t, resp.Body)
req = NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
"_csrf": htmlDoc.GetCSRF(),
"name": "newUsername",
"email": "[email protected]",
})
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

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)
req := NewRequest(t, "GET", "/user/settings")
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

htmlDoc := NewHTMLParser(t, resp.Body)
req = NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
"_csrf": htmlDoc.GetCSRF(),
"name": invalidUsername,
"email": "[email protected]",
})
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
htmlDoc = NewHTMLParser(t, resp.Body)
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)
req := NewRequest(t, "GET", "/user/settings")
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)

htmlDoc := NewHTMLParser(t, resp.Body)
req = NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
"_csrf": htmlDoc.GetCSRF(),
"name": reservedUsername,
"email": "[email protected]",
})
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)

req = NewRequest(t, "GET", "/user/settings")
resp = session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
htmlDoc = NewHTMLParser(t, resp.Body)
assert.Contains(t,
htmlDoc.doc.Find(".ui.negative.message").Text(),
i18n.Tr("en", "user.newName_reserved"),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now you have 3 identical loops, have them be a common function like so testUpdateUser(t *testing.T, username, email string, code http.StatusCode) 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bkcsoft They're not identical. A lot of the setup code (e.g. getting the CSRF token) is shared, but once you make the POST /user/settings request, they have little in common (different status codes, may or may not have to follow a redirect, may or may not check for whether rename succeeded, have to check for different error messages).

The boilerplate setup code is also shared with tests from other files, so IMO the best solution is to add helper functions for things like getting a CSRF token.


models.AssertNotExistsBean(t, &models.User{Name: reservedUsername})
}
}
2 changes: 1 addition & 1 deletion modules/auth/user_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (f *SignInForm) Validate(ctx *macaron.Context, errs binding.Errors) binding

// UpdateProfileForm form for updating profile
type UpdateProfileForm struct {
Name string `binding:"OmitEmpty;MaxSize(35)"`
Name string `binding:"Required;AlphaDashDot;MaxSize(35)"`
FullName string `binding:"MaxSize(100)"`
Email string `binding:"Required;Email;MaxSize(254)"`
KeepEmailPrivate bool
Expand Down