Skip to content

Commit 6296633

Browse files
committed
review feedback
1 parent e061960 commit 6296633

4 files changed

Lines changed: 47 additions & 9 deletions

File tree

‎internal/pkg/auth/service_account.go‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,25 @@ func AuthenticateServiceAccount(p *print.Printer, rt http.RoundTripper, disableW
108108

109109
if !disableWriting {
110110
// Clean up user credentials to avoid stale fields
111-
_ = DeleteAuthField(USER_EMAIL)
112-
_ = DeleteAuthField(REFRESH_TOKEN)
113-
_ = DeleteAuthField(SERVICE_ACCOUNT_TOKEN)
114-
_ = DeleteAuthField(SERVICE_ACCOUNT_KEY)
115-
_ = DeleteAuthField(PRIVATE_KEY)
111+
if err = DeleteAuthField(USER_EMAIL); err != nil {
112+
p.Debug(print.ErrorLevel, "failed to delete auth field %s: %v", USER_EMAIL, err)
113+
}
114+
115+
if err = DeleteAuthField(REFRESH_TOKEN); err != nil {
116+
p.Debug(print.ErrorLevel, "failed to delete auth field %s: &v", USER_EMAIL, err)
117+
}
118+
119+
if err = DeleteAuthField(SERVICE_ACCOUNT_TOKEN); err != nil {
120+
p.Debug(print.ErrorLevel, "failed to delete auth field %s: &v", REFRESH_TOKEN, err)
121+
}
122+
123+
if err = DeleteAuthField(SERVICE_ACCOUNT_KEY); err != nil {
124+
p.Debug(print.ErrorLevel, "failed to delete auth field %s: &v", SERVICE_ACCOUNT_TOKEN, err)
125+
}
126+
127+
if err = DeleteAuthField(PRIVATE_KEY); err != nil {
128+
p.Debug(print.ErrorLevel, "failed to delete auth field %s: &v", SERVICE_ACCOUNT_KEY, err)
129+
}
116130

117131
err = SetAuthFlow(authFlowType)
118132
if err != nil {

‎internal/pkg/auth/service_account_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package auth
33
import (
44
"fmt"
55
"net/http"
6+
"os"
67
"testing"
78

89
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
@@ -28,6 +29,21 @@ type keyFlowMocked struct {
2829
tokenInvalid bool
2930
}
3031

32+
// TestMain will be executed before all tests in the "auth" package.
33+
// It's used to configure a temp directory for the CLI configuration files.
34+
func TestMain(m *testing.M) {
35+
tempDir, err := os.MkdirTemp("", "stackit-auth-test-*")
36+
if err != nil {
37+
fmt.Printf("could not create temp dir: %v", err)
38+
os.Exit(1)
39+
}
40+
defer os.RemoveAll(tempDir)
41+
42+
config.SetDefaultConfigFolderPath(tempDir)
43+
44+
m.Run()
45+
}
46+
3147
func (f *keyFlowMocked) GetAccessToken() (string, error) {
3248
if f.getAccessTokenFail {
3349
return "", fmt.Errorf("error")

‎internal/pkg/auth/storage_test.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func TestSetGetAuthFieldWithProfile(t *testing.T) {
271271

272272
for _, tt := range tests {
273273
t.Run(tt.description, func(t *testing.T) {
274-
// Apppend random string to profile name to avoid conflicts
274+
// Append random string to profile name to avoid conflicts
275275
tt.activeProfile = makeProfileNameUnique(tt.activeProfile)
276276

277277
// Make sure profile name is valid
@@ -1288,7 +1288,6 @@ func TestFileNotCreatedOnReadOrDelete(t *testing.T) {
12881288
}
12891289

12901290
textFilePath := filepath.Join(config.GetProfileFolderPath(profile), textFileName)
1291-
_ = os.Remove(textFilePath)
12921291

12931292
// Ensure file doesn't exist
12941293
if _, err := os.Stat(textFilePath); !os.IsNotExist(err) {
@@ -1318,13 +1317,13 @@ func TestFileNotCreatedOnReadOrDelete(t *testing.T) {
13181317
}
13191318

13201319
func TestFileRemovedWhenEmpty(t *testing.T) {
1320+
13211321
profile, err := config.GetProfile()
13221322
if err != nil {
13231323
t.Fatalf("get profile: %v", err)
13241324
}
13251325

13261326
textFilePath := filepath.Join(config.GetProfileFolderPath(profile), textFileName)
1327-
_ = os.Remove(textFilePath)
13281327

13291328
field1 := authFieldKey("test-cleanup-field-1")
13301329
field2 := authFieldKey("test-cleanup-field-2")

‎internal/pkg/config/config.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ var configFolderPath string
141141
var profileFilePath string
142142

143143
func InitConfig() {
144-
initConfig(getInitialConfigDir())
144+
dir := defaultConfigFolderPath
145+
if dir == "" {
146+
dir = getInitialConfigDir()
147+
}
148+
initConfig(dir)
145149
}
146150

147151
func initConfig(configPath string) {
@@ -254,3 +258,8 @@ func getInitialProfileFilePath() string {
254258

255259
return filepath.Join(configFolderPath, fmt.Sprintf("%s.%s", profileFileName, profileFileExtension))
256260
}
261+
262+
// SetDefaultConfigFolderPath overrides the default config folder path. Intended for testing.
263+
func SetDefaultConfigFolderPath(path string) {
264+
defaultConfigFolderPath = path
265+
}

0 commit comments

Comments
 (0)