forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthokta_test.go
240 lines (221 loc) · 7.87 KB
/
authokta_test.go
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) 2017-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"errors"
"net/http"
"net/url"
"testing"
"time"
)
func TestUnitPostBackURL(t *testing.T) {
c := `<html><form id="1" action="https://abc.com/"></form></html>`
pbURL, err := postBackURL([]byte(c))
if err != nil {
t.Fatalf("failed to get URL. err: %v, %v", err, c)
}
if pbURL.String() != "https://abc.com/" {
t.Errorf("failed to get URL. got: %v, %v", pbURL, c)
}
c = `<html></html>`
_, err = postBackURL([]byte(c))
if err == nil {
t.Fatalf("should have failed")
}
c = `<html><form id="1"/></html>`
_, err = postBackURL([]byte(c))
if err == nil {
t.Fatalf("should have failed")
}
c = `<html><form id="1" action="https://abc.com//></html>`
_, err = postBackURL([]byte(c))
if err == nil {
t.Fatalf("should have failed")
}
}
func getTestError(_ context.Context, _ *snowflakeRestful, _ *url.URL, _ map[string]string, _ time.Duration) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: &fakeResponseBody{body: []byte{0x12, 0x34}},
}, errors.New("failed to run post method")
}
func getTestAppBadGatewayError(_ context.Context, _ *snowflakeRestful, _ *url.URL, _ map[string]string, _ time.Duration) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusBadGateway,
Body: &fakeResponseBody{body: []byte{0x12, 0x34}},
}, nil
}
func getTestHTMLSuccess(_ context.Context, _ *snowflakeRestful, _ *url.URL, _ map[string]string, _ time.Duration) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: &fakeResponseBody{body: []byte("<htm></html>")},
}, nil
}
func TestUnitPostAuthSAML(t *testing.T) {
sr := &snowflakeRestful{
FuncPost: postTestError,
TokenAccessor: getSimpleTokenAccessor(),
}
var err error
_, err = postAuthSAML(context.TODO(), sr, make(map[string]string), []byte{}, 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPost = postTestAppBadGatewayError
_, err = postAuthSAML(context.TODO(), sr, make(map[string]string), []byte{}, 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPost = postTestSuccessButInvalidJSON
_, err = postAuthSAML(context.TODO(), sr, make(map[string]string), []byte{0x12, 0x34}, 0)
if err == nil {
t.Fatalf("should have failed to post")
}
}
func TestUnitPostAuthOKTA(t *testing.T) {
sr := &snowflakeRestful{
FuncPost: postTestError,
TokenAccessor: getSimpleTokenAccessor(),
}
var err error
_, err = postAuthOKTA(context.TODO(), sr, make(map[string]string), []byte{}, "hahah", 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPost = postTestAppBadGatewayError
_, err = postAuthOKTA(context.TODO(), sr, make(map[string]string), []byte{}, "hahah", 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPost = postTestSuccessButInvalidJSON
_, err = postAuthOKTA(context.TODO(), sr, make(map[string]string), []byte{0x12, 0x34}, "haha", 0)
if err == nil {
t.Fatal("should have failed to run post request after the renewal")
}
}
func TestUnitGetSSO(t *testing.T) {
sr := &snowflakeRestful{
FuncGet: getTestError,
TokenAccessor: getSimpleTokenAccessor(),
}
var err error
_, err = getSSO(context.TODO(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncGet = getTestAppBadGatewayError
_, err = getSSO(context.TODO(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncGet = getTestHTMLSuccess
_, err = getSSO(context.TODO(), sr, &url.Values{}, make(map[string]string), "hahah", 0)
if err != nil {
t.Fatalf("failed to get HTML content. err: %v", err)
}
}
func postAuthSAMLError(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{}, errors.New("failed to get SAML response")
}
func postAuthSAMLAuthFail(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{
Success: false,
Message: "SAML auth failed",
}, nil
}
func postAuthSAMLAuthSuccessButInvalidURL(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{
Success: true,
Message: "",
Data: authResponseMain{
TokenURL: "https://1abc.com/token",
SSOURL: "https://2abc.com/sso",
},
}, nil
}
func postAuthSAMLAuthSuccess(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{
Success: true,
Message: "",
Data: authResponseMain{
TokenURL: "https://abc.com/token",
SSOURL: "https://abc.com/sso",
},
}, nil
}
func postAuthOKTAError(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ string, _ time.Duration) (*authOKTAResponse, error) {
return &authOKTAResponse{}, errors.New("failed to get SAML response")
}
func postAuthOKTASuccess(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ string, _ time.Duration) (*authOKTAResponse, error) {
return &authOKTAResponse{}, nil
}
func getSSOError(_ context.Context, _ *snowflakeRestful, _ *url.Values, _ map[string]string, _ string, _ time.Duration) ([]byte, error) {
return []byte{}, errors.New("failed to get SSO html")
}
func getSSOSuccessButInvalidURL(_ context.Context, _ *snowflakeRestful, _ *url.Values, _ map[string]string, _ string, _ time.Duration) ([]byte, error) {
return []byte(`<html><form id="1"/></html>`), nil
}
func getSSOSuccess(_ context.Context, _ *snowflakeRestful, _ *url.Values, _ map[string]string, _ string, _ time.Duration) ([]byte, error) {
return []byte(`<html><form id="1" action="https://abc.com/"></form></html>`), nil
}
func TestUnitAuthenticateBySAML(t *testing.T) {
authenticator := &url.URL{
Scheme: "https",
Host: "abc.com",
}
application := "testapp"
account := "testaccount"
user := "u"
password := "p"
sr := &snowflakeRestful{
Protocol: "https",
Host: "abc.com",
Port: 443,
FuncPostAuthSAML: postAuthSAMLError,
TokenAccessor: getSimpleTokenAccessor(),
}
var err error
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPostAuthSAML = postAuthSAMLAuthFail
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPostAuthSAML = postAuthSAMLAuthSuccessButInvalidURL
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
if err == nil {
t.Fatal("should have failed.")
}
driverErr, ok := err.(*SnowflakeError)
if !ok {
t.Fatalf("should be snowflake error. err: %v", err)
}
if driverErr.Number != ErrCodeIdpConnectionError {
t.Fatalf("unexpected error code. expected: %v, got: %v", ErrCodeIdpConnectionError, driverErr.Number)
}
sr.FuncPostAuthSAML = postAuthSAMLAuthSuccess
sr.FuncPostAuthOKTA = postAuthOKTAError
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPostAuthOKTA = postAuthOKTASuccess
sr.FuncGetSSO = getSSOError
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncGetSSO = getSSOSuccessButInvalidURL
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncGetSSO = getSSOSuccess
_, err = authenticateBySAML(context.TODO(), sr, authenticator, application, account, user, password)
if err != nil {
t.Fatalf("failed. err: %v", err)
}
}