@@ -23,10 +23,10 @@ func TestAPICreateAndDeleteToken(t *testing.T) {
23
23
defer tests .PrepareTestEnv (t )()
24
24
user := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 1 })
25
25
26
- newAccessToken := createAPIAccessTokenWithoutCleanUp (t , "test-key-1" , user , nil )
26
+ newAccessToken := createAPIAccessTokenWithoutCleanUp (t , "test-key-1" , user , []auth_model. AccessTokenScope { auth_model . AccessTokenScopeAll } )
27
27
deleteAPIAccessToken (t , newAccessToken , user )
28
28
29
- newAccessToken = createAPIAccessTokenWithoutCleanUp (t , "test-key-2" , user , nil )
29
+ newAccessToken = createAPIAccessTokenWithoutCleanUp (t , "test-key-2" , user , []auth_model. AccessTokenScope { auth_model . AccessTokenScopeAll } )
30
30
deleteAPIAccessToken (t , newAccessToken , user )
31
31
}
32
32
@@ -72,19 +72,19 @@ func TestAPIDeleteTokensPermission(t *testing.T) {
72
72
user4 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 4 })
73
73
74
74
// admin can delete tokens for other users
75
- createAPIAccessTokenWithoutCleanUp (t , "test-key-1" , user2 , nil )
75
+ createAPIAccessTokenWithoutCleanUp (t , "test-key-1" , user2 , []auth_model. AccessTokenScope { auth_model . AccessTokenScopeAll } )
76
76
req := NewRequest (t , "DELETE" , "/api/v1/users/" + user2 .LoginName + "/tokens/test-key-1" ).
77
77
AddBasicAuth (admin .Name )
78
78
MakeRequest (t , req , http .StatusNoContent )
79
79
80
80
// non-admin can delete tokens for himself
81
- createAPIAccessTokenWithoutCleanUp (t , "test-key-2" , user2 , nil )
81
+ createAPIAccessTokenWithoutCleanUp (t , "test-key-2" , user2 , []auth_model. AccessTokenScope { auth_model . AccessTokenScopeAll } )
82
82
req = NewRequest (t , "DELETE" , "/api/v1/users/" + user2 .LoginName + "/tokens/test-key-2" ).
83
83
AddBasicAuth (user2 .Name )
84
84
MakeRequest (t , req , http .StatusNoContent )
85
85
86
86
// non-admin can't delete tokens for other users
87
- createAPIAccessTokenWithoutCleanUp (t , "test-key-3" , user2 , nil )
87
+ createAPIAccessTokenWithoutCleanUp (t , "test-key-3" , user2 , []auth_model. AccessTokenScope { auth_model . AccessTokenScopeAll } )
88
88
req = NewRequest (t , "DELETE" , "/api/v1/users/" + user2 .LoginName + "/tokens/test-key-3" ).
89
89
AddBasicAuth (user4 .Name )
90
90
MakeRequest (t , req , http .StatusForbidden )
@@ -520,7 +520,7 @@ func runTestCase(t *testing.T, testCase *requiredScopeTestCase, user *user_model
520
520
unauthorizedScopes = append (unauthorizedScopes , cateogoryUnauthorizedScopes ... )
521
521
}
522
522
523
- accessToken := createAPIAccessTokenWithoutCleanUp (t , "test-token" , user , & unauthorizedScopes )
523
+ accessToken := createAPIAccessTokenWithoutCleanUp (t , "test-token" , user , unauthorizedScopes )
524
524
defer deleteAPIAccessToken (t , accessToken , user )
525
525
526
526
// Request the endpoint. Verify that permission is denied.
@@ -532,20 +532,12 @@ func runTestCase(t *testing.T, testCase *requiredScopeTestCase, user *user_model
532
532
533
533
// createAPIAccessTokenWithoutCleanUp Create an API access token and assert that
534
534
// creation succeeded. The caller is responsible for deleting the token.
535
- func createAPIAccessTokenWithoutCleanUp (t * testing.T , tokenName string , user * user_model.User , scopes * []auth_model.AccessTokenScope ) api.AccessToken {
535
+ func createAPIAccessTokenWithoutCleanUp (t * testing.T , tokenName string , user * user_model.User , scopes []auth_model.AccessTokenScope ) api.AccessToken {
536
536
payload := map [string ]any {
537
- "name" : tokenName ,
538
- }
539
- if scopes != nil {
540
- for _ , scope := range * scopes {
541
- scopes , scopesExists := payload ["scopes" ].([]string )
542
- if ! scopesExists {
543
- scopes = make ([]string , 0 )
544
- }
545
- scopes = append (scopes , string (scope ))
546
- payload ["scopes" ] = scopes
547
- }
537
+ "name" : tokenName ,
538
+ "scopes" : scopes ,
548
539
}
540
+
549
541
log .Debug ("Requesting creation of token with scopes: %v" , scopes )
550
542
req := NewRequestWithJSON (t , "POST" , "/api/v1/users/" + user .LoginName + "/tokens" , payload ).
551
543
AddBasicAuth (user .Name )
@@ -563,8 +555,7 @@ func createAPIAccessTokenWithoutCleanUp(t *testing.T, tokenName string, user *us
563
555
return newAccessToken
564
556
}
565
557
566
- // createAPIAccessTokenWithoutCleanUp Delete an API access token and assert that
567
- // deletion succeeded.
558
+ // deleteAPIAccessToken deletes an API access token and assert that deletion succeeded.
568
559
func deleteAPIAccessToken (t * testing.T , accessToken api.AccessToken , user * user_model.User ) {
569
560
req := NewRequestf (t , "DELETE" , "/api/v1/users/" + user .LoginName + "/tokens/%d" , accessToken .ID ).
570
561
AddBasicAuth (user .Name )
0 commit comments