-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhandlers_test.go
More file actions
238 lines (206 loc) · 6.38 KB
/
handlers_test.go
File metadata and controls
238 lines (206 loc) · 6.38 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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"unsafe"
"context"
"github.com/emicklei/go-restful"
"google.golang.org/appengine/datastore"
)
type fakeKey struct {
kind string
stringID string
intID int64
parent *datastore.Key
appID string
namespace string
}
func makeKey(kind, stringID string, intID int64, parent *datastore.Key) *datastore.Key {
fk := fakeKey{
kind: kind,
stringID: stringID,
intID: intID,
parent: parent,
appID: "test-app",
namespace: "",
}
return (*datastore.Key)(unsafe.Pointer(&fk))
}
func TestInsertGChart(t *testing.T) {
os.Setenv("APPLICATION_ID", "test-app")
os.Setenv("Basic_Auth", "secret")
// Setup Mock Store
mockStore := &MockStore{
PutFunc: func(ctx context.Context, key *datastore.Key, src interface{}) (*datastore.Key, error) {
return makeKey("gchartentity", "", 123, nil), nil
},
NewQueryFunc: func(kind string) Query {
return &MockQuery{
CountFunc: func(ctx context.Context) (int, error) {
return 0, nil
},
GetAllFunc: func(ctx context.Context, dst interface{}) ([]*datastore.Key, error) {
return nil, fmt.Errorf("mock error")
},
}
},
NewKeyFunc: func(ctx context.Context, kind, stringID string, intID int64, parent *datastore.Key) *datastore.Key {
return makeKey(kind, stringID, intID, parent)
},
NewIncompleteKeyFunc: func(ctx context.Context, kind string, parent *datastore.Key) *datastore.Key {
return makeKey(kind, "", 0, parent)
},
CacheGetFunc: func(ctx context.Context, key string, dst interface{}) error {
return fmt.Errorf("mock error")
},
}
DB = mockStore
// Setup Request
chart := GChartPostAPIv1{
Header: CommonAPIHeaderV1{
Name: "Test Chart",
},
ChartSport: "Bike",
}
body, _ := json.Marshal(chart)
req, _ := http.NewRequest("POST", "/v1/gchart/", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Basic secret")
rw := httptest.NewRecorder()
// Dispatch through DefaultContainer
restful.DefaultContainer.ServeHTTP(rw, req)
// Check Response
if rw.Code != http.StatusCreated {
t.Errorf("Expected 201 Created, got %d. Body: %s", rw.Code, rw.Body.String())
}
}
func TestGetGChartById(t *testing.T) {
os.Setenv("APPLICATION_ID", "test-app")
os.Setenv("Basic_Auth", "secret")
// Setup Mock Store
mockStore := &MockStore{
GetFunc: func(ctx context.Context, key *datastore.Key, dst interface{}) error {
// Fill dst with dummy data
if chart, ok := dst.(*GChartEntity); ok {
chart.ChartSport = "Run"
return nil
}
return datastore.ErrNoSuchEntity
},
NewKeyFunc: func(ctx context.Context, kind, stringID string, intID int64, parent *datastore.Key) *datastore.Key {
return makeKey(kind, stringID, intID, parent)
},
NewQueryFunc: func(kind string) Query {
return &MockQuery{
GetAllFunc: func(ctx context.Context, dst interface{}) ([]*datastore.Key, error) {
return nil, fmt.Errorf("mock error")
},
}
},
CacheGetFunc: func(ctx context.Context, key string, dst interface{}) error {
return fmt.Errorf("mock error")
},
}
DB = mockStore
// Setup Request
req, _ := http.NewRequest("GET", "/v1/gchart/123", nil)
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Basic secret")
rw := httptest.NewRecorder()
// Dispatch through DefaultContainer
restful.DefaultContainer.ServeHTTP(rw, req)
// Check Response
if rw.Code != http.StatusOK {
t.Errorf("Expected 200 OK, got %d. Body: %s", rw.Code, rw.Body.String())
}
var respChart GChartGetAPIv1
json.Unmarshal(rw.Body.Bytes(), &respChart)
if respChart.ChartSport != "Run" {
t.Errorf("Expected ChartSport 'Run', got '%s'", respChart.ChartSport)
}
}
func TestUpsertTelemetry(t *testing.T) {
os.Setenv("APPLICATION_ID", "test-app")
os.Setenv("Basic_Auth", "secret")
// Setup Mock Store
mockStore := &MockStore{
PutFunc: func(ctx context.Context, key *datastore.Key, src interface{}) (*datastore.Key, error) {
return makeKey("telemetryentity", "test-key", 0, nil), nil
},
GetFunc: func(ctx context.Context, key *datastore.Key, dst interface{}) error {
return datastore.ErrNoSuchEntity
},
NewKeyFunc: func(ctx context.Context, kind, stringID string, intID int64, parent *datastore.Key) *datastore.Key {
return makeKey(kind, stringID, intID, parent)
},
}
DB = mockStore
// Setup Request
telemetry := TelemetryEntityPostAPIv1{
UserKey: "test-key",
OS: "Linux",
GCVersion: "3.6",
Increment: 25,
LastChange: time.Now().Format(dateTimeLayout),
}
body, _ := json.Marshal(telemetry)
req, _ := http.NewRequest("PUT", "/v1/telemetry", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Basic secret")
rw := httptest.NewRecorder()
// Dispatch through DefaultContainer
restful.DefaultContainer.ServeHTTP(rw, req)
// Check Response
if rw.Code != http.StatusCreated {
t.Errorf("Expected 201 Created, got %d. Body: %s", rw.Code, rw.Body.String())
}
}
func TestGetVersion(t *testing.T) {
os.Setenv("APPLICATION_ID", "test-app")
os.Setenv("Basic_Auth", "secret")
// Setup Mock Store
mockStore := &MockStore{
NewQueryFunc: func(kind string) Query {
return &MockQuery{
GetAllFunc: func(ctx context.Context, dst interface{}) ([]*datastore.Key, error) {
// Populate dst with mock data
versions := dst.(*[]VersionEntity)
*versions = append(*versions, VersionEntity{
Version: 3600,
Type: 10,
URL: "http://example.com",
Text: "Release",
VersionText: "3.6",
})
return []*datastore.Key{makeKey("versionentity", "", 1, nil)}, nil
},
}
},
}
DB = mockStore
// Setup Request
req, _ := http.NewRequest("GET", "/v1/version?version=3500", nil)
req.Header.Set("Authorization", "Basic secret")
rw := httptest.NewRecorder()
// Dispatch through DefaultContainer
restful.DefaultContainer.ServeHTTP(rw, req)
// Check Response
if rw.Code != http.StatusOK {
t.Errorf("Expected 200 OK, got %d. Body: %s", rw.Code, rw.Body.String())
}
var versions []VersionEntityGetAPIv1
json.Unmarshal(rw.Body.Bytes(), &versions)
if len(versions) != 1 {
t.Errorf("Expected 1 version, got %d", len(versions))
}
if versions[0].Version != 3600 {
t.Errorf("Expected Version 3600, got %d", versions[0].Version)
}
}