-
Notifications
You must be signed in to change notification settings - Fork 73
/
client_test.go
253 lines (232 loc) · 7.4 KB
/
client_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
241
242
243
244
245
246
247
248
249
250
251
252
253
package tesla
import (
"bytes"
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestClientSpec(t *testing.T) {
ts := serveHTTP(t)
defer ts.Close()
previousAuthURL := AuthURL
previousURL := BaseURL
AuthURL = ts.URL + "/oauth/token"
BaseURL = ts.URL + "/api/1"
auth := &Auth{
GrantType: "password",
ClientID: "abc123",
ClientSecret: "def456",
Email: "elon@tesla.com",
Password: "go",
}
client, err := NewClient(auth)
Convey("Should set the HTTP headers", t, func() {
req, _ := http.NewRequest("GET", "http://foo.com", nil)
client.setHeaders(req)
So(req.Header.Get("Authorization"), ShouldEqual, "Bearer ghi789")
So(req.Header.Get("Accept"), ShouldEqual, "application/json")
So(req.Header.Get("Content-Type"), ShouldEqual, "application/json")
})
Convey("Should login and get an access token", t, func() {
So(err, ShouldBeNil)
So(client.Token.AccessToken, ShouldEqual, "ghi789")
})
AuthURL = previousAuthURL
BaseURL = previousURL
}
func TestTokenExpiredSpec(t *testing.T) {
// Expired token
expiredToken := &Token{
AccessToken: "foo",
TokenType: "bar",
ExpiresIn: 1,
Expires: 0,
}
validToken := &Token{
AccessToken: "foo",
TokenType: "bar",
ExpiresIn: 1,
Expires: 9999999999999,
}
client := &Client{
Token: expiredToken,
}
Convey("Should have an expired token", t, func() {
So(client.TokenExpired(), ShouldBeTrue)
})
client.Token = validToken
Convey("Should have a valid token", t, func() {
So(client.TokenExpired(), ShouldBeFalse)
})
}
func TestClientWithTokenSpec(t *testing.T) {
ts := serveHTTP(t)
defer ts.Close()
previousAuthURL := AuthURL
previousURL := BaseURL
AuthURL = ts.URL + "/oauth/token"
BaseURL = ts.URL + "/api/1"
auth := &Auth{
GrantType: "password",
ClientID: "abc123",
ClientSecret: "def456",
Email: "elon@tesla.com",
Password: "go",
}
validToken := &Token{
AccessToken: "foo",
TokenType: "bar",
ExpiresIn: 1,
Expires: 99999999999,
}
client, err := NewClientWithToken(auth, validToken)
Convey("Should login with a valid access token", t, func() {
So(err, ShouldBeNil)
So(client.Token.AccessToken, ShouldEqual, "foo")
})
AuthURL = previousAuthURL
BaseURL = previousURL
}
func serveHTTP(t *testing.T) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
body, _ := ioutil.ReadAll(req.Body)
defer req.Body.Close()
switch req.URL.String() {
case "/oauth/token":
checkHeaders(t, req)
Convey("Request body should be set correctly", t, func() {
auth := &Auth{}
json.Unmarshal(body, auth)
So(auth.ClientID, ShouldEqual, "abc123")
So(auth.ClientSecret, ShouldEqual, "def456")
So(auth.Email, ShouldEqual, "elon@tesla.com")
So(auth.Password, ShouldEqual, "go")
So(auth.URL, ShouldEqual, BaseURL)
So(auth.StreamingURL, ShouldEqual, StreamingURL)
})
w.WriteHeader(200)
w.Write([]byte("{\"access_token\": \"ghi789\"}"))
case "/api/1/vehicles":
checkHeaders(t, req)
w.WriteHeader(200)
w.Write([]byte(VehiclesJSON))
case "/api/1/vehicles/1234/mobile_enabled":
checkHeaders(t, req)
w.WriteHeader(200)
w.Write([]byte(TrueJSON))
case "/api/1/vehicles/1234/data_request/charge_state":
checkHeaders(t, req)
w.WriteHeader(200)
w.Write([]byte(ChargeStateJSON))
case "/api/1/vehicles/1234/data_request/climate_state":
w.WriteHeader(200)
w.Write([]byte(ClimateStateJSON))
case "/api/1/vehicles/1234/data_request/drive_state":
checkHeaders(t, req)
w.WriteHeader(200)
w.Write([]byte(DriveStateJSON))
case "/api/1/vehicles/1234/data_request/gui_settings":
checkHeaders(t, req)
w.WriteHeader(200)
w.Write([]byte(GuiSettingsJSON))
case "/api/1/vehicles/1234/data_request/vehicle_state":
checkHeaders(t, req)
w.WriteHeader(200)
w.Write([]byte(VehicleStateJSON))
case "/api/1/vehicles/1234/wake_up":
checkHeaders(t, req)
w.WriteHeader(200)
w.Write([]byte(WakeupResponseJSON))
case "/api/1/vehicles/1234/command/set_charge_limit":
w.WriteHeader(200)
Convey("Should receive a set charge limit request", t, func() {
So(string(body), ShouldEqual, `{"percent": 50}`)
})
case "/api/1/vehicles/1234/command/charge_standard":
checkHeaders(t, req)
w.WriteHeader(200)
w.Write([]byte(ChargeAlreadySetJSON))
case "/api/1/vehicles/1234/command/charge_start":
checkHeaders(t, req)
w.WriteHeader(200)
w.Write([]byte(ChargedJSON))
case "/api/1/vehicles/1234/command/charge_stop",
"/api/1/vehicles/1234/command/charge_max_range",
"/api/1/vehicles/1234/command/charge_port_door_open",
"/api/1/vehicles/1234/command/flash_lights",
"/api/1/vehicles/1234/command/honk_horn",
"/api/1/vehicles/1234/command/auto_conditioning_start",
"/api/1/vehicles/1234/command/auto_conditioning_stop",
"/api/1/vehicles/1234/command/door_unlock",
"/api/1/vehicles/1234/command/door_lock",
"/api/1/vehicles/1234/command/reset_valet_pin",
"/api/1/vehicles/1234/command/set_temps?driver_temp=72&passenger_temp=72",
"/api/1/vehicles/1234/command/remote_start_drive?password=foo":
checkHeaders(t, req)
w.WriteHeader(200)
w.Write([]byte(CommandResponseJSON))
case "/stream/123/?values=speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state,range,est_range,heading":
w.WriteHeader(200)
events := StreamEventString + "\n" +
StreamEventString + "\n" +
BadStreamEventString + "\n"
b := bytes.NewBufferString(events)
b.WriteTo(w)
case "/api/1/vehicles/1234/command/autopark_request":
w.WriteHeader(200)
Convey("Auto park request should have appropriate body", t, func() {
autoParkRequest := &AutoParkRequest{}
err := json.Unmarshal(body, autoParkRequest)
So(err, ShouldBeNil)
So(autoParkRequest.Action, shouldBeValidAutoparkCommand)
So(autoParkRequest.VehicleID, ShouldEqual, 456)
So(autoParkRequest.Lat, ShouldEqual, 35.1)
So(autoParkRequest.Lon, ShouldEqual, 20.2)
})
case "/api/1/vehicles/1234/command/trigger_homelink":
w.WriteHeader(200)
Convey("Auto park request should have appropriate body", t, func() {
autoParkRequest := &AutoParkRequest{}
err := json.Unmarshal(body, autoParkRequest)
So(err, ShouldBeNil)
So(autoParkRequest.Lat, ShouldEqual, 35.1)
So(autoParkRequest.Lon, ShouldEqual, 20.2)
})
case "/api/1/vehicles/1234/command/sun_roof_control":
w.WriteHeader(200)
Convey("Should set the Pano roof appropriately", t, func() {
passed := false
strBody := string(body)
if strBody == `{"state": "vent", "percent":0}` {
passed = true
}
if strBody == `{"state": "open", "percent":0}` {
passed = true
}
if strBody == `{"state": "move", "percent":50}` {
passed = true
}
if strBody == `{"state": "close", "percent":0}` {
passed = true
}
So(passed, ShouldBeTrue)
})
}
}))
}
func checkHeaders(t *testing.T, req *http.Request) {
Convey("HTTP headers should be present", t, func() {
So(req.Header["Accept"][0], ShouldEqual, "application/json")
So(req.Header["Content-Type"][0], ShouldEqual, "application/json")
})
}
func shouldBeValidAutoparkCommand(actual interface{}, expected ...interface{}) string {
if actual == "start_forward" || actual == "start_reverse" || actual == "abort" {
return ""
} else {
return "The Autopark command should pass start_forward, start_reverse or abort"
}
}