Skip to content

Commit cadc080

Browse files
committed
fix: encapsulated homedir retrieval to allow testing without interfering with real user home
1 parent b24b9c5 commit cadc080

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

core/auth/auth.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const (
2727
privateKeyPathCredentialType credentialType = "private_key_path"
2828
)
2929

30+
var userHomeDir = os.UserHomeDir
31+
3032
// SetupAuth sets up authentication based on the configuration. The different options are
3133
// custom authentication, no authentication, explicit key flow, explicit token flow or default authentication
3234
func SetupAuth(cfg *config.Configuration) (rt http.RoundTripper, err error) {
@@ -195,7 +197,7 @@ func readCredentialsFile(path string) (*Credentials, error) {
195197
customPath, customPathSet := os.LookupEnv("STACKIT_CREDENTIALS_PATH")
196198
if !customPathSet || customPath == "" {
197199
path = credentialsFilePath
198-
home, err := os.UserHomeDir()
200+
home, err := userHomeDir()
199201
if err != nil {
200202
return nil, fmt.Errorf("getting home directory: %w", err)
201203
}

core/auth/auth_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ func TestKeyAuth(t *testing.T) {
448448
envVarPrivateKey string
449449
expectedPrivateKey string
450450
isValid bool
451+
homeDir func(*testing.T) string
451452
}{
452453
{
453454
desc: "configured_private_key",
@@ -484,6 +485,7 @@ func TestKeyAuth(t *testing.T) {
484485
serviceAccountKey: nil,
485486
configuredPrivateKey: configuredPrivateKey,
486487
isValid: false,
488+
homeDir: func(t *testing.T) string { return t.TempDir() },
487489
},
488490
{
489491
desc: "missing_private_key",
@@ -496,14 +498,23 @@ func TestKeyAuth(t *testing.T) {
496498
serviceAccountKey: nil,
497499
configuredPrivateKey: "",
498500
isValid: false,
501+
homeDir: func(t *testing.T) string { return t.TempDir() },
499502
},
500503
} {
501504
t.Run(test.desc, func(t *testing.T) {
502505
t.Setenv("STACKIT_SERVICE_ACCOUNT_KEY", "")
503506
t.Setenv("STACKIT_SERVICE_ACCOUNT_KEY_PATH", "")
504507
t.Setenv("STACKIT_PRIVATE_KEY", "")
505508
t.Setenv("STACKIT_PRIVATE_KEY_PATH", "")
506-
t.Setenv("HOME", t.TempDir())
509+
if homeDir := test.homeDir; homeDir != nil {
510+
old := userHomeDir
511+
t.Cleanup(func() {
512+
userHomeDir = old
513+
})
514+
userHomeDir = func() (string, error) {
515+
return test.homeDir(t), nil
516+
}
517+
}
507518

508519
var saKey string
509520
if test.serviceAccountKey != nil {
@@ -770,6 +781,7 @@ func TestGetServiceAccountKey(t *testing.T) {
770781
credentialsFilePath string
771782
wantErr bool
772783
expectedKey string
784+
userHomeDir func(*testing.T) string
773785
}{
774786
{
775787
name: "cfg_sa_key",
@@ -841,11 +853,20 @@ func TestGetServiceAccountKey(t *testing.T) {
841853
cfg: &config.Configuration{},
842854
wantErr: true,
843855
expectedKey: "",
856+
userHomeDir: func(t *testing.T) string { return t.TempDir() },
844857
},
845858
} {
846859
t.Run(test.name, func(t *testing.T) {
847-
t.Setenv("HOME", t.TempDir())
848860
t.Setenv("STACKIT_CREDENTIALS_PATH", test.credentialsFilePath)
861+
if homeDir := test.userHomeDir; homeDir != nil {
862+
old := userHomeDir
863+
t.Cleanup(func() {
864+
userHomeDir = old
865+
})
866+
userHomeDir = func() (string, error) {
867+
return homeDir(t), nil
868+
}
869+
}
849870

850871
if test.envServiceAccountKeyPathSet {
851872
t.Setenv("STACKIT_SERVICE_ACCOUNT_KEY_PATH", "test_resources/test_string_key.txt")

0 commit comments

Comments
 (0)