@@ -249,55 +249,19 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
249249// token has to be unique this counter take care of
250250var tokenCounter int64
251251
252- // getTokenForLoggedInUser returns a token for a logged in user.
253- // The scope is an optional list of snake_case strings like the frontend form fields,
254- // but without the "scope_" prefix.
252+ // getTokenForLoggedInUser returns a token for a logged-in user.
255253func getTokenForLoggedInUser (t testing.TB , session * TestSession , scopes ... auth.AccessTokenScope ) string {
256254 t .Helper ()
257- var token string
258- req := NewRequest (t , "GET" , "/user/settings/applications" )
259- resp := session .MakeRequest (t , req , http .StatusOK )
260- var csrf string
261- for _ , cookie := range resp .Result ().Cookies () {
262- if cookie .Name != "_csrf" {
263- continue
264- }
265- csrf = cookie .Value
266- break
267- }
268- if csrf == "" {
269- doc := NewHTMLParser (t , resp .Body )
270- csrf = doc .GetCSRF ()
271- }
272- assert .NotEmpty (t , csrf )
273255 urlValues := url.Values {}
274- urlValues .Add ("_csrf" , csrf )
256+ urlValues .Add ("_csrf" , GetUserCSRFToken ( t , session ) )
275257 urlValues .Add ("name" , fmt .Sprintf ("api-testing-token-%d" , atomic .AddInt64 (& tokenCounter , 1 )))
276258 for _ , scope := range scopes {
277- urlValues .Add ("scope" , string (scope ))
259+ urlValues .Add ("scope-dummy " , string (scope )) // it only needs to start with "scope-" to be accepted
278260 }
279- req = NewRequestWithURLValues (t , "POST" , "/user/settings/applications" , urlValues )
280- resp = session .MakeRequest (t , req , http .StatusSeeOther )
281-
282- // Log the flash values on failure
283- if ! assert .Equal (t , []string {"/user/settings/applications" }, resp .Result ().Header ["Location" ]) {
284- for _ , cookie := range resp .Result ().Cookies () {
285- if cookie .Name != gitea_context .CookieNameFlash {
286- continue
287- }
288- flash , _ := url .ParseQuery (cookie .Value )
289- for key , value := range flash {
290- t .Logf ("Flash %q: %q" , key , value )
291- }
292- }
293- }
294-
295- req = NewRequest (t , "GET" , "/user/settings/applications" )
296- resp = session .MakeRequest (t , req , http .StatusOK )
297- htmlDoc := NewHTMLParser (t , resp .Body )
298- token = htmlDoc .doc .Find (".ui.info p" ).Text ()
299- assert .NotEmpty (t , token )
300- return token
261+ req := NewRequestWithURLValues (t , "POST" , "/user/settings/applications" , urlValues )
262+ session .MakeRequest (t , req , http .StatusSeeOther )
263+ flashes := session .GetCookieFlashMessage ()
264+ return flashes .InfoMsg
301265}
302266
303267type RequestWrapper struct {
0 commit comments