-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathchecks_test.go
More file actions
139 lines (126 loc) · 3.88 KB
/
checks_test.go
File metadata and controls
139 lines (126 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package core_test
import (
"context"
"errors"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"github.com/scaleway/scaleway-cli/v2/core"
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/assert"
)
func TestCheckAPIKey(t *testing.T) {
testCommands := core.NewCommands(
&core.Command{
Namespace: "test",
ArgSpecs: core.ArgSpecs{},
ArgsType: reflect.TypeOf(testType{}),
Run: func(ctx context.Context, _ any) (i any, e error) {
// Test command reload the client so the profile used is the edited one
return "", core.ReloadClient(ctx)
},
})
metadataKey := "ApiKey"
t.Run("basic", core.Test(&core.TestConfig{
Commands: testCommands,
TmpHomeDir: true,
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
api := iam.NewAPI(ctx.Client)
accessKey, exists := ctx.Client.GetAccessKey()
if !exists {
return errors.New("missing access-key")
}
apiKey, err := api.GetAPIKey(&iam.GetAPIKeyRequest{
AccessKey: accessKey,
})
if err != nil {
return err
}
expiresAt := time.Now().Add(time.Hour)
apiKey, err = api.CreateAPIKey(&iam.CreateAPIKeyRequest{
ApplicationID: apiKey.ApplicationID,
UserID: apiKey.UserID,
ExpiresAt: &expiresAt,
Description: "test-cli-TestCheckAPIKey",
})
if err != nil {
return err
}
if !*core.UpdateCassettes {
apiKey.AccessKey = "SCWXXXXXXXXXXXXXXXXX"
}
ctx.Meta[metadataKey] = apiKey
cfg := &scw.Config{
Profile: scw.Profile{
AccessKey: &apiKey.AccessKey,
SecretKey: apiKey.SecretKey,
DefaultProjectID: &apiKey.DefaultProjectID,
DefaultOrganizationID: &apiKey.DefaultProjectID,
},
}
configPath := filepath.Join(ctx.OverrideEnv["HOME"], ".config", "scw", "config.yaml")
return cfg.SaveTo(configPath)
},
Cmd: "scw test",
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.True(t, strings.HasPrefix(ctx.LogBuffer, "Current api key expires in"))
},
),
AfterFunc: func(ctx *core.AfterFuncCtx) error {
return iam.NewAPI(ctx.Client).DeleteAPIKey(&iam.DeleteAPIKeyRequest{
AccessKey: ctx.Meta[metadataKey].(*iam.APIKey).AccessKey,
})
},
}))
}
func TestCheckIfMultipleVariableSources(t *testing.T) {
testCommands := core.NewCommands(
&core.Command{
Namespace: "test",
ArgSpecs: core.ArgSpecs{},
ArgsType: reflect.TypeOf(testType{}),
Run: func(ctx context.Context, _ any) (any, error) { return "", nil },
},
)
t.Run("conflicting sources should trigger warning", core.Test(&core.TestConfig{
Commands: testCommands,
TmpHomeDir: true,
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
cfg := &scw.Config{
Profile: scw.Profile{
AccessKey: scw.StringPtr("SCW11111111111111111"),
SecretKey: scw.StringPtr("config-secret"),
DefaultProjectID: scw.StringPtr("config-project-id"),
},
}
configPath := filepath.Join(ctx.OverrideEnv["HOME"], ".config", "scw", "config.yaml")
if err := cfg.SaveTo(configPath); err != nil {
return err
}
t.Setenv("SCW_ACCESS_KEY", "SCW99999999999999999")
t.Setenv("SCW_SECRET_KEY", "env-secret")
t.Setenv("SCW_DEFAULT_PROJECT_ID", "config-project-id")
return nil
},
Cmd: "scw test",
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
expected := "" +
"Checking multiple variable sources: \n" +
"- Variable 'AccessKey' is defined in both config.yaml and environment with different values. " +
"Using: environment variable.\n" +
"- Variable 'SecretKey' is defined in both config.yaml and environment with different values. " +
"Using: environment variable.\n\n"
assert.Equal(t, expected, ctx.LogBuffer)
},
),
}))
}