-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver_internal_test.go
148 lines (140 loc) · 3.96 KB
/
driver_internal_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
package awql
import (
"database/sql/driver"
"testing"
"time"
)
// TestAwqlDriver_Open tests the method named Open on Driver struct.
func TestAwqlDriver_Open(t *testing.T) {
var driverTests = []struct {
dsn string
conn driver.Conn
err error
}{
// Errors.
// 0
{"", nil, driver.ErrBadConn},
{"123-456-7890", nil, driver.ErrBadConn},
{"123-456-7890|dEve1op3er7okeN|ya29.AcC3s57okeN|Oops", nil, driver.ErrBadConn},
{"123-456-7890:v201607||ya29.AcC3s57okeN", nil, ErrDevToken},
{"|dEve1op3er7okeN|ya29.AcC3s57okeN", nil, ErrAdwordsID},
// 5
{"123-456-7890:v201607|dEve1op3er7okeN|", nil, ErrBadToken},
{"123-456-7890|dEve1op3er7okeN||c1ien753cr37|1/R3Fr35h-70k3n", nil, ErrBadToken},
// Ok.
{
"123-456-7890|dEve1op3er7okeN",
&Conn{adwordsID: "123-456-7890", developerToken: "dEve1op3er7okeN"},
nil,
},
{
"123-456-7890|dEve1op3er7okeN|ya29.AcC3s57okeN",
&Conn{
adwordsID: "123-456-7890", developerToken: "dEve1op3er7okeN",
oAuth: &Auth{AuthKey{}, AuthToken{AccessToken: "ya29.AcC3s57okeN"}},
},
nil,
},
{
"123-456-7890:v201607|dEve1op3er7okeN|ya29.AcC3s57okeN",
&Conn{
adwordsID: "123-456-7890", developerToken: "dEve1op3er7okeN",
oAuth: &Auth{AuthKey{}, AuthToken{AccessToken: "ya29.AcC3s57okeN"}},
opts: &Opts{Version: "v201607"},
},
nil,
},
// 10
{
"123-456-7890|dEve1op3er7okeN|1234567890-c1i3n7iD.apps.googleusercontent.com|c1ien753cr37|1/R3Fr35h-70k3n",
&Conn{
adwordsID: "123-456-7890", developerToken: "dEve1op3er7okeN",
oAuth: &Auth{
AuthKey{
ClientID: "1234567890-c1i3n7iD.apps.googleusercontent.com",
ClientSecret: "c1ien753cr37", RefreshToken: "1/R3Fr35h-70k3n",
},
AuthToken{
AccessToken: "ya29.AcC3s57okeN",
TokenType: "Bearer",
Expiry: time.Now().Add(tokenExpiryDuration),
},
},
},
nil,
},
}
d := &Driver{}
for _, dt := range driverTests {
if _, err := d.Open(dt.dsn); err == nil {
if dt.err != nil {
t.Errorf("Expected error %v, received no error with %v", dt.err, dt.dsn)
}
} else if dt.err == nil {
t.Errorf("Expected no error with %s, received %v", dt.dsn, err)
} else if err.Error() != dt.err.Error() {
t.Errorf("Expected error %v with %s, received %v", dt.err, dt.dsn, err)
}
}
}
var authTests = []struct {
token *Auth // in
str string // out
isValid, isSet bool
}{
{
&Auth{
AuthKey{},
AuthToken{TokenType: "Bearer", AccessToken: "ya29.AcC3s57okeN"},
},
"Bearer ya29.AcC3s57okeN", false, false,
},
{
&Auth{
AuthKey{},
AuthToken{TokenType: "Bearer", AccessToken: "ya29.A", Expiry: time.Now()},
},
"Bearer ya29.A", false, false,
},
{
&Auth{
AuthKey{},
AuthToken{TokenType: "Bearer", AccessToken: "ya29.B", Expiry: time.Now().Add(tokenExpiryDuration)},
},
"Bearer ya29.B", true, false,
},
{
&Auth{
AuthKey{
ClientID: "1234567890-c1i3n7iD.apps.googleusercontent.com",
ClientSecret: "c1ien753cr37", RefreshToken: "1/R3Fr35h-70k3n",
},
AuthToken{TokenType: "Bearer", AccessToken: "ya29.AcC3s57okeN", Expiry: time.Now().Add(tokenExpiryDuration)},
},
"Bearer ya29.AcC3s57okeN", true, true,
},
}
// TestAwqlAuth_Valid test the Auth method named Valid.
func TestAwqlAuth_IsSet(t *testing.T) {
for _, a := range authTests {
if a.token.IsSet() != a.isSet {
t.Errorf("Expected %v for the check of setting of the access token %v, received %v", a.isSet, a.token, a.token.IsSet())
}
}
}
// TestAwqlAuth_String test the Auth method named String.
func TestAwqlAuth_String(t *testing.T) {
for _, a := range authTests {
if a.token.String() != a.str {
t.Errorf("Expected %v as access token, received %v", a.str, a.token.String())
}
}
}
// TestAwqlAuth_Valid test the Auth method named Valid.
func TestAwqlAuth_Valid(t *testing.T) {
for _, a := range authTests {
if a.token.Valid() != a.isValid {
t.Errorf("Expected %v as access token validity for %v, received %v", a.isValid, a.token, a.token.Valid())
}
}
}