Skip to content

Commit f158c41

Browse files
Copilotyurishkuro
andcommitted
Fix TestAuthenticationConditionalCreation to properly validate expected values
Restored expected value fields in the test table and proper assertions that validate the actual values against expected values for all authentication parameters (username, password, file paths, reload intervals, context flags). Co-authored-by: yurishkuro <[email protected]>
1 parent 5b9c21c commit f158c41

File tree

1 file changed

+158
-18
lines changed

1 file changed

+158
-18
lines changed

internal/storage/v1/elasticsearch/options_test.go

Lines changed: 158 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,30 @@ func TestOptionsWithFlags(t *testing.T) {
202202

203203
func TestAuthenticationConditionalCreation(t *testing.T) {
204204
testCases := []struct {
205-
name string
206-
config escfg.Configuration
205+
name string
206+
config escfg.Configuration
207+
expectBasicAuth bool
208+
expectBearerAuth bool
209+
expectAPIKeyAuth bool
210+
expectedUsername string
211+
expectedPassword string
212+
expectedPasswordFilePath string
213+
expectedPasswordReloadInterval time.Duration
214+
expectedTokenPath string
215+
expectedBearerFromContext bool
216+
expectedBearerReloadInterval time.Duration
217+
expectedAPIKeyFilePath string
218+
expectedAPIKeyFromContext bool
219+
expectedAPIKeyReloadInterval time.Duration
207220
}{
208221
{
209222
name: "no authentication flags",
210223
config: escfg.Configuration{
211224
Authentication: escfg.Authentication{},
212225
},
226+
expectBasicAuth: false,
227+
expectBearerAuth: false,
228+
expectAPIKeyAuth: false,
213229
},
214230
{
215231
name: "only username provided",
@@ -221,6 +237,11 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
221237
}),
222238
},
223239
},
240+
expectBasicAuth: true,
241+
expectBearerAuth: false,
242+
expectAPIKeyAuth: false,
243+
expectedUsername: "testuser",
244+
expectedPasswordReloadInterval: 10 * time.Second,
224245
},
225246
{
226247
name: "only password provided",
@@ -232,6 +253,11 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
232253
}),
233254
},
234255
},
256+
expectBasicAuth: true,
257+
expectBearerAuth: false,
258+
expectAPIKeyAuth: false,
259+
expectedPassword: "testpass",
260+
expectedPasswordReloadInterval: 10 * time.Second,
235261
},
236262
{
237263
name: "only token file provided",
@@ -244,6 +270,12 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
244270
}),
245271
},
246272
},
273+
expectBasicAuth: false,
274+
expectBearerAuth: true,
275+
expectAPIKeyAuth: false,
276+
expectedTokenPath: "/path/to/token",
277+
expectedBearerFromContext: false,
278+
expectedBearerReloadInterval: 10 * time.Second,
247279
},
248280
{
249281
name: "username and password provided",
@@ -256,6 +288,12 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
256288
}),
257289
},
258290
},
291+
expectBasicAuth: true,
292+
expectBearerAuth: false,
293+
expectAPIKeyAuth: false,
294+
expectedUsername: "testuser",
295+
expectedPassword: "testpass",
296+
expectedPasswordReloadInterval: 10 * time.Second,
259297
},
260298
{
261299
name: "only bearer token context propagation enabled",
@@ -267,6 +305,11 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
267305
}),
268306
},
269307
},
308+
expectBasicAuth: false,
309+
expectBearerAuth: true,
310+
expectAPIKeyAuth: false,
311+
expectedBearerFromContext: true,
312+
expectedBearerReloadInterval: 10 * time.Second,
270313
},
271314
{
272315
name: "both token file and context propagation enabled",
@@ -279,6 +322,12 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
279322
}),
280323
},
281324
},
325+
expectBasicAuth: false,
326+
expectBearerAuth: true,
327+
expectAPIKeyAuth: false,
328+
expectedTokenPath: "/path/to/token",
329+
expectedBearerFromContext: true,
330+
expectedBearerReloadInterval: 10 * time.Second,
282331
},
283332
{
284333
name: "bearer token with custom reload interval",
@@ -291,6 +340,12 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
291340
}),
292341
},
293342
},
343+
expectBasicAuth: false,
344+
expectBearerAuth: true,
345+
expectAPIKeyAuth: false,
346+
expectedTokenPath: "/path/to/token",
347+
expectedBearerFromContext: true,
348+
expectedBearerReloadInterval: 45 * time.Second,
294349
},
295350
{
296351
name: "API key all options with zero reload interval",
@@ -303,6 +358,12 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
303358
}),
304359
},
305360
},
361+
expectBasicAuth: false,
362+
expectBearerAuth: false,
363+
expectAPIKeyAuth: true,
364+
expectedAPIKeyFilePath: "/path/to/keyfile",
365+
expectedAPIKeyFromContext: true,
366+
expectedAPIKeyReloadInterval: 0 * time.Second,
306367
},
307368
{
308369
name: "API key with non-zero reload interval",
@@ -315,6 +376,12 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
315376
}),
316377
},
317378
},
379+
expectBasicAuth: false,
380+
expectBearerAuth: false,
381+
expectAPIKeyAuth: true,
382+
expectedAPIKeyFilePath: "/path/to/keyfile",
383+
expectedAPIKeyFromContext: true,
384+
expectedAPIKeyReloadInterval: 30 * time.Second,
318385
},
319386
{
320387
name: "only API key file provided",
@@ -327,6 +394,12 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
327394
}),
328395
},
329396
},
397+
expectBasicAuth: false,
398+
expectBearerAuth: false,
399+
expectAPIKeyAuth: true,
400+
expectedAPIKeyFilePath: "/path/to/key",
401+
expectedAPIKeyFromContext: false,
402+
expectedAPIKeyReloadInterval: 10 * time.Second,
330403
},
331404
{
332405
name: "only API key context propagation enabled",
@@ -338,6 +411,11 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
338411
}),
339412
},
340413
},
414+
expectBasicAuth: false,
415+
expectBearerAuth: false,
416+
expectAPIKeyAuth: true,
417+
expectedAPIKeyFromContext: true,
418+
expectedAPIKeyReloadInterval: 10 * time.Second,
341419
},
342420
{
343421
name: "both API key file and context enabled",
@@ -350,6 +428,12 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
350428
}),
351429
},
352430
},
431+
expectBasicAuth: false,
432+
expectBearerAuth: false,
433+
expectAPIKeyAuth: true,
434+
expectedAPIKeyFilePath: "/path/to/key",
435+
expectedAPIKeyFromContext: true,
436+
expectedAPIKeyReloadInterval: 10 * time.Second,
353437
},
354438
{
355439
name: "all API key options provided",
@@ -362,6 +446,12 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
362446
}),
363447
},
364448
},
449+
expectBasicAuth: false,
450+
expectBearerAuth: false,
451+
expectAPIKeyAuth: true,
452+
expectedAPIKeyFilePath: "/path/to/key",
453+
expectedAPIKeyFromContext: true,
454+
expectedAPIKeyReloadInterval: 60 * time.Second,
365455
},
366456
{
367457
name: "basic auth and API key both enabled",
@@ -378,6 +468,14 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
378468
}),
379469
},
380470
},
471+
expectBasicAuth: true,
472+
expectBearerAuth: false,
473+
expectAPIKeyAuth: true,
474+
expectedUsername: "testuser",
475+
expectedPassword: "testpass",
476+
expectedPasswordReloadInterval: 10 * time.Second,
477+
expectedAPIKeyFilePath: "/path/to/key",
478+
expectedAPIKeyReloadInterval: 10 * time.Second,
381479
},
382480
{
383481
name: "bearer token and API key both enabled",
@@ -394,6 +492,14 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
394492
}),
395493
},
396494
},
495+
expectBasicAuth: false,
496+
expectBearerAuth: true,
497+
expectAPIKeyAuth: true,
498+
expectedTokenPath: "/path/to/token",
499+
expectedBearerFromContext: false,
500+
expectedBearerReloadInterval: 10 * time.Second,
501+
expectedAPIKeyFromContext: true,
502+
expectedAPIKeyReloadInterval: 10 * time.Second,
397503
},
398504
{
399505
name: "basic auth password reload interval disabled",
@@ -406,6 +512,12 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
406512
}),
407513
},
408514
},
515+
expectBasicAuth: true,
516+
expectBearerAuth: false,
517+
expectAPIKeyAuth: false,
518+
expectedUsername: "testuser",
519+
expectedPasswordFilePath: "/path/to/password",
520+
expectedPasswordReloadInterval: 0 * time.Second,
409521
},
410522
{
411523
name: "bearer token reload interval disabled",
@@ -417,6 +529,11 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
417529
}),
418530
},
419531
},
532+
expectBasicAuth: false,
533+
expectBearerAuth: true,
534+
expectAPIKeyAuth: false,
535+
expectedTokenPath: "/path/to/token",
536+
expectedBearerReloadInterval: 0 * time.Second,
420537
},
421538
{
422539
name: "all three authentication methods enabled",
@@ -439,6 +556,18 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
439556
}),
440557
},
441558
},
559+
expectBasicAuth: true,
560+
expectBearerAuth: true,
561+
expectAPIKeyAuth: true,
562+
expectedUsername: "testuser",
563+
expectedPassword: "testpass",
564+
expectedPasswordReloadInterval: 10 * time.Second,
565+
expectedTokenPath: "/path/to/token",
566+
expectedBearerFromContext: true,
567+
expectedBearerReloadInterval: 25 * time.Second,
568+
expectedAPIKeyFilePath: "/path/to/key",
569+
expectedAPIKeyFromContext: true,
570+
expectedAPIKeyReloadInterval: 30 * time.Second,
442571
},
443572
{
444573
name: "basic auth with custom reload interval (non-zero)",
@@ -451,6 +580,12 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
451580
}),
452581
},
453582
},
583+
expectBasicAuth: true,
584+
expectBearerAuth: false,
585+
expectAPIKeyAuth: false,
586+
expectedUsername: "testuser",
587+
expectedPasswordFilePath: "/path/to/password",
588+
expectedPasswordReloadInterval: 15 * time.Second,
454589
},
455590
{
456591
name: "bearer token with custom reload interval (non-zero)",
@@ -462,6 +597,11 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
462597
}),
463598
},
464599
},
600+
expectBasicAuth: false,
601+
expectBearerAuth: true,
602+
expectAPIKeyAuth: false,
603+
expectedTokenPath: "/path/to/token",
604+
expectedBearerReloadInterval: 20 * time.Second,
465605
},
466606
}
467607

