From 33d09d0e36ed8c408926e66aaf49af27bb624002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Tue, 21 May 2024 08:31:49 +0200 Subject: [PATCH 1/4] move reading profile to caller functions --- internal/pkg/auth/storage.go | 57 +++++++++++++----------------------- 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/internal/pkg/auth/storage.go b/internal/pkg/auth/storage.go index de08f25d5..87ecd9747 100644 --- a/internal/pkg/auth/storage.go +++ b/internal/pkg/auth/storage.go @@ -60,9 +60,14 @@ func SetAuthFieldMap(keyMap map[authFieldKey]string) error { } func SetAuthField(key authFieldKey, value string) error { - err := setAuthFieldInKeyring(key, value) + activeProfile, err := config.GetProfile() + if err != nil { + return fmt.Errorf("get profile: %w", err) + } + + err = setAuthFieldInKeyring(activeProfile, key, value) if err != nil { - errFallback := setAuthFieldInEncodedTextFile(key, value) + errFallback := setAuthFieldInEncodedTextFile(activeProfile, key, value) if errFallback != nil { return fmt.Errorf("write to keyring failed (%w), try writing to encoded text file: %w", err, errFallback) } @@ -70,12 +75,7 @@ func SetAuthField(key authFieldKey, value string) error { return nil } -func setAuthFieldInKeyring(key authFieldKey, value string) error { - activeProfile, err := config.GetProfile() - if err != nil { - return fmt.Errorf("get profile: %w", err) - } - +func setAuthFieldInKeyring(activeProfile string, key authFieldKey, value string) error { if activeProfile != "" { activeProfileKeyring := filepath.Join(keyringService, activeProfile) return keyring.Set(activeProfileKeyring, string(key), value) @@ -83,8 +83,8 @@ func setAuthFieldInKeyring(key authFieldKey, value string) error { return keyring.Set(keyringService, string(key), value) } -func setAuthFieldInEncodedTextFile(key authFieldKey, value string) error { - err := createEncodedTextFile() +func setAuthFieldInEncodedTextFile(activeProfile string, key authFieldKey, value string) error { + err := createEncodedTextFile(activeProfile) if err != nil { return err } @@ -94,11 +94,6 @@ func setAuthFieldInEncodedTextFile(key authFieldKey, value string) error { return fmt.Errorf("get config dir: %w", err) } - activeProfile, err := config.GetProfile() - if err != nil { - return fmt.Errorf("get profile: %w", err) - } - profileTextFileFolderName := textFileFolderName if activeProfile != "" { profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName) @@ -153,10 +148,15 @@ func GetAuthFlow() (AuthFlow, error) { } func GetAuthField(key authFieldKey) (string, error) { - value, err := getAuthFieldFromKeyring(key) + activeProfile, err := config.GetProfile() + if err != nil { + return "", fmt.Errorf("get profile: %w", err) + } + + value, err := getAuthFieldFromKeyring(activeProfile, key) if err != nil { var errFallback error - value, errFallback = getAuthFieldFromEncodedTextFile(key) + value, errFallback = getAuthFieldFromEncodedTextFile(activeProfile, key) if errFallback != nil { return "", fmt.Errorf("read from keyring: %w, read from encoded file as fallback: %w", err, errFallback) } @@ -164,12 +164,7 @@ func GetAuthField(key authFieldKey) (string, error) { return value, nil } -func getAuthFieldFromKeyring(key authFieldKey) (string, error) { - activeProfile, err := config.GetProfile() - if err != nil { - return "", fmt.Errorf("get profile: %w", err) - } - +func getAuthFieldFromKeyring(activeProfile string, key authFieldKey) (string, error) { if activeProfile != "" { activeProfileKeyring := filepath.Join(keyringService, activeProfile) return keyring.Get(activeProfileKeyring, string(key)) @@ -177,8 +172,8 @@ func getAuthFieldFromKeyring(key authFieldKey) (string, error) { return keyring.Get(keyringService, string(key)) } -func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) { - err := createEncodedTextFile() +func getAuthFieldFromEncodedTextFile(activeProfile string, key authFieldKey) (string, error) { + err := createEncodedTextFile(activeProfile) if err != nil { return "", err } @@ -188,11 +183,6 @@ func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) { return "", fmt.Errorf("get config dir: %w", err) } - activeProfile, err := config.GetProfile() - if err != nil { - return "", fmt.Errorf("get profile: %w", err) - } - profileTextFileFolderName := textFileFolderName if activeProfile != "" { profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName) @@ -224,17 +214,12 @@ func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) { // Checks if the encoded text file exist. // If it doesn't, creates it with the content "{}" encoded. // If it does, does nothing (and returns nil). -func createEncodedTextFile() error { +func createEncodedTextFile(activeProfile string) error { configDir, err := os.UserConfigDir() if err != nil { return fmt.Errorf("get config dir: %w", err) } - activeProfile, err := config.GetProfile() - if err != nil { - return fmt.Errorf("get profile: %w", err) - } - profileTextFileFolderName := textFileFolderName if activeProfile != "" { profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName) From f8850080ad0225049ba502646d25c542ace705c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Tue, 21 May 2024 08:32:06 +0200 Subject: [PATCH 2/4] adapt unit tests --- internal/pkg/auth/storage_test.go | 36 ++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/internal/pkg/auth/storage_test.go b/internal/pkg/auth/storage_test.go index 306b4deee..df9c47b32 100644 --- a/internal/pkg/auth/storage_test.go +++ b/internal/pkg/auth/storage_test.go @@ -115,6 +115,11 @@ func TestSetGetAuthField(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { + activeProfile, err := config.GetProfile() + if err != nil { + t.Errorf("get profile: %v", err) + } + if !tt.keyringFails { keyring.MockInit() } else { @@ -147,7 +152,7 @@ func TestSetGetAuthField(t *testing.T) { t.Errorf("Post-test cleanup failed: remove field \"%s\" from keyring: %v. Please remove it manually", key, err) } } else { - err = deleteAuthFieldInEncodedTextFile(key) + err = deleteAuthFieldInEncodedTextFile(activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from text file: %v. Please remove it manually", key, err) } @@ -219,8 +224,13 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { t.Run(tt.description, func(t *testing.T) { keyring.MockInit() + activeProfile, err := config.GetProfile() + if err != nil { + t.Errorf("get profile: %v", err) + } + for _, assignment := range tt.valueAssignments { - err := setAuthFieldInKeyring(assignment.key, assignment.value) + err := setAuthFieldInKeyring(activeProfile, assignment.key, assignment.value) if err != nil { t.Fatalf("Failed to set \"%s\" as \"%s\": %v", assignment.key, assignment.value, err) } @@ -231,7 +241,7 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { } for key, valueExpected := range tt.expectedValues { - value, err := getAuthFieldFromKeyring(key) + value, err := getAuthFieldFromKeyring(activeProfile, key) if err != nil { t.Errorf("Failed to get value of \"%s\": %v", key, err) continue @@ -308,8 +318,13 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { + activeProfile, err := config.GetProfile() + if err != nil { + t.Errorf("get profile: %v", err) + } + for _, assignment := range tt.valueAssignments { - err := setAuthFieldInEncodedTextFile(assignment.key, assignment.value) + err := setAuthFieldInEncodedTextFile(activeProfile, assignment.key, assignment.value) if err != nil { t.Fatalf("Failed to set \"%s\" as \"%s\": %v", assignment.key, assignment.value, err) } @@ -320,7 +335,7 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { } for key, valueExpected := range tt.expectedValues { - value, err := getAuthFieldFromEncodedTextFile(key) + value, err := getAuthFieldFromEncodedTextFile(activeProfile, key) if err != nil { t.Errorf("Failed to get value of \"%s\": %v", key, err) continue @@ -328,7 +343,7 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { t.Errorf("Value of field \"%s\" is wrong: expected \"%s\", got \"%s\"", key, valueExpected, value) } - err = deleteAuthFieldInEncodedTextFile(key) + err = deleteAuthFieldInEncodedTextFile(activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from text file: %v. Please remove it manually", key, err) } @@ -351,8 +366,8 @@ func deleteAuthFieldInKeyring(key authFieldKey) error { return keyring.Delete(keyringService, string(key)) } -func deleteAuthFieldInEncodedTextFile(key authFieldKey) error { - err := createEncodedTextFile() +func deleteAuthFieldInEncodedTextFile(activeProfile string, key authFieldKey) error { + err := createEncodedTextFile(activeProfile) if err != nil { return err } @@ -362,11 +377,6 @@ func deleteAuthFieldInEncodedTextFile(key authFieldKey) error { return fmt.Errorf("get config dir: %w", err) } - activeProfile, err := config.GetProfile() - if err != nil { - return fmt.Errorf("get profile: %w", err) - } - profileTextFileFolderName := textFileFolderName if activeProfile != "" { profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName) From 29b8be74fdf62d93a560ec763f505784429115d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Tue, 21 May 2024 13:49:17 +0200 Subject: [PATCH 3/4] adapt unit tests --- internal/pkg/auth/storage_test.go | 263 ++++++++++++++++++++++++++---- 1 file changed, 228 insertions(+), 35 deletions(-) diff --git a/internal/pkg/auth/storage_test.go b/internal/pkg/auth/storage_test.go index df9c47b32..21d66c660 100644 --- a/internal/pkg/auth/storage_test.go +++ b/internal/pkg/auth/storage_test.go @@ -12,6 +12,7 @@ import ( "github.com/zalando/go-keyring" "github.com/stackitcloud/stackit-cli/internal/pkg/config" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" ) func TestSetGetAuthField(t *testing.T) { @@ -32,9 +33,11 @@ func TestSetGetAuthField(t *testing.T) { keyringFails bool valueAssignments []valueAssignment expectedValues map[authFieldKey]string + activeProfile string }{ { - description: "simple assignments", + description: "simple assignments with default profile", + activeProfile: "", valueAssignments: []valueAssignment{ { key: testField1, @@ -51,8 +54,9 @@ func TestSetGetAuthField(t *testing.T) { }, }, { - description: "simple assignments w/ keyring failing", - keyringFails: true, + description: "simple assignments w/ keyring failing with default profile", + activeProfile: "", + keyringFails: true, valueAssignments: []valueAssignment{ { key: testField1, @@ -69,7 +73,8 @@ func TestSetGetAuthField(t *testing.T) { }, }, { - description: "overlapping assignments", + description: "overlapping assignments with default profile", + activeProfile: "", valueAssignments: []valueAssignment{ { key: testField1, @@ -90,8 +95,91 @@ func TestSetGetAuthField(t *testing.T) { }, }, { - description: "overlapping assignments w/ keyring failing", - keyringFails: true, + description: "overlapping assignments w/ keyring failing with default profile", + activeProfile: "", + keyringFails: true, + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + { + key: testField1, + value: testValue3, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue3, + testField2: testValue2, + }, + }, + { + description: "simple assignments with testProfile", + activeProfile: "testProfile", + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue1, + testField2: testValue2, + }, + }, + { + description: "simple assignments w/ keyring failing with testProfile", + activeProfile: "testProfile", + keyringFails: true, + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue1, + testField2: testValue2, + }, + }, + { + description: "overlapping assignments with testProfile", + activeProfile: "testProfile", + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + { + key: testField1, + value: testValue3, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue3, + testField2: testValue2, + }, + }, + { + description: "overlapping assignments w/ keyring failing with testProfile", + activeProfile: "testProfile", + keyringFails: true, valueAssignments: []valueAssignment{ { key: testField1, @@ -115,17 +203,25 @@ func TestSetGetAuthField(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - activeProfile, err := config.GetProfile() - if err != nil { - t.Errorf("get profile: %v", err) - } - if !tt.keyringFails { keyring.MockInit() } else { keyring.MockInitWithError(fmt.Errorf("keyring unavailable for testing")) } + p := print.NewPrinter() + if tt.activeProfile != "" { + err := config.SetProfile(p, tt.activeProfile) + if err != nil { + t.Errorf("set profile: %v", err) + } + } else { + err := config.UnsetProfile(p) + if err != nil { + t.Errorf("unset profile: %v", err) + } + } + for _, assignment := range tt.valueAssignments { err := SetAuthField(assignment.key, assignment.value) if err != nil { @@ -147,12 +243,12 @@ func TestSetGetAuthField(t *testing.T) { } if !tt.keyringFails { - err = deleteAuthFieldInKeyring(key) + err = deleteAuthFieldInKeyring(tt.activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from keyring: %v. Please remove it manually", key, err) } } else { - err = deleteAuthFieldInEncodedTextFile(activeProfile, key) + err = deleteAuthFieldInEncodedTextFile(tt.activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from text file: %v. Please remove it manually", key, err) } @@ -179,9 +275,51 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { description string valueAssignments []valueAssignment expectedValues map[authFieldKey]string + activeProfile string }{ { - description: "simple assignments", + description: "simple assignments with default profile", + activeProfile: "", + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue1, + testField2: testValue2, + }, + }, + { + description: "overlapping assignments with default profile", + activeProfile: "", + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + { + key: testField1, + value: testValue3, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue3, + testField2: testValue2, + }, + }, + { + description: "simple assignments with testProfile", + activeProfile: "testProfile", valueAssignments: []valueAssignment{ { key: testField1, @@ -198,7 +336,8 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { }, }, { - description: "overlapping assignments", + description: "overlapping assignments with testProfile", + activeProfile: "testProfile", valueAssignments: []valueAssignment{ { key: testField1, @@ -224,13 +363,21 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { t.Run(tt.description, func(t *testing.T) { keyring.MockInit() - activeProfile, err := config.GetProfile() - if err != nil { - t.Errorf("get profile: %v", err) + p := print.NewPrinter() + if tt.activeProfile != "" { + err := config.SetProfile(p, tt.activeProfile) + if err != nil { + t.Errorf("set profile: %v", err) + } + } else { + err := config.UnsetProfile(p) + if err != nil { + t.Errorf("unset profile: %v", err) + } } for _, assignment := range tt.valueAssignments { - err := setAuthFieldInKeyring(activeProfile, assignment.key, assignment.value) + err := setAuthFieldInKeyring(tt.activeProfile, assignment.key, assignment.value) if err != nil { t.Fatalf("Failed to set \"%s\" as \"%s\": %v", assignment.key, assignment.value, err) } @@ -241,7 +388,7 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { } for key, valueExpected := range tt.expectedValues { - value, err := getAuthFieldFromKeyring(activeProfile, key) + value, err := getAuthFieldFromKeyring(tt.activeProfile, key) if err != nil { t.Errorf("Failed to get value of \"%s\": %v", key, err) continue @@ -249,7 +396,7 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { t.Errorf("Value of field \"%s\" is wrong: expected \"%s\", got \"%s\"", key, valueExpected, value) } - err = deleteAuthFieldInKeyring(key) + err = deleteAuthFieldInKeyring(tt.activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from keyring: %v. Please remove it manually", key, err) } @@ -273,11 +420,13 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { tests := []struct { description string + activeProfile string valueAssignments []valueAssignment expectedValues map[authFieldKey]string }{ { - description: "simple assignments", + description: "simple assignments with default profile", + activeProfile: "", valueAssignments: []valueAssignment{ { key: testField1, @@ -294,7 +443,48 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { }, }, { - description: "overlapping assignments", + description: "overlapping assignments with default profile", + activeProfile: "", + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + { + key: testField1, + value: testValue3, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue3, + testField2: testValue2, + }, + }, + { + description: "simple assignments with testProfile", + activeProfile: "testProfile", + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue1, + testField2: testValue2, + }, + }, + { + description: "overlapping assignments with testProfile", + activeProfile: "testProfile", valueAssignments: []valueAssignment{ { key: testField1, @@ -318,13 +508,21 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - activeProfile, err := config.GetProfile() - if err != nil { - t.Errorf("get profile: %v", err) + p := print.NewPrinter() + if tt.activeProfile != "" { + err := config.SetProfile(p, tt.activeProfile) + if err != nil { + t.Errorf("set profile: %v", err) + } + } else { + err := config.UnsetProfile(p) + if err != nil { + t.Errorf("unset profile: %v", err) + } } for _, assignment := range tt.valueAssignments { - err := setAuthFieldInEncodedTextFile(activeProfile, assignment.key, assignment.value) + err := setAuthFieldInEncodedTextFile(tt.activeProfile, assignment.key, assignment.value) if err != nil { t.Fatalf("Failed to set \"%s\" as \"%s\": %v", assignment.key, assignment.value, err) } @@ -335,7 +533,7 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { } for key, valueExpected := range tt.expectedValues { - value, err := getAuthFieldFromEncodedTextFile(activeProfile, key) + value, err := getAuthFieldFromEncodedTextFile(tt.activeProfile, key) if err != nil { t.Errorf("Failed to get value of \"%s\": %v", key, err) continue @@ -343,7 +541,7 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { t.Errorf("Value of field \"%s\" is wrong: expected \"%s\", got \"%s\"", key, valueExpected, value) } - err = deleteAuthFieldInEncodedTextFile(activeProfile, key) + err = deleteAuthFieldInEncodedTextFile(tt.activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from text file: %v. Please remove it manually", key, err) } @@ -352,12 +550,7 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { } } -func deleteAuthFieldInKeyring(key authFieldKey) error { - activeProfile, err := config.GetProfile() - if err != nil { - return fmt.Errorf("get profile: %w", err) - } - +func deleteAuthFieldInKeyring(activeProfile string, key authFieldKey) error { if activeProfile != "" { activeProfileKeyring := filepath.Join(keyringService, activeProfile) return keyring.Delete(activeProfileKeyring, string(key)) From 7fa394b1506a8fb0f1b428b10c37e2491628eb97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Go=CC=88kc=CC=A7e=20Go=CC=88k=20Klingel?= Date: Tue, 21 May 2024 15:07:04 +0200 Subject: [PATCH 4/4] adapt unit tests --- internal/pkg/auth/storage_test.go | 148 +++--------------------------- 1 file changed, 13 insertions(+), 135 deletions(-) diff --git a/internal/pkg/auth/storage_test.go b/internal/pkg/auth/storage_test.go index 21d66c660..5733ab69a 100644 --- a/internal/pkg/auth/storage_test.go +++ b/internal/pkg/auth/storage_test.go @@ -12,7 +12,6 @@ import ( "github.com/zalando/go-keyring" "github.com/stackitcloud/stackit-cli/internal/pkg/config" - "github.com/stackitcloud/stackit-cli/internal/pkg/print" ) func TestSetGetAuthField(t *testing.T) { @@ -33,93 +32,9 @@ func TestSetGetAuthField(t *testing.T) { keyringFails bool valueAssignments []valueAssignment expectedValues map[authFieldKey]string - activeProfile string }{ { - description: "simple assignments with default profile", - activeProfile: "", - valueAssignments: []valueAssignment{ - { - key: testField1, - value: testValue1, - }, - { - key: testField2, - value: testValue2, - }, - }, - expectedValues: map[authFieldKey]string{ - testField1: testValue1, - testField2: testValue2, - }, - }, - { - description: "simple assignments w/ keyring failing with default profile", - activeProfile: "", - keyringFails: true, - valueAssignments: []valueAssignment{ - { - key: testField1, - value: testValue1, - }, - { - key: testField2, - value: testValue2, - }, - }, - expectedValues: map[authFieldKey]string{ - testField1: testValue1, - testField2: testValue2, - }, - }, - { - description: "overlapping assignments with default profile", - activeProfile: "", - valueAssignments: []valueAssignment{ - { - key: testField1, - value: testValue1, - }, - { - key: testField2, - value: testValue2, - }, - { - key: testField1, - value: testValue3, - }, - }, - expectedValues: map[authFieldKey]string{ - testField1: testValue3, - testField2: testValue2, - }, - }, - { - description: "overlapping assignments w/ keyring failing with default profile", - activeProfile: "", - keyringFails: true, - valueAssignments: []valueAssignment{ - { - key: testField1, - value: testValue1, - }, - { - key: testField2, - value: testValue2, - }, - { - key: testField1, - value: testValue3, - }, - }, - expectedValues: map[authFieldKey]string{ - testField1: testValue3, - testField2: testValue2, - }, - }, - { - description: "simple assignments with testProfile", - activeProfile: "testProfile", + description: "simple assignments", valueAssignments: []valueAssignment{ { key: testField1, @@ -136,9 +51,8 @@ func TestSetGetAuthField(t *testing.T) { }, }, { - description: "simple assignments w/ keyring failing with testProfile", - activeProfile: "testProfile", - keyringFails: true, + description: "simple assignments w/ keyring failing", + keyringFails: true, valueAssignments: []valueAssignment{ { key: testField1, @@ -155,8 +69,7 @@ func TestSetGetAuthField(t *testing.T) { }, }, { - description: "overlapping assignments with testProfile", - activeProfile: "testProfile", + description: "overlapping assignments", valueAssignments: []valueAssignment{ { key: testField1, @@ -177,9 +90,8 @@ func TestSetGetAuthField(t *testing.T) { }, }, { - description: "overlapping assignments w/ keyring failing with testProfile", - activeProfile: "testProfile", - keyringFails: true, + description: "overlapping assignments w/ keyring failing", + keyringFails: true, valueAssignments: []valueAssignment{ { key: testField1, @@ -203,25 +115,17 @@ func TestSetGetAuthField(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { + activeProfile, err := config.GetProfile() + if err != nil { + t.Errorf("get profile: %v", err) + } + if !tt.keyringFails { keyring.MockInit() } else { keyring.MockInitWithError(fmt.Errorf("keyring unavailable for testing")) } - p := print.NewPrinter() - if tt.activeProfile != "" { - err := config.SetProfile(p, tt.activeProfile) - if err != nil { - t.Errorf("set profile: %v", err) - } - } else { - err := config.UnsetProfile(p) - if err != nil { - t.Errorf("unset profile: %v", err) - } - } - for _, assignment := range tt.valueAssignments { err := SetAuthField(assignment.key, assignment.value) if err != nil { @@ -243,12 +147,12 @@ func TestSetGetAuthField(t *testing.T) { } if !tt.keyringFails { - err = deleteAuthFieldInKeyring(tt.activeProfile, key) + err = deleteAuthFieldInKeyring(activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from keyring: %v. Please remove it manually", key, err) } } else { - err = deleteAuthFieldInEncodedTextFile(tt.activeProfile, key) + err = deleteAuthFieldInEncodedTextFile(activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from text file: %v. Please remove it manually", key, err) } @@ -363,19 +267,6 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { t.Run(tt.description, func(t *testing.T) { keyring.MockInit() - p := print.NewPrinter() - if tt.activeProfile != "" { - err := config.SetProfile(p, tt.activeProfile) - if err != nil { - t.Errorf("set profile: %v", err) - } - } else { - err := config.UnsetProfile(p) - if err != nil { - t.Errorf("unset profile: %v", err) - } - } - for _, assignment := range tt.valueAssignments { err := setAuthFieldInKeyring(tt.activeProfile, assignment.key, assignment.value) if err != nil { @@ -508,19 +399,6 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - p := print.NewPrinter() - if tt.activeProfile != "" { - err := config.SetProfile(p, tt.activeProfile) - if err != nil { - t.Errorf("set profile: %v", err) - } - } else { - err := config.UnsetProfile(p) - if err != nil { - t.Errorf("unset profile: %v", err) - } - } - for _, assignment := range tt.valueAssignments { err := setAuthFieldInEncodedTextFile(tt.activeProfile, assignment.key, assignment.value) if err != nil {