@@ -24,13 +24,13 @@ import (
2424 "context"
2525 "errors"
2626 "fmt"
27- "math/rand"
2827 "testing"
2928 "time"
3029
3130 "github.com/arangodb/go-driver/v2/arangodb"
3231 "github.com/arangodb/go-driver/v2/arangodb/shared"
3332 "github.com/arangodb/go-driver/v2/utils"
33+ "github.com/google/uuid"
3434 "github.com/stretchr/testify/require"
3535)
3636
@@ -44,23 +44,41 @@ func Test_AccessTokens(t *testing.T) {
4444 user := "root"
4545
4646 t .Run ("Create Access Token With All valid data" , func (t * testing.T ) {
47- tokenName := fmt .Sprintf ("Token-%d-%d" , time .Now ().UnixNano (), rand .Int ())
48- t .Logf ("Create Access Token With All valid data - Creating token with name: %s\n " , tokenName )
49- req := arangodb.AccessTokenRequest {
50- Name : utils .NewType (tokenName ),
51- ValidUntil : utils .NewType (expiresAt ),
52- }
5347 var err error
54- resp , err := client .CreateAccessToken (ctx , & user , req )
48+ maxRetries := 3
49+
50+ for i := 0 ; i < maxRetries ; i ++ {
51+ tokenName := fmt .Sprintf ("Token-%s" , uuid .New ().String ())
52+ cleanupToken (ctx , t , client , user , tokenName )
53+
54+ req := arangodb.AccessTokenRequest {
55+ Name : utils .NewType (tokenName ),
56+ ValidUntil : utils .NewType (expiresAt ),
57+ }
58+
59+ resp , err := client .CreateAccessToken (ctx , & user , req )
60+ if err == nil {
61+ tokenResp = & resp
62+ require .NotNil (t , tokenResp )
63+ require .NotNil (t , tokenResp .Id )
64+ require .NotNil (t , tokenResp .Token )
65+ require .NotNil (t , tokenResp .Fingerprint )
66+ require .Equal (t , tokenName , * tokenResp .Name )
67+ require .Equal (t , true , * tokenResp .Active )
68+ require .Equal (t , expiresAt , * tokenResp .ValidUntil )
69+ break // success
70+ }
71+
72+ // if conflict, retry; else fail immediately
73+ var arangoErr shared.ArangoError
74+ if errors .As (err , & arangoErr ) && arangoErr .Code == 409 {
75+ t .Logf ("Conflict detected, retrying token creation... attempt %d\n " , i + 1 )
76+ continue
77+ } else {
78+ break
79+ }
80+ }
5581 require .NoError (t , err )
56- require .NotNil (t , resp )
57- tokenResp = & resp
58- require .NotNil (t , tokenResp .Id )
59- require .NotNil (t , tokenResp .Token )
60- require .NotNil (t , tokenResp .Fingerprint )
61- require .Equal (t , tokenName , * tokenResp .Name )
62- require .Equal (t , true , * tokenResp .Active )
63- require .Equal (t , expiresAt , * tokenResp .ValidUntil )
6482 })
6583
6684 t .Run ("Get All Access Tokens" , func (t * testing.T ) {
@@ -86,7 +104,7 @@ func Test_AccessTokens(t *testing.T) {
86104 })
87105
88106 t .Run ("Client try to create duplicate access token name" , func (t * testing.T ) {
89- if tokenResp .Name == nil {
107+ if tokenResp == nil || tokenResp .Name == nil {
90108 t .Skip ("Skipping delete test because token creation failed" )
91109 }
92110 t .Logf ("Client try to create duplicate access token name - token name: %s\n " , * tokenResp .Name )
@@ -106,7 +124,7 @@ func Test_AccessTokens(t *testing.T) {
106124 })
107125
108126 t .Run ("Delete Access Token" , func (t * testing.T ) {
109- if tokenResp .Id == nil {
127+ if tokenResp == nil || tokenResp .Id == nil {
110128 t .Skip ("Skipping delete test because token creation failed" )
111129 }
112130 err := client .DeleteAccessToken (ctx , & user , tokenResp .Id )
@@ -118,7 +136,7 @@ func Test_AccessTokens(t *testing.T) {
118136
119137 t .Run ("Create Access Token With invalid user" , func (t * testing.T ) {
120138 invalidUser := "roothyd"
121- tokenName := fmt .Sprintf ("Token-%d-%d " , time . Now ().UnixNano (), rand . Int ())
139+ tokenName := fmt .Sprintf ("Token-%s " , uuid . New ().String ())
122140 t .Logf ("Create Access Token With invalid user - Creating token with name: %s\n " , tokenName )
123141 req := arangodb.AccessTokenRequest {
124142 Name : utils .NewType (tokenName ),
@@ -137,7 +155,7 @@ func Test_AccessTokens(t *testing.T) {
137155 })
138156
139157 t .Run ("Create Access Token With missing user" , func (t * testing.T ) {
140- tokenName := fmt .Sprintf ("Token-%d-%d " , time . Now ().UnixNano (), rand . Int ())
158+ tokenName := fmt .Sprintf ("Token-%s " , uuid . New ().String ())
141159 t .Logf ("Create Access Token With missing user - Creating token with name: %s\n " , tokenName )
142160 localExpiresAt := time .Now ().Add (5 * time .Minute ).Unix ()
143161 req := arangodb.AccessTokenRequest {
@@ -174,3 +192,23 @@ func Test_AccessTokens(t *testing.T) {
174192 })
175193 })
176194}
195+
196+ // Cleanup tokens with the same name
197+ func cleanupToken (ctx context.Context , t * testing.T , client arangodb.Client , user string , tokenName string ) {
198+ tokens , err := client .GetAllAccessToken (ctx , & user )
199+ if err != nil {
200+ t .Logf ("Failed to list tokens for cleanup: %v" , err )
201+ return
202+ }
203+
204+ for _ , token := range tokens .Tokens {
205+ if token .Name != nil && * token .Name == tokenName {
206+ if token .Id != nil {
207+ err := client .DeleteAccessToken (ctx , & user , token .Id )
208+ if err != nil {
209+ t .Logf ("Failed to delete token %s: %v" , * token .Name , err )
210+ }
211+ }
212+ }
213+ }
214+ }
0 commit comments