@@ -470,33 +610,33 @@ func TestAuthenticationConditionalCreation(t *testing.T) {
470610
primary := tc.config
471611

472612
// Assert authentication method presence
473-
expectBasicAuth := primary.Authentication.BasicAuthentication.HasValue()
474-
expectBearerAuth := primary.Authentication.BearerTokenAuth.HasValue()
475-
expectAPIKeyAuth := primary.Authentication.APIKeyAuth.HasValue()
476-
477-
assert.Equal(t, expectBasicAuth, primary.Authentication.BasicAuthentication.HasValue())
478-
assert.Equal(t, expectBearerAuth, primary.Authentication.BearerTokenAuth.HasValue())
479-
assert.Equal(t, expectAPIKeyAuth, primary.Authentication.APIKeyAuth.HasValue())
613+
assert.Equal(t, tc.expectBasicAuth, primary.Authentication.BasicAuthentication.HasValue())
614+
assert.Equal(t, tc.expectBearerAuth, primary.Authentication.BearerTokenAuth.HasValue())
615+
assert.Equal(t, tc.expectAPIKeyAuth, primary.Authentication.APIKeyAuth.HasValue())
480616

481617
// Assert basic authentication details
482-
if expectBasicAuth {
618+
if tc.expectBasicAuth {
483619
basicAuth := primary.Authentication.BasicAuthentication.Get()
484-
hasAtLeastOneField := basicAuth.Username != "" || basicAuth.Password != "" || basicAuth.PasswordFilePath != ""
485-
assert.True(t, hasAtLeastOneField, "at least one basic auth field should be set")
620+
assert.Equal(t, tc.expectedUsername, basicAuth.Username)
621+
assert.Equal(t, tc.expectedPassword, basicAuth.Password)
622+
assert.Equal(t, tc.expectedPasswordFilePath, basicAuth.PasswordFilePath)
623+
assert.Equal(t, tc.expectedPasswordReloadInterval, basicAuth.ReloadInterval)
486624
}
487625

488626
// Assert bearer token authentication details
489-
if expectBearerAuth {
627+
if tc.expectBearerAuth {
490628
bearerAuth := primary.Authentication.BearerTokenAuth.Get()
491-
hasAtLeastOneField := bearerAuth.FilePath != "" || bearerAuth.AllowFromContext
492-
assert.True(t, hasAtLeastOneField, "at least one bearer auth field should be set")
629+
assert.Equal(t, tc.expectedTokenPath, bearerAuth.FilePath)
630+
assert.Equal(t, tc.expectedBearerFromContext, bearerAuth.AllowFromContext)
631+
assert.Equal(t, tc.expectedBearerReloadInterval, bearerAuth.ReloadInterval)
493632
}
494633

495634
// Assert API key authentication details
496-
if expectAPIKeyAuth {
635+
if tc.expectAPIKeyAuth {
497636
apiKeyAuth := primary.Authentication.APIKeyAuth.Get()
498-
hasAtLeastOneField := apiKeyAuth.FilePath != "" || apiKeyAuth.AllowFromContext
499-
assert.True(t, hasAtLeastOneField, "at least one API key auth field should be set")
637+
assert.Equal(t, tc.expectedAPIKeyFilePath, apiKeyAuth.FilePath)
638+
assert.Equal(t, tc.expectedAPIKeyFromContext, apiKeyAuth.AllowFromContext)
639+
assert.Equal(t, tc.expectedAPIKeyReloadInterval, apiKeyAuth.ReloadInterval)
500640
}
501641
})
502642
}

0 commit comments

Comments
 (0)