@@ -170,6 +170,8 @@ func TestSetupAuth(t *testing.T) {
170170 t .Setenv ("STACKIT_CREDENTIALS_PATH" , "" )
171171 }
172172
173+ t .Setenv ("STACKIT_SERVICE_ACCOUNT_EMAIL" , "test-email" )
174+
173175 authRoundTripper , err := SetupAuth (test .config )
174176
175177 if err != nil && test .isValid {
@@ -568,6 +570,78 @@ func TestNoAuth(t *testing.T) {
568570 }
569571}
570572
573+ func TestGetServiceAccountEmail (t * testing.T ) {
574+ for _ , test := range []struct {
575+ description string
576+ cfg * config.Configuration
577+ envEmailSet bool
578+ path string
579+ expectedEmail string
580+ isValid bool
581+ }{
582+ {
583+ description : "custom_config" ,
584+ cfg : & config.Configuration {
585+ ServiceAccountEmail : "test_email" ,
586+ },
587+ path : "" ,
588+ expectedEmail : "test_email" ,
589+ isValid : true ,
590+ },
591+ {
592+ description : "config_over_env_var" ,
593+ cfg : & config.Configuration {},
594+ envEmailSet : true ,
595+ path : "" ,
596+ expectedEmail : "env_email" ,
597+ isValid : true ,
598+ },
599+ {
600+ description : "env_variable" ,
601+ cfg : & config.Configuration {
602+ ServiceAccountEmail : "test_email" ,
603+ },
604+ envEmailSet : true ,
605+ path : "" ,
606+ expectedEmail : "test_email" ,
607+ isValid : true ,
608+ },
609+ {
610+ description : "path" ,
611+ cfg : & config.Configuration {},
612+ envEmailSet : false ,
613+ path : "test_resources/test_credentials_bar.json" ,
614+ expectedEmail : "bar_email" ,
615+ isValid : true ,
616+ },
617+ {
618+ description : "invalid_structure" ,
619+ cfg : & config.Configuration {},
620+ envEmailSet : false ,
621+ path : "test_resources/test_invalid_structure.json" ,
622+ expectedEmail : "" ,
623+ isValid : false ,
624+ },
625+ } {
626+ t .Run (test .description , func (t * testing.T ) {
627+ if test .envEmailSet {
628+ t .Setenv ("STACKIT_SERVICE_ACCOUNT_EMAIL" , "env_email" )
629+ } else {
630+ t .Setenv ("STACKIT_SERVICE_ACCOUNT_EMAIL" , "" )
631+ }
632+ t .Setenv ("STACKIT_CREDENTIALS_PATH" , test .path )
633+ got := getServiceAccountEmail (test .cfg )
634+ if (got != "" ) && ! test .isValid {
635+ t .Errorf ("getServiceAccountEmail() did not return empty value for invalid test case" )
636+ return
637+ }
638+ if got != test .expectedEmail {
639+ t .Errorf ("getServiceAccountEmail() = %v, want %v" , got , test .expectedEmail )
640+ }
641+ })
642+ }
643+ }
644+
571645func generatePrivateKey () (string , error ) {
572646 // Generate a new RSA key pair with a size of 2048 bits
573647 privKey , err := rsa .GenerateKey (rand .Reader , 2048 )
0 commit comments