@@ -398,3 +398,194 @@ func TestBasicAuthListMethods(T *testing.T) {
398398 require .NoError (client .Consumers .Delete (defaultCtx , consumer1 .ID ))
399399 require .NoError (client .Consumers .Delete (defaultCtx , consumer2 .ID ))
400400}
401+
402+ func TestBasicAuthCreateWithOptions (T * testing.T ) {
403+ RunWhenDBMode (T , "postgres" )
404+
405+ assert := assert .New (T )
406+ require := require .New (T )
407+
408+ client , err := NewTestClient (nil , nil )
409+ require .NoError (err )
410+ assert .NotNil (client )
411+
412+ // Test with nil options
413+ basicAuth , err := client .BasicAuths .CreateWithOptions (defaultCtx ,
414+ String ("foo" ), nil )
415+ require .Error (err )
416+ assert .Nil (basicAuth )
417+ assert .Contains (err .Error (), "basic auth options and credential are required" )
418+
419+ // consumer for the basic-auth:
420+ consumer := & Consumer {
421+ Username : String ("foo" ),
422+ }
423+
424+ consumer , err = client .Consumers .Create (defaultCtx , consumer )
425+ require .NoError (err )
426+ require .NotNil (consumer )
427+
428+ // Test with empty consumer ID
429+ opts := & BasicAuthOptions {
430+ BasicAuth : BasicAuth {
431+ Username : String ("test-username" ),
432+ Password : String ("test-password" ),
433+ },
434+ }
435+ basicAuth , err = client .BasicAuths .CreateWithOptions (defaultCtx ,
436+ String ("" ), opts )
437+ require .Error (err )
438+ assert .Nil (basicAuth )
439+
440+ // Test with non-existent consumer
441+ basicAuth , err = client .BasicAuths .CreateWithOptions (defaultCtx ,
442+ String ("does-not-exist" ), opts )
443+ require .Error (err )
444+ assert .Nil (basicAuth )
445+
446+ // Test with missing username
447+ optsNoUsername := & BasicAuthOptions {
448+ BasicAuth : BasicAuth {
449+ Password : String ("test-password" ),
450+ },
451+ }
452+ basicAuth , err = client .BasicAuths .CreateWithOptions (defaultCtx ,
453+ consumer .ID , optsNoUsername )
454+ require .Error (err )
455+ assert .Nil (basicAuth )
456+
457+ // Test successful creation with SkipHash = false
458+ uuid1 := uuid .NewString ()
459+ skipHashFalse := false
460+ opts = & BasicAuthOptions {
461+ BasicAuth : BasicAuth {
462+ ID : String (uuid1 ),
463+ Username : String ("test-username" ),
464+ Password : String ("test-password" ),
465+ },
466+ SkipHash : & skipHashFalse ,
467+ }
468+ basicAuth , err = client .BasicAuths .CreateWithOptions (defaultCtx ,
469+ consumer .ID , opts )
470+ require .NoError (err )
471+ assert .NotNil (basicAuth )
472+ assert .Equal (uuid1 , * basicAuth .ID )
473+ assert .Equal ("test-username" , * basicAuth .Username )
474+ // password should be hashed when SkipHash is false
475+ assert .NotEqual ("test-password" , * basicAuth .Password )
476+
477+ // Clean up
478+ err = client .BasicAuths .Delete (defaultCtx , consumer .ID , basicAuth .Username )
479+ require .NoError (err )
480+
481+ // Test with SkipHash = true can work only with Konnect for now
482+ // TODO: enable this test for Konnect when we have a way to run tests against Konnect
483+
484+ // Test successful creation with nil SkipHash (should default to false)
485+ uuid3 := uuid .NewString ()
486+ opts = & BasicAuthOptions {
487+ BasicAuth : BasicAuth {
488+ ID : String (uuid3 ),
489+ Username : String ("test-username-3" ),
490+ Password : String ("test-password-3" ),
491+ },
492+ SkipHash : nil ,
493+ }
494+ basicAuthNilSkip , err := client .BasicAuths .CreateWithOptions (defaultCtx ,
495+ consumer .ID , opts )
496+ require .NoError (err )
497+ assert .NotNil (basicAuthNilSkip )
498+ assert .Equal (uuid3 , * basicAuthNilSkip .ID )
499+ assert .Equal ("test-username-3" , * basicAuthNilSkip .Username )
500+ // password should be hashed when SkipHash is nil (defaults to false)
501+ assert .NotEqual ("test-password-3" , * basicAuthNilSkip .Password )
502+
503+ require .NoError (client .Consumers .Delete (defaultCtx , consumer .ID ))
504+ }
505+
506+ func TestBasicAuthUpdateWithOptions (T * testing.T ) {
507+ RunWhenDBMode (T , "postgres" )
508+
509+ assert := assert .New (T )
510+ require := require .New (T )
511+
512+ client , err := NewTestClient (nil , nil )
513+ require .NoError (err )
514+ assert .NotNil (client )
515+
516+ // consumer for the basic-auth:
517+ consumer := & Consumer {
518+ Username : String ("foo" ),
519+ }
520+
521+ consumer , err = client .Consumers .Create (defaultCtx , consumer )
522+ require .NoError (err )
523+ require .NotNil (consumer )
524+
525+ // Create initial basic auth credential
526+ uuidStr := uuid .NewString ()
527+ basicAuth := & BasicAuth {
528+ ID : String (uuidStr ),
529+ Username : String ("initial-username" ),
530+ Password : String ("initial-password" ),
531+ }
532+
533+ createdBasicAuth , err := client .BasicAuths .Create (defaultCtx ,
534+ consumer .ID , basicAuth )
535+ require .NoError (err )
536+ assert .NotNil (createdBasicAuth )
537+
538+ // Test with nil options
539+ updatedBasicAuth , err := client .BasicAuths .UpdateWithOptions (defaultCtx ,
540+ consumer .ID , nil )
541+ require .Error (err )
542+ assert .Nil (updatedBasicAuth )
543+ assert .Contains (err .Error (), "basic auth options and credential are required" )
544+
545+ // Test successful update with SkipHash = false
546+ skipHashFalse := false
547+ opts := & BasicAuthOptions {
548+ BasicAuth : BasicAuth {
549+ ID : createdBasicAuth .ID ,
550+ Username : String ("updated-username" ),
551+ Password : String ("updated-password" ),
552+ },
553+ SkipHash : & skipHashFalse ,
554+ }
555+ updatedBasicAuth , err = client .BasicAuths .UpdateWithOptions (defaultCtx ,
556+ consumer .ID , opts )
557+ require .NoError (err )
558+ assert .NotNil (updatedBasicAuth )
559+ assert .Equal ("updated-username" , * updatedBasicAuth .Username )
560+ // password should be hashed when SkipHash is false
561+ assert .NotEqual ("updated-password" , * updatedBasicAuth .Password )
562+
563+ // Test successful update with SkipHash = true
564+ // Test with SkipHash = true can work only with Konnect for now
565+ // TODO: enable this test for Konnect when we have a way to run tests against Konnect
566+
567+ // Test successful update with nil SkipHash (should default to false)
568+ opts = & BasicAuthOptions {
569+ BasicAuth : BasicAuth {
570+ ID : createdBasicAuth .ID ,
571+ Username : String ("updated-username-3" ),
572+ Password : String ("updated-password-3" ),
573+ },
574+ SkipHash : nil ,
575+ }
576+ updatedBasicAuth , err = client .BasicAuths .UpdateWithOptions (defaultCtx ,
577+ consumer .ID , opts )
578+ require .NoError (err )
579+ assert .NotNil (updatedBasicAuth )
580+ assert .Equal ("updated-username-3" , * updatedBasicAuth .Username )
581+ // password should be hashed when SkipHash is nil (defaults to false)
582+ assert .NotEqual ("updated-password-3" , * updatedBasicAuth .Password )
583+
584+ // Verify the credential was actually updated
585+ retrievedBasicAuth , err := client .BasicAuths .Get (defaultCtx ,
586+ consumer .ID , updatedBasicAuth .Username )
587+ require .NoError (err )
588+ assert .Equal ("updated-username-3" , * retrievedBasicAuth .Username )
589+
590+ require .NoError (client .Consumers .Delete (defaultCtx , consumer .ID ))
591+ }
0 commit comments