-
Notifications
You must be signed in to change notification settings - Fork 125
/
authexternalbrowser_test.go
176 lines (158 loc) · 5.76 KB
/
authexternalbrowser_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
// Copyright (c) 2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"errors"
"net/url"
"strings"
"testing"
"time"
)
func TestGetTokenFromResponseFail(t *testing.T) {
response := "GET /?fakeToken=fakeEncodedSamlToken HTTP/1.1\r\n" +
"Host: localhost:54001\r\n" +
"Connection: keep-alive\r\n" +
"Upgrade-Insecure-Requests: 1\r\n" +
"User-Agent: userAgentStr\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n" +
"Referer: https://myaccount.snowflakecomputing.com/fed/login\r\n" +
"Accept-Encoding: gzip, deflate, br\r\n" +
"Accept-Language: en-US,en;q=0.9\r\n\r\n"
_, err := getTokenFromResponse(response)
if err == nil {
t.Errorf("Should have failed parsing the malformed response.")
}
}
func TestGetTokenFromResponse(t *testing.T) {
response := "GET /?token=GETtokenFromResponse HTTP/1.1\r\n" +
"Host: localhost:54001\r\n" +
"Connection: keep-alive\r\n" +
"Upgrade-Insecure-Requests: 1\r\n" +
"User-Agent: userAgentStr\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n" +
"Referer: https://myaccount.snowflakecomputing.com/fed/login\r\n" +
"Accept-Encoding: gzip, deflate, br\r\n" +
"Accept-Language: en-US,en;q=0.9\r\n\r\n"
expected := "GETtokenFromResponse"
token, err := getTokenFromResponse(response)
if err != nil {
t.Errorf("Failed to get the token. Err: %#v", err)
}
if token != expected {
t.Errorf("Expected: %s, found: %s", expected, token)
}
}
func TestBuildResponse(t *testing.T) {
resp, err := buildResponse("Go")
assertNilF(t, err)
bytes := resp.Bytes()
respStr := string(bytes[:])
if !strings.Contains(respStr, "Your identity was confirmed and propagated to Snowflake Go.\nYou can close this window now and go back where you started from.") {
t.Fatalf("failed to build response")
}
}
func postAuthExternalBrowserError(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{}, errors.New("failed to get SAML response")
}
func postAuthExternalBrowserFail(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{
Success: false,
Message: "external browser auth failed",
}, nil
}
func postAuthExternalBrowserFailWithCode(_ context.Context, _ *snowflakeRestful, _ map[string]string, _ []byte, _ time.Duration) (*authResponse, error) {
return &authResponse{
Success: false,
Message: "failed to connect to db",
Code: "260008",
}, nil
}
func TestUnitAuthenticateByExternalBrowser(t *testing.T) {
authenticator := "externalbrowser"
application := "testapp"
account := "testaccount"
user := "u"
password := "p"
timeout := defaultExternalBrowserTimeout
sr := &snowflakeRestful{
Protocol: "https",
Host: "abc.com",
Port: 443,
FuncPostAuthSAML: postAuthExternalBrowserError,
TokenAccessor: getSimpleTokenAccessor(),
}
_, _, err := authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout, ConfigBoolTrue)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPostAuthSAML = postAuthExternalBrowserFail
_, _, err = authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout, ConfigBoolTrue)
if err == nil {
t.Fatal("should have failed.")
}
sr.FuncPostAuthSAML = postAuthExternalBrowserFailWithCode
_, _, err = authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout, ConfigBoolTrue)
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 != ErrCodeFailedToConnect {
t.Fatalf("unexpected error code. expected: %v, got: %v", ErrCodeFailedToConnect, driverErr.Number)
}
}
func TestAuthenticationTimeout(t *testing.T) {
authenticator := "externalbrowser"
application := "testapp"
account := "testaccount"
user := "u"
password := "p"
timeout := 0 * time.Second
sr := &snowflakeRestful{
Protocol: "https",
Host: "abc.com",
Port: 443,
FuncPostAuthSAML: postAuthExternalBrowserError,
TokenAccessor: getSimpleTokenAccessor(),
}
_, _, err := authenticateByExternalBrowser(context.Background(), sr, authenticator, application, account, user, password, timeout, ConfigBoolTrue)
if err.Error() != "authentication timed out" {
t.Fatal("should have timed out")
}
}
func Test_createLocalTCPListener(t *testing.T) {
listener, err := createLocalTCPListener()
if err != nil {
t.Fatalf("createLocalTCPListener() failed: %v", err)
}
if listener == nil {
t.Fatal("createLocalTCPListener() returned nil listener")
}
// Close the listener after the test.
defer listener.Close()
}
func TestUnitGetLoginURL(t *testing.T) {
expectedScheme := "https"
expectedHost := "abc.com:443"
user := "u"
callbackPort := 123
sr := &snowflakeRestful{
Protocol: "https",
Host: "abc.com",
Port: 443,
TokenAccessor: getSimpleTokenAccessor(),
}
loginURL, proofKey, err := getLoginURL(sr, user, callbackPort)
assertNilF(t, err, "failed to get login URL")
assertNotNilF(t, len(proofKey), "proofKey should be non-empty string")
urlPtr, err := url.Parse(loginURL)
assertNilF(t, err, "failed to parse the login URL")
assertEqualF(t, urlPtr.Scheme, expectedScheme)
assertEqualF(t, urlPtr.Host, expectedHost)
assertEqualF(t, urlPtr.Path, consoleLoginRequestPath)
assertStringContainsF(t, urlPtr.RawQuery, "login_name")
assertStringContainsF(t, urlPtr.RawQuery, "browser_mode_redirect_port")
assertStringContainsF(t, urlPtr.RawQuery, "proof_key")
}