This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apidAnalytics_suite_test.go
301 lines (276 loc) · 7.15 KB
/
apidAnalytics_suite_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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apidAnalytics
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"encoding/json"
"github.com/apid/apid-core"
"github.com/apid/apid-core/factory"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
)
var (
testTempDir string
testServer *httptest.Server
)
func TestApidAnalytics(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "ApidAnalytics Suite")
}
var _ = BeforeSuite(func() {
apid.Initialize(factory.DefaultServicesFactory())
config = apid.Config()
var err error
testTempDir, err = ioutil.TempDir("", "api_test")
Expect(err).NotTo(HaveOccurred())
config.Set("local_storage_path", testTempDir)
config.Set("data_path", testTempDir)
config.Set("apigeesync_apid_instance_id", "abcdefgh-ijkl-mnop-qrst-uvwxyz123456") // dummy value
db, err := apid.Data().DB()
Expect(err).NotTo(HaveOccurred())
initDb(db)
config.Set(uapServerBase, "http://localhost:9000") // dummy value
apid.InitializePlugins("")
// Analytics POST API
router := apid.API().Router()
router.HandleFunc(analyticsBasePath+"/{bundle_scope_uuid}", func(w http.ResponseWriter, req *http.Request) {
saveAnalyticsRecord(w, req)
}).Methods("POST")
// Mock UAP collection endpoint
router.HandleFunc("/analytics", func(w http.ResponseWriter, req *http.Request) {
mockUAPCollection(w, req)
}).Methods("GET")
// fake AWS S3
router.HandleFunc("/upload", func(w http.ResponseWriter, req *http.Request) {
mockFinalDatastore(w, req)
}).Methods("PUT")
testServer = httptest.NewServer(router)
// ser serverbased to local so that responses can be mocked
config.Set(uapServerBase, testServer.URL)
})
func mockUAPCollection(w http.ResponseWriter, req *http.Request) {
if req.Header.Get("Authorization") == "" {
w.WriteHeader(http.StatusUnauthorized)
} else {
if req.URL.Query().Get("tenant") == "testorg~testenv" {
w.WriteHeader(http.StatusOK)
body := make(map[string]interface{})
body["url"] = testServer.URL + "/upload?awskey=xxxx"
bytes, _ := json.Marshal(body)
w.Write(bytes)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
}
func mockFinalDatastore(w http.ResponseWriter, req *http.Request) {
status := req.URL.Query().Get("expected_status")
switch status {
case "ok":
w.WriteHeader(http.StatusOK)
case "serverError":
w.WriteHeader(http.StatusInternalServerError)
case "forbidden":
w.WriteHeader(http.StatusForbidden)
default:
w.WriteHeader(http.StatusOK)
}
}
func initDb(db apid.DB) {
createApidClusterTables(db)
createTables(db)
insertTestData(db)
setDB(db)
}
func createTables(db apid.DB) {
tx, err := db.Begin()
Expect(err).Should(Succeed())
defer tx.Rollback()
_, err = tx.Exec(`
CREATE TABLE IF NOT EXISTS kms_api_product (
id text,
tenant_id text,
name text,
display_name text,
description text,
api_resources text[],
approval_type text,
_change_selector text,
proxies text[],
environments text[],
quota text,
quota_time_unit text,
quota_interval int,
created_at int64,
created_by text,
updated_at int64,
updated_by text,
PRIMARY KEY (tenant_id, id));
CREATE TABLE IF NOT EXISTS kms_developer (
id text,
tenant_id text,
username text,
first_name text,
last_name text,
password text,
email text,
status text,
encrypted_password text,
salt text,
_change_selector text,
created_at int64,
created_by text,
updated_at int64,
updated_by text,
PRIMARY KEY (tenant_id, id)
);
CREATE TABLE IF NOT EXISTS kms_app (
id text,
tenant_id text,
name text,
display_name text,
access_type text,
callback_url text,
status text,
app_family text,
company_id text,
developer_id text,
type int,
created_at int64,
created_by text,
updated_at int64,
updated_by text,
_change_selector text,
PRIMARY KEY (tenant_id, id)
);
CREATE TABLE IF NOT EXISTS kms_app_credential_apiproduct_mapper (
tenant_id text,
appcred_id text,
app_id text,
apiprdt_id text,
_change_selector text,
status text,
PRIMARY KEY (appcred_id, app_id, apiprdt_id,tenant_id)
);
`)
Expect(err).Should(Succeed())
err = tx.Commit()
Expect(err).Should(Succeed())
}
func createApidClusterTables(db apid.DB) {
txn, err := db.Begin()
Expect(err).Should(Succeed())
defer txn.Rollback()
_, err = txn.Exec(`
CREATE TABLE edgex_apid_cluster (
id text,
instance_id text,
name text,
description text,
umbrella_org_app_name text,
created int64,
created_by text,
updated int64,
updated_by text,
_change_selector text,
snapshotInfo text,
lastSequence text,
PRIMARY KEY (id)
);
CREATE TABLE edgex_data_scope (
id text,
apid_cluster_id text,
scope text,
org text,
env text,
created int64,
created_by text,
updated int64,
updated_by text,
_change_selector text,
PRIMARY KEY (id)
);
`)
Expect(err).Should(Succeed())
err = txn.Commit()
Expect(err).Should(Succeed())
}
func insertTestData(db apid.DB) {
txn, err := db.Begin()
Expect(err).Should(Succeed())
defer txn.Rollback()
_, err = txn.Exec("INSERT INTO kms_app_credential_apiproduct_mapper (tenant_id,"+
" appcred_id, app_id, apiprdt_id, status, _change_selector) "+
"VALUES"+
"($1,$2,$3,$4,$5,$6)",
"tenantid",
"testapikey",
"testappid",
"testproductid",
"APPROVED",
"12345",
)
Expect(err).Should(Succeed())
_, err = txn.Exec("INSERT INTO kms_app (id, tenant_id, name, developer_id) "+
"VALUES"+
"($1,$2,$3,$4)",
"testappid",
"tenantid",
"testapp",
"testdeveloperid",
)
Expect(err).Should(Succeed())
_, err = txn.Exec("INSERT INTO kms_api_product (id, tenant_id, name) "+
"VALUES"+
"($1,$2,$3)",
"testproductid",
"tenantid",
"testproduct",
)
Expect(err).Should(Succeed())
_, err = txn.Exec("INSERT INTO kms_developer (id, tenant_id, username, email) "+
"VALUES"+
"($1,$2,$3,$4)",
"testdeveloperid",
"tenantid",
"testdeveloper",
)
Expect(err).Should(Succeed())
_, err = txn.Exec("INSERT INTO edgex_data_scope (id, _change_selector, "+
"apid_cluster_id, scope, org, env) "+
"VALUES"+
"($1,$2,$3,$4,$5,$6)",
"testid",
"some_change_selector",
"some_cluster_id",
"tenantid",
"testorg",
"testenv",
)
Expect(err).Should(Succeed())
err = txn.Commit()
Expect(err).Should(Succeed())
}
var _ = AfterSuite(func() {
apid.Events().Close()
if testServer != nil {
testServer.Close()
}
os.RemoveAll(testTempDir)
})