@@ -81,7 +81,7 @@ var _ = Describe("ApiToken controller", Ordered, func() {
81
81
82
82
existingTokens .Tokens = append (existingTokens .Tokens , unleashclient.ApiToken {
83
83
Secret : ApiTokenSecret ,
84
- Username : tokenRequest .Username ,
84
+ TokenName : tokenRequest .Username ,
85
85
Type : strings .ToLower (tokenRequest .Type ),
86
86
Environment : tokenRequest .Environment ,
87
87
Project : tokenRequest .Projects [0 ],
@@ -219,7 +219,7 @@ var _ = Describe("ApiToken controller", Ordered, func() {
219
219
220
220
Expect (httpmock .GetCallCountInfo ()[fmt .Sprintf ("POST %s" , unleashclient .ApiTokensEndpoint )]).Should (Equal (1 ))
221
221
Expect (existingTokens .Tokens ).Should (HaveLen (1 ))
222
- Expect (existingTokens .Tokens [0 ].Username ).Should (Equal (apiTokenCreated .ApiTokenName ("unleasherator" )))
222
+ Expect (existingTokens .Tokens [0 ].TokenName ).Should (Equal (apiTokenCreated .ApiTokenName ("unleasherator" )))
223
223
Expect (existingTokens .Tokens [0 ].Type ).Should (Equal ("client" ))
224
224
Expect (existingTokens .Tokens [0 ].Environment ).Should (Equal ("development" ))
225
225
Expect (existingTokens .Tokens [0 ].Projects ).Should (Equal ([]string {"default" }))
@@ -309,7 +309,7 @@ var _ = Describe("ApiToken controller", Ordered, func() {
309
309
existingTokens .Tokens = []unleashclient.ApiToken {
310
310
{
311
311
Secret : ApiTokenSecret ,
312
- Username : apiTokenCreated .ApiTokenName ("unleasherator" ),
312
+ TokenName : apiTokenCreated .ApiTokenName ("unleasherator" ),
313
313
Type : "CLIENT" ,
314
314
Environment : "development" ,
315
315
Project : "default" ,
@@ -386,7 +386,7 @@ var _ = Describe("ApiToken controller", Ordered, func() {
386
386
})
387
387
388
388
Context ("When dealing with duplicate Unleash tokens" , func () {
389
- It ("Should count duplicate tokens" , func () {
389
+ It ("Should delete duplicate tokens" , func () {
390
390
ctx := context .Background ()
391
391
392
392
apiTokenName := "test-apitoken-duplicate"
@@ -401,23 +401,32 @@ var _ = Describe("ApiToken controller", Ordered, func() {
401
401
402
402
existingTokens .Tokens = []unleashclient.ApiToken {
403
403
{
404
- Secret : ApiTokenSecret ,
405
- Username : "test-apitoken-duplicate-unleasherator" ,
404
+ Secret : ApiTokenSecret + "-1" ,
405
+ TokenName : "test-apitoken-duplicate-unleasherator" ,
406
406
Type : "CLIENT" ,
407
407
Environment : "development" ,
408
408
Project : "default" ,
409
409
Projects : []string {"default" },
410
- CreatedAt : time .Date (2021 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ).Format (time .RFC3339 ),
410
+ CreatedAt : time .Date (2021 , 1 , 1 , 2 , 0 , 0 , 0 , time .UTC ).Format (time .RFC3339 ),
411
411
},
412
412
{
413
- Secret : ApiTokenSecret ,
414
- Username : "test-apitoken-duplicate-unleasherator" ,
413
+ Secret : ApiTokenSecret + "-2" ,
414
+ TokenName : "test-apitoken-duplicate-unleasherator" ,
415
415
Type : "CLIENT" ,
416
- Environment : "development " ,
416
+ Environment : "production " ,
417
417
Project : "default" ,
418
418
Projects : []string {"default" },
419
419
CreatedAt : time .Date (2021 , 1 , 1 , 1 , 0 , 0 , 0 , time .UTC ).Format (time .RFC3339 ),
420
420
},
421
+ {
422
+ Secret : ApiTokenSecret + "-3" ,
423
+ TokenName : "test-apitoken-duplicate-unleasherator" ,
424
+ Type : "CLIENT" ,
425
+ Environment : "development" ,
426
+ Project : "default" ,
427
+ Projects : []string {"default" },
428
+ CreatedAt : time .Date (2021 , 1 , 1 , 3 , 0 , 0 , 0 , time .UTC ).Format (time .RFC3339 ),
429
+ },
421
430
}
422
431
423
432
By ("By creating a new ApiToken" )
@@ -427,7 +436,20 @@ var _ = Describe("ApiToken controller", Ordered, func() {
427
436
428
437
fmt .Printf ("existingTokens=%+v\n " , existingTokens )
429
438
430
- Expect (promGaugeVecVal (apiTokenExistingTokens , ApiTokenNamespace , apiTokenName , "development" )).Should (Equal (2.0 ))
439
+ Expect (promGaugeVecVal (apiTokenExistingTokens , ApiTokenNamespace , apiTokenName , "development" )).Should (Equal (3.0 ))
440
+ Expect (promCounterVecVal (apiTokenDeletedCounter , ApiTokenNamespace , apiTokenName )).Should (Equal (2.0 ))
441
+
442
+ Expect (httpmock .GetCallCountInfo ()[fmt .Sprintf ("DELETE %s" , fmt .Sprintf ("=~%s/.*" , unleashclient .ApiTokensEndpoint ))]).Should (Equal (2 ))
443
+
444
+ // Verify that the older tokens were deleted
445
+ for _ , token := range existingTokens .Tokens {
446
+ Expect (token .Secret ).ShouldNot (Equal (ApiTokenSecret + "-1" ))
447
+ Expect (token .Secret ).ShouldNot (Equal (ApiTokenSecret + "-2" ))
448
+ }
449
+
450
+ // Verify that we kept the newest duplicate token
451
+ Expect (existingTokens .Tokens ).Should (HaveLen (1 ))
452
+ Expect (existingTokens .Tokens [0 ].Secret ).Should (Equal (ApiTokenSecret + "-3" ))
431
453
})
432
454
})
433
455
})
0 commit comments