-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathutils_test.go
More file actions
203 lines (187 loc) · 5.33 KB
/
utils_test.go
File metadata and controls
203 lines (187 loc) · 5.33 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package auth
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/spf13/viper"
"github.com/zalando/go-keyring"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
)
func TestGetWellKnownConfig(t *testing.T) {
tests := []struct {
name string
idpCustomEndpoint string
allowedUrlDomain string
isValid bool
expected string
}{
{
name: "custom endpoint specified",
idpCustomEndpoint: "https://example.stackit.cloud",
allowedUrlDomain: "stackit.cloud",
isValid: true,
expected: "https://example.stackit.cloud",
},
{
name: "custom endpoint outside STACKIT",
idpCustomEndpoint: "https://www.very-suspicious-website.com/",
allowedUrlDomain: "stackit.cloud",
isValid: false,
},
{
name: "non-STACKIT custom endpoint invalid",
idpCustomEndpoint: "https://www.very-suspicious-website.com/",
allowedUrlDomain: "stackit.cloud",
isValid: false,
},
{
name: "non-STACKIT custom endpoint valid",
idpCustomEndpoint: "https://www.test.example.com/",
allowedUrlDomain: "example.com",
isValid: true,
expected: "https://www.test.example.com/",
},
{
name: "every URL valid",
idpCustomEndpoint: "https://www.test.example.com/",
allowedUrlDomain: "",
isValid: true,
expected: "https://www.test.example.com/",
},
{
name: "custom endpoint not specified",
idpCustomEndpoint: "",
allowedUrlDomain: "",
isValid: true,
expected: defaultWellKnownConfig,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
viper.Reset()
viper.Set(config.IdentityProviderCustomWellKnownConfigurationKey, tt.idpCustomEndpoint)
viper.Set(config.AllowedUrlDomainKey, tt.allowedUrlDomain)
got, err := getIDPWellKnownConfigURL()
if tt.isValid && err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !tt.isValid && err == nil {
t.Fatalf("expected error, got none")
}
if got != tt.expected {
t.Fatalf("expected idp endpoint %q, got %q", tt.expected, got)
}
})
}
}
func TestGetIDPClientID(t *testing.T) {
tests := []struct {
name string
idpCustomClientID string
isValid bool
expected string
}{
{
name: "custom client ID specified",
idpCustomClientID: "custom-client-id",
isValid: true,
expected: "custom-client-id",
},
{
name: "custom client ID not specified",
idpCustomClientID: "",
isValid: true,
expected: defaultCLIClientID,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
viper.Reset()
viper.Set(config.IdentityProviderCustomClientIdKey, tt.idpCustomClientID)
got, err := getIDPClientID()
if tt.isValid && err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !tt.isValid && err == nil {
t.Fatalf("expected error, got none")
}
if got != tt.expected {
t.Fatalf("expected idp client ID %q, got %q", tt.expected, got)
}
})
}
}
func TestParseWellKnownConfig(t *testing.T) {
tests := []struct {
name string
getFails bool
getResponse string
isValid bool
expected *wellKnownConfig
}{
{
name: "success",
getFails: false,
getResponse: `{"issuer":"https://issuer.stackit.cloud/endpoint","authorization_endpoint":"https://auth.stackit.cloud/enpoint","token_endpoint":"https://token.stackit.cloud/endpoint"}`,
isValid: true,
expected: &wellKnownConfig{ //nolint:gosec // just a testcase; no credentials
Issuer: "https://issuer.stackit.cloud/endpoint",
AuthorizationEndpoint: "https://auth.stackit.cloud/enpoint",
TokenEndpoint: "https://token.stackit.cloud/endpoint",
},
},
{
name: "get_fails",
getFails: true,
getResponse: "",
isValid: false,
expected: nil,
},
{
name: "empty_response",
getFails: true,
getResponse: "",
isValid: false,
expected: nil,
},
{
name: "missing_issuer",
getFails: true,
getResponse: `{"authorization_endpoint":"https://auth.stackit.cloud/enpoint","token_endpoint":"https://token.stackit.cloud/endpoint"}`,
isValid: false,
expected: nil,
},
{
name: "missing_authorization",
getFails: true,
getResponse: `{"issuer":"https://issuer.stackit.cloud/endpoint","token_endpoint":"https://token.stackit.cloud/endpoint"}`,
isValid: false,
expected: nil,
},
{
name: "missing_token",
getFails: true,
getResponse: `{"issuer":"https://issuer.stackit.cloud/endpoint","authorization_endpoint":"https://auth.stackit.cloud/enpoint"}`,
isValid: false,
expected: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
keyring.MockInit()
testClient := apiClientMocked{
tt.getFails,
tt.getResponse,
}
got, err := parseWellKnownConfiguration(&testClient, "")
if tt.isValid && err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !tt.isValid && err == nil {
t.Fatalf("expected error, got none")
}
if tt.isValid && !cmp.Equal(*got, *tt.expected) {
t.Fatalf("expected %v, got %v", tt.expected, got)
}
})
}
}