mirror of
https://github.com/go-gitea/gitea
synced 2025-07-13 14:07:20 +00:00
Improve "generate new access token" form (#33730)
Fix: https://github.com/go-gitea/gitea/issues/33519 As discussed in [PR #33614](https://github.com/go-gitea/gitea/pull/33614), the ScopedAccessTokenSelector Vue component is not particularly useful. This PR removes the component and reverts to using HTML templates. It also introduces some (hopefully) useful refactoring. The Vue component was causing the UX bug reported in the linked issue. Required form fields are now properly working, as expected (see screenshot).  --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@ -249,55 +249,19 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
|
||||
// token has to be unique this counter take care of
|
||||
var tokenCounter int64
|
||||
|
||||
// getTokenForLoggedInUser returns a token for a logged in user.
|
||||
// The scope is an optional list of snake_case strings like the frontend form fields,
|
||||
// but without the "scope_" prefix.
|
||||
// getTokenForLoggedInUser returns a token for a logged-in user.
|
||||
func getTokenForLoggedInUser(t testing.TB, session *TestSession, scopes ...auth.AccessTokenScope) string {
|
||||
t.Helper()
|
||||
var token string
|
||||
req := NewRequest(t, "GET", "/user/settings/applications")
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
var csrf string
|
||||
for _, cookie := range resp.Result().Cookies() {
|
||||
if cookie.Name != "_csrf" {
|
||||
continue
|
||||
}
|
||||
csrf = cookie.Value
|
||||
break
|
||||
}
|
||||
if csrf == "" {
|
||||
doc := NewHTMLParser(t, resp.Body)
|
||||
csrf = doc.GetCSRF()
|
||||
}
|
||||
assert.NotEmpty(t, csrf)
|
||||
urlValues := url.Values{}
|
||||
urlValues.Add("_csrf", csrf)
|
||||
urlValues.Add("_csrf", GetUserCSRFToken(t, session))
|
||||
urlValues.Add("name", fmt.Sprintf("api-testing-token-%d", atomic.AddInt64(&tokenCounter, 1)))
|
||||
for _, scope := range scopes {
|
||||
urlValues.Add("scope", string(scope))
|
||||
urlValues.Add("scope-dummy", string(scope)) // it only needs to start with "scope-" to be accepted
|
||||
}
|
||||
req = NewRequestWithURLValues(t, "POST", "/user/settings/applications", urlValues)
|
||||
resp = session.MakeRequest(t, req, http.StatusSeeOther)
|
||||
|
||||
// Log the flash values on failure
|
||||
if !assert.Equal(t, []string{"/user/settings/applications"}, resp.Result().Header["Location"]) {
|
||||
for _, cookie := range resp.Result().Cookies() {
|
||||
if cookie.Name != gitea_context.CookieNameFlash {
|
||||
continue
|
||||
}
|
||||
flash, _ := url.ParseQuery(cookie.Value)
|
||||
for key, value := range flash {
|
||||
t.Logf("Flash %q: %q", key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
req = NewRequest(t, "GET", "/user/settings/applications")
|
||||
resp = session.MakeRequest(t, req, http.StatusOK)
|
||||
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||
token = htmlDoc.doc.Find(".ui.info p").Text()
|
||||
assert.NotEmpty(t, token)
|
||||
return token
|
||||
req := NewRequestWithURLValues(t, "POST", "/user/settings/applications", urlValues)
|
||||
session.MakeRequest(t, req, http.StatusSeeOther)
|
||||
flashes := session.GetCookieFlashMessage()
|
||||
return flashes.InfoMsg
|
||||
}
|
||||
|
||||
type RequestWrapper struct {
|
||||
|
Reference in New Issue
Block a user