Skip to content

Commit 762b5c8

Browse files
committed
Add test for CLI
1 parent df57b26 commit 762b5c8

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

core/auth/auth.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ func SetupAuth(cfg *config.Configuration) (rt http.RoundTripper, err error) {
6363
return nil, fmt.Errorf("configuring token authentication: %w", err)
6464
}
6565
return tokenRoundTripper, nil
66-
} else if !cfg.DisableCLIAuthFlow {
67-
cliRoundTripper, err := StackitCliAuth(cfg)
68-
if err != nil {
69-
return nil, fmt.Errorf("configuring CLI authentication: %w", err)
70-
}
71-
return cliRoundTripper, nil
7266
}
7367

7468
authRoundTripper, err := DefaultAuth(cfg)

core/auth/auth_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package auth
22

33
import (
4+
"context"
45
"crypto/rand"
56
"crypto/rsa"
67
"crypto/x509"
@@ -17,6 +18,9 @@ import (
1718
"github.com/stackitcloud/stackit-sdk-go/core/config"
1819
)
1920

21+
//nolint:gosec // testServiceAccountToken is a test token
22+
const testServiceAccountToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImR1bW15QGV4YW1wbGUuY29tIiwiZXhwIjo5MDA3MTkyNTQ3NDA5OTF9.sM2yd5GL9kK4h8IKHbr_fA2XmrzEsLOeLTIPrU0VfMg"
23+
2024
func setTemporaryHome(t *testing.T) {
2125
old := userHomeDir
2226
t.Cleanup(func() {
@@ -150,6 +154,7 @@ func TestSetupAuth(t *testing.T) {
150154
setKeyPaths bool
151155
setCredentialsFilePathToken bool
152156
setCredentialsFilePathKey bool
157+
setCLIToken bool
153158
isValid bool
154159
}{
155160
{
@@ -189,6 +194,13 @@ func TestSetupAuth(t *testing.T) {
189194
setCredentialsFilePathToken: true,
190195
isValid: true,
191196
},
197+
{
198+
desc: "cli auth",
199+
config: nil,
200+
setCredentialsFilePathToken: false,
201+
setCLIToken: true,
202+
isValid: true,
203+
},
192204
{
193205
desc: "custom_config_token",
194206
config: &config.Configuration{
@@ -240,6 +252,25 @@ func TestSetupAuth(t *testing.T) {
240252
t.Setenv("STACKIT_CREDENTIALS_PATH", "")
241253
}
242254

255+
if test.setCLIToken {
256+
_, _ = clients.RunSTACKITCLICommand(context.TODO(), "stackit config profile delete test-setup-auth -y")
257+
_, err := clients.RunSTACKITCLICommand(context.TODO(), "stackit config profile create test-setup-auth")
258+
if err != nil {
259+
t.Errorf("RunSTACKITCLICommand() error = %v", err)
260+
return
261+
}
262+
263+
_, err = clients.RunSTACKITCLICommand(context.TODO(), "stackit auth activate-service-account --service-account-token="+testServiceAccountToken)
264+
if err != nil {
265+
t.Errorf("RunSTACKITCLICommand() error = %v", err)
266+
return
267+
}
268+
269+
defer func() {
270+
_, _ = clients.RunSTACKITCLICommand(context.TODO(), "stackit config profile delete test-setup-auth -y")
271+
}()
272+
}
273+
243274
t.Setenv("STACKIT_SERVICE_ACCOUNT_EMAIL", "test-email")
244275

245276
authRoundTripper, err := SetupAuth(test.config)

core/clients/stackit_cli_flow.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ func (c *STACKITCLIFlow) Init(cfg *STACKITCLIFlowConfig) error {
4040
}
4141

4242
func (c *STACKITCLIFlow) getTokenFromCLI() (string, error) {
43-
return runSTACKITCLICommand(context.TODO(), "stackit auth get-access-token")
43+
return RunSTACKITCLICommand(context.TODO(), "stackit auth get-access-token")
4444
}
4545

46-
// runSTACKITCLICommand executes the command line and returns the output.
47-
func runSTACKITCLICommand(ctx context.Context, commandLine string) (string, error) {
46+
// RunSTACKITCLICommand executes the command line and returns the output.
47+
func RunSTACKITCLICommand(ctx context.Context, commandLine string) (string, error) {
4848
var cliCmd *exec.Cmd
4949
if runtime.GOOS == "windows" {
5050
dir := os.Getenv("SYSTEMROOT")

core/clients/stackit_cli_flow_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ func TestSTACKITCLIFlow_Init(t *testing.T) {
2626
{"ok", args{
2727
cfg: &STACKITCLIFlowConfig{},
2828
confFn: func(t *testing.T) {
29-
_, err := runSTACKITCLICommand(context.TODO(), "stackit auth activate-service-account --service-account-token="+testServiceAccountToken)
29+
_, err := RunSTACKITCLICommand(context.TODO(), "stackit auth activate-service-account --service-account-token="+testServiceAccountToken)
3030
if err != nil {
31-
t.Errorf("runSTACKITCLICommand() error = %v", err)
31+
t.Errorf("RunSTACKITCLICommand() error = %v", err)
3232
return
3333
}
3434
},
@@ -46,17 +46,17 @@ func TestSTACKITCLIFlow_Init(t *testing.T) {
4646

4747
cliProfileName := "test-stackit-cli-flow-init" + tt.name
4848

49-
_, _ = runSTACKITCLICommand(ctx, fmt.Sprintf("stackit config profile delete %s -y", cliProfileName))
50-
_, err := runSTACKITCLICommand(ctx, "stackit config profile create "+cliProfileName)
49+
_, _ = RunSTACKITCLICommand(ctx, fmt.Sprintf("stackit config profile delete %s -y", cliProfileName))
50+
_, err := RunSTACKITCLICommand(ctx, "stackit config profile create "+cliProfileName)
5151
if err != nil {
52-
t.Errorf("runSTACKITCLICommand() error = %v", err)
52+
t.Errorf("RunSTACKITCLICommand() error = %v", err)
5353
return
5454
}
5555

5656
tt.args.confFn(t)
5757

5858
defer func() {
59-
_, _ = runSTACKITCLICommand(ctx, fmt.Sprintf("stackit config profile delete %s -y", cliProfileName))
59+
_, _ = RunSTACKITCLICommand(ctx, fmt.Sprintf("stackit config profile delete %s -y", cliProfileName))
6060
}()
6161

6262
if err := c.Init(tt.args.cfg); err != nil {
@@ -93,21 +93,21 @@ func TestSTACKITCLIFlow_Do(t *testing.T) {
9393
t.Run(tt.name, func(t *testing.T) {
9494
ctx := context.TODO()
9595

96-
_, _ = runSTACKITCLICommand(ctx, "stackit config profile delete test-stackit-cli-flow-do -y")
97-
_, err := runSTACKITCLICommand(ctx, "stackit config profile create test-stackit-cli-flow-do")
96+
_, _ = RunSTACKITCLICommand(ctx, "stackit config profile delete test-stackit-cli-flow-do -y")
97+
_, err := RunSTACKITCLICommand(ctx, "stackit config profile create test-stackit-cli-flow-do")
9898
if err != nil {
99-
t.Errorf("runSTACKITCLICommand() error = %v", err)
99+
t.Errorf("RunSTACKITCLICommand() error = %v", err)
100100
return
101101
}
102102

103-
_, err = runSTACKITCLICommand(ctx, "stackit auth activate-service-account --service-account-token="+testServiceAccountToken)
103+
_, err = RunSTACKITCLICommand(ctx, "stackit auth activate-service-account --service-account-token="+testServiceAccountToken)
104104
if err != nil {
105-
t.Errorf("runSTACKITCLICommand() error = %v", err)
105+
t.Errorf("RunSTACKITCLICommand() error = %v", err)
106106
return
107107
}
108108

109109
defer func() {
110-
_, _ = runSTACKITCLICommand(ctx, "stackit config profile delete test-stackit-cli-flow-do -y")
110+
_, _ = RunSTACKITCLICommand(ctx, "stackit config profile delete test-stackit-cli-flow-do -y")
111111
}()
112112

113113
c := &STACKITCLIFlow{}

0 commit comments

Comments
 (0)