forked from stackitcloud/stackit-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackit_cli_flow_test.go
More file actions
166 lines (147 loc) · 4.3 KB
/
stackit_cli_flow_test.go
File metadata and controls
166 lines (147 loc) · 4.3 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package clients
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
//nolint:gosec // testServiceAccountToken is a test token
const testServiceAccountToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImR1bW15QGV4YW1wbGUuY29tIiwiZXhwIjo5MDA3MTkyNTQ3NDA5OTF9.sM2yd5GL9kK4h8IKHbr_fA2XmrzEsLOeLTIPrU0VfMg"
func TestSTACKITCLIFlow_Init(t *testing.T) {
type args struct {
cfg *STACKITCLIFlowConfig
confFn func(t *testing.T)
}
tests := []struct {
name string
args args
wantErr bool
}{
{"ok", args{
cfg: &STACKITCLIFlowConfig{},
confFn: func(t *testing.T) {
_, err := RunSTACKITCLICommand(context.TODO(), "stackit auth activate-service-account --service-account-token="+testServiceAccountToken)
if err != nil {
t.Errorf("RunSTACKITCLICommand() error = %v", err)
return
}
},
}, false},
{"no-token", args{
cfg: &STACKITCLIFlowConfig{},
confFn: func(_ *testing.T) {},
}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.TODO()
c := &STACKITCLIFlow{}
cliProfileName := "test-stackit-cli-flow-init" + tt.name
_, _ = RunSTACKITCLICommand(ctx, fmt.Sprintf("stackit config profile delete %s -y", cliProfileName))
_, err := RunSTACKITCLICommand(ctx, "stackit config profile create "+cliProfileName)
if err != nil {
t.Errorf("RunSTACKITCLICommand() error = %v", err)
return
}
tt.args.confFn(t)
defer func() {
_, _ = RunSTACKITCLICommand(ctx, fmt.Sprintf("stackit config profile delete %s -y", cliProfileName))
}()
if err := c.Init(tt.args.cfg); err != nil {
if (err != nil) != tt.wantErr {
t.Errorf("TokenFlow.Init() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
if c.config == nil {
t.Error("config is nil")
}
})
}
}
func TestSTACKITCLIFlow_Do(t *testing.T) {
type fields struct {
client *http.Client
config *STACKITCLIFlowConfig
}
type args struct{}
tests := []struct {
name string
fields fields
args args
want int
wantErr bool
}{
{"success", fields{&http.Client{}, &STACKITCLIFlowConfig{}}, args{}, http.StatusOK, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.TODO()
_, _ = RunSTACKITCLICommand(ctx, "stackit config profile delete test-stackit-cli-flow-do -y")
_, err := RunSTACKITCLICommand(ctx, "stackit config profile create test-stackit-cli-flow-do")
if err != nil {
t.Errorf("RunSTACKITCLICommand() error = %v", err)
return
}
_, err = RunSTACKITCLICommand(ctx, "stackit auth activate-service-account --service-account-token="+testServiceAccountToken)
if err != nil {
t.Errorf("RunSTACKITCLICommand() error = %v", err)
return
}
defer func() {
_, _ = RunSTACKITCLICommand(ctx, "stackit config profile delete test-stackit-cli-flow-do -y")
}()
c := &STACKITCLIFlow{}
err = c.Init(tt.fields.config)
if err != nil {
t.Errorf("Init() error = %v", err)
return
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authorization := r.Header.Get("Authorization")
if authorization != "Bearer "+testServiceAccountToken {
w.WriteHeader(http.StatusUnauthorized)
_, _ = fmt.Fprintln(w, `{"error":"missing authorization header"}`)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintln(w, `{"status":"ok"}`)
})
server := httptest.NewServer(handler)
defer server.Close()
u, err := url.Parse(server.URL)
if err != nil {
t.Error(err)
return
}
req, err := http.NewRequest(http.MethodGet, u.String(), http.NoBody)
if err != nil {
t.Error(err)
return
}
got, err := c.RoundTrip(req)
if err == nil {
// Defer discard and close the body
defer func() {
if _, discardErr := io.Copy(io.Discard, got.Body); discardErr != nil && err == nil {
err = discardErr
}
if closeErr := got.Body.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()
}
if (err != nil) != tt.wantErr {
t.Errorf("STACKITCLIFlow.Do() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != nil && got.StatusCode != tt.want {
t.Errorf("STACKITCLIFlow.Do() = %v, want %v", got.StatusCode, tt.want)
}
})
}
}