/
ProductionConfigTests.js
601 lines (553 loc) · 21.4 KB
/
ProductionConfigTests.js
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/**
GPII Production Config tests
Requirements:
* an internet connection
* a cloud based flow manager running at `http://flowmanager.gpii.net` containing at least the MikelVargas
preferences
---
Copyright 2015 Raising the Floor - International
Copyright 2018 OCAD University
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
The research leading to these results has received funding from the European Union's
Seventh Framework Programme (FP7/2007-2013) under grant agreement no. 289016.
You may obtain a copy of the License at
https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
"use strict";
var fs = require("fs"),
fluid = require("infusion"),
jqUnit = fluid.require("node-jqunit", require, "jqUnit"),
gpii = fluid.registerNamespace("gpii"),
kettle = fluid.registerNamespace("kettle");
fluid.registerNamespace("gpii.tests.productionConfigTesting");
fluid.require("%gpii-universal");
/*
* ========================================================================
* Testing of untrusted local config with the live cloud based flow manager
* ========================================================================
*/
require("./shared/DevelopmentTestDefs.js");
require("./shared/FlowManagerSettingsGetTestDefs.js");
require("./shared/FlowManagerSettingsPutTestDefs.js");
gpii.loadTestingSupport();
gpii.tests.productionConfigTesting.config = {
configName: "gpii.tests.untrusted.production.config",
configPath: "%gpii-universal/tests/configs"
};
gpii.tests.productionConfigTesting.getKeyOrPrefsFromFile = function (filePath, id) {
var dataArray = JSON.parse(fs.readFileSync(filePath, "utf-8"));
return fluid.find(dataArray, function (anEntry) {
if (anEntry._id === id) {
return anEntry;
}
}, null);
};
gpii.tests.productionConfigTesting.gpiiKeyNoPrefsSafe =
gpii.tests.productionConfigTesting.getKeyOrPrefsFromFile(
"./tests/data/dbData/gpiiKeys.json", "gpii_key_no_prefs_safe"
);
gpii.tests.productionConfigTesting.settingsUserKey =
gpii.tests.productionConfigTesting.getKeyOrPrefsFromFile(
"./build/tests/dbData/gpiiKeys.json", "os_gnome"
);
gpii.tests.productionConfigTesting.settingsUserPrefsSafe =
gpii.tests.productionConfigTesting.getKeyOrPrefsFromFile(
"./build/tests/dbData/prefsSafes.json", "prefsSafe-os_gnome"
);
gpii.tests.productionConfigTesting.carlaKey = "carla";
gpii.tests.productionConfigTesting.carlaTokenRequestPayload = {
"username": gpii.tests.productionConfigTesting.carlaKey,
"gpiiKey": gpii.tests.productionConfigTesting.carlaKey,
"password": "dummy",
"client_id": "pilot-computer",
"client_secret": "pilot-computer-secret",
"grant_type": "password"
};
gpii.tests.productionConfigTesting.device = {
OS: {
id: "linux"
},
solutions: [{
"id": "org.gnome.desktop.a11y.magnifier"
}]
};
gpii.tests.productionConfigTesting.prefsUpdate = {
"contexts": {
"gpii-default": {
"name": "Default preferences",
"preferences": {
"http://registry.gpii.net/common/setting1": 12
}
}
}
};
// Flowmanager tests for:
// /user/%gpiiKey/login and /user/%gpiiKey/logout (as defined in gpii.tests.development.testDefs),
// /health,
// /ready,
// and using the "carla" snapset to test that it cannot be modified:
// /access_token,
// /%gpiiKey/settings/%device
// /%gpiiKey/settings
gpii.tests.productionConfigTesting.testDefs = fluid.transform(gpii.tests.development.testDefs, function (testDefIn) {
var testDef = fluid.extend(true, {}, testDefIn, {
name: "Flow Manager production tests",
expect: 16,
config: gpii.tests.productionConfigTesting.config,
components: {
healthRequest: {
type: "kettle.test.request.http",
options: {
hostname: "flowmanager",
path: "/health",
method: "GET",
expectedStatusCode: 200,
expectedPayload: {"isHealthy": true}
}
},
readyRequest: {
type: "kettle.test.request.http",
options: {
hostname: "flowmanager",
path: "/ready",
method: "GET",
expectedStatusCode: 200,
expectedPayload: {"isReady": true}
}
},
accessTokenRequest: {
type: "kettle.test.request.http",
options: {
hostname: "flowmanager",
path: "/access_token",
method: "POST",
expectedStatusCode: 200
}
},
lifeCycleRequest: {
type: "kettle.test.request.http",
options: {
hostname: "flowmanager",
path: fluid.stringTemplate("/%gpiiKey/settings/%device", {
gpiiKey: gpii.tests.productionConfigTesting.carlaKey,
device: encodeURIComponent(JSON.stringify(
gpii.tests.productionConfigTesting.device
))}
),
headers: {
"Authorization": "Bearer token" // set at test run
},
method: "GET",
expectedStatusCode: 200
}
},
putSettingsRequestFailure: { // can't update snapset (readonly)
type: "kettle.test.request.http",
options: {
hostname: "flowmanager",
path: "/%gpiiKey/settings",
termMap: {
"gpiiKey": gpii.tests.productionConfigTesting.carlaKey
},
headers: {
"Authorization": "Bearer token" // set at test run
},
method: "PUT",
expectedStatusCode: 404,
expectedPayload: {
"isError": true,
"message": "Cannot update: GPII key \"" + gpii.tests.productionConfigTesting.carlaKey + "\" is a snapset while executing HTTP PUT on url http://preferences:9081/preferences/%gpiiKey?merge=%merge"
}
}
}
}
});
gpii.test.push(testDef.sequence, [
{
func: "{healthRequest}.send"
}, {
event: "{healthRequest}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testResponse"
}, {
func: "{readyRequest}.send"
}, {
event: "{readyRequest}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testResponse"
}, {
func: "{accessTokenRequest}.send",
args: [gpii.tests.productionConfigTesting.carlaTokenRequestPayload]
}, {
event: "{accessTokenRequest}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testAccessResponse"
}, {
func: "{lifeCycleRequest}.send",
args: [
null,
{
"headers": {
"Authorization": "{accessTokenRequest}.options.stashedAuth"
}
}
]
}, {
event: "{lifeCycleRequest}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testLifecycleResponse"
}, {
func: "{putSettingsRequestFailure}.send",
args: [
gpii.tests.productionConfigTesting.prefsUpdate,
{
"headers": {
"Authorization": "{accessTokenRequest}.options.stashedAuth"
}
}
]
}, {
event: "{putSettingsRequestFailure}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testResponse"
}
]);
return testDef;
});
gpii.tests.productionConfigTesting.testStatusCode = function (data, request) {
jqUnit.assertEquals(
"Checking status of " + request.options.path,
request.options.expectedStatusCode, request.nativeResponse.statusCode
);
};
gpii.tests.productionConfigTesting.testAddedToDatabase = function (data, request) {
var expected = request.options.expectedStatusCodes;
var actual = request.nativeResponse.statusCode;
jqUnit.assertNotEquals(
"Adding record to database using " + request.options.path +
", status: " + actual,
expected.indexOf(actual), -1
);
// Store the added record's id and rev
if (actual === 201 || actual === 200) {
request.options.result = JSON.parse(data);
fluid.log(request.options.result);
}
};
gpii.tests.productionConfigTesting.testResponse = function (data, request) {
gpii.tests.productionConfigTesting.testStatusCode(data, request);
jqUnit.assertDeepEq(
"Checking paylod of " + request.options.path,
request.options.expectedPayload, JSON.parse(data)
);
};
gpii.tests.productionConfigTesting.testAccessResponse = function (data, request) {
var token = JSON.parse(data);
var auth = "Bearer " + token.access_token;
request.options.stashedAuth = auth;
gpii.tests.productionConfigTesting.testStatusCode(data, request);
jqUnit.assertNotNull("Checking 'access_token'", token.access_token);
jqUnit.assertNotNull("Checking 'expiresIn'", token.expiresIn);
jqUnit.assertEquals("Checking 'token_type'", "Bearer", token.token_type);
};
gpii.tests.productionConfigTesting.testLifecycleResponse = function (data, request) {
gpii.tests.productionConfigTesting.testStatusCode(data, request);
var lifeCycle = JSON.parse(data);
jqUnit.assertEquals(
"Checking lifeCycle user '" + gpii.tests.productionConfigTesting.carlaKey + "'",
gpii.tests.productionConfigTesting.carlaKey,
lifeCycle.gpiiKey
);
// These checks based on
// https://github.com/GPII/universal/blob/master/documentation/FlowManager.md#get-lifecycle-instructions-from-cloud-based-flow-manager-get-gpiikeysettingsdevice
jqUnit.assertNotNull("Checking 'solutionsRegistryEntries'", lifeCycle.solutionsRegistryEntries);
jqUnit.assertNotNull("Checking 'matchMakerOutput'", lifeCycle.matchMakerOutput);
fluid.log(lifeCycle);
};
gpii.tests.productionConfigTesting.testGetForDeletion = function (data, request) {
var expected = request.options.expectedStatusCodes;
var actual = request.nativeResponse.statusCode;
jqUnit.assertNotEquals(
"Deleting record from database using " + request.options.path +
", status: " + actual,
expected.indexOf(actual), -1
);
// Mark and store the to-be-deleted record
if (actual === 201 || actual === 200) {
request.options.docToRemove = JSON.parse(data);
request.options.docToRemove._deleted = true;
}
fluid.log(request.options.docToRemove);
};
gpii.tests.productionConfigTesting.testBulkDelete = function (data, request) {
gpii.tests.productionConfigTesting.testStatusCode(data, request);
if (request.nativeResponse.statusCode === 201) {
request.options.docsToPurge = {};
fluid.each(JSON.parse(data), function (aDoc) {
fluid.set(request.options.docsToPurge, aDoc.id, [aDoc.rev]);
});
fluid.log(request.options.docsToPurge);
}
};
gpii.tests.productionConfigTesting.testPurge = function (data, request) {
fluid.log(JSON.parse(data));
gpii.tests.productionConfigTesting.testStatusCode(data, request);
};
kettle.test.bootstrapServer(gpii.tests.productionConfigTesting.testDefs);
// =======================================================
//
// Shared with SettingsGetTests.js and SettingsPutTests.js
// Tests that add 'user' preferences to the data base
gpii.tests.productionConfigTesting.addTestRecordsToDatabaseTests = {
testDef: {
name: "Flow manager production tests - add test GPII keys and PrefsSafe",
expect: 3,
config: gpii.tests.productionConfigTesting.config,
gradeNames: ["gpii.test.common.testCaseHolder"],
components: {
addSettingsUserKey: {
type: "kettle.test.request.http",
options: {
port: "5984",
hostname: "couchdb",
path: "/gpii",
method: "POST",
headers: {
"Content-Type": "application/json"
},
expectedStatusCodes: [201, 409] // created or already exists
}
},
addSettingsUserPrefsSafe: {
type: "kettle.test.request.http",
options: {
port: "5984",
hostname: "couchdb",
path: "/gpii",
method: "POST",
headers: {
"Content-Type": "application/json"
},
expectedStatusCodes: [201, 409] // created or already exists
}
},
addGpiiKeyNoPrefsSafe: {
type: "kettle.test.request.http",
options: {
port: "5984",
hostname: "couchdb",
path: "/gpii",
method: "POST",
headers: {
"Content-Type": "application/json"
},
expectedStatusCodes: [201, 409] // created or already exists
}
}
}
},
disruptions: [{
gradeName: "gpii.tests.productionConfigTesting.addTestRecordsToDatabase"
}]
};
fluid.defaults("gpii.tests.productionConfigTesting.addTestRecordsToDatabase", {
gradeNames: ["gpii.test.disruption"],
sequenceName: "gpii.tests.productionConfigTesting.addTestRecordsToDatabase.sequence"
});
gpii.tests.productionConfigTesting.addTestRecordsToDatabase.sequence = [
{
func: "{addSettingsUserKey}.send",
args: [
gpii.tests.productionConfigTesting.settingsUserKey,
{ port: "5984" }
]
}, {
event: "{addSettingsUserKey}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testAddedToDatabase"
}, {
func: "{addSettingsUserPrefsSafe}.send",
args: [
gpii.tests.productionConfigTesting.settingsUserPrefsSafe,
{ port: "5984" }
]
}, {
event: "{addSettingsUserPrefsSafe}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testAddedToDatabase"
},
{
func: "{addGpiiKeyNoPrefsSafe}.send",
args: [
gpii.tests.productionConfigTesting.gpiiKeyNoPrefsSafe,
{ port: "5984" }
]
},
{
event: "{addGpiiKeyNoPrefsSafe}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testAddedToDatabase"
}
];
// Tests that delete test user preferences from the data base
gpii.tests.productionConfigTesting.deleteTestRecordsFromDatabaseTests = {
testDef: {
name: "Flow manager production tests - delete test GPII keys and PrefsSafe",
expect: 4,
config: gpii.tests.productionConfigTesting.config,
gradeNames: ["gpii.test.common.testCaseHolder"],
components: {
getSettingsUserKey: {
type: "kettle.test.request.http",
options: {
port: "5984",
hostname: "couchdb",
path: "/gpii/os_gnome",
method: "GET",
expectedStatusCodes: [200],
docToRemove: null // set by successful request.
}
},
getSettingsUserPrefsSafe: {
type: "kettle.test.request.http",
options: {
port: "5984",
hostname: "couchdb",
path: "/gpii/prefsSafe-os_gnome",
method: "GET",
expectedStatusCodes: [200],
docToRemove: null // set by successful request.
}
},
getChromeAndFirefoxKey: {
type: "kettle.test.request.http",
options: {
port: "5984",
hostname: "couchdb",
path: "/gpii/gpii_key_no_prefs_safe",
method: "GET",
expectedStatusCodes: [200],
docToRemove: null // set by successful request.
}
},
deleteInBulk: {
type: "kettle.test.request.http",
options: {
port: "5984",
hostname: "couchdb",
path: "/gpii/_bulk_docs",
method: "POST",
expectedStatusCode: 201
}
}
}
},
disruptions: [{
gradeName: "gpii.tests.productionConfigTesting.deleteTestRecordsFromDatabase"
}]
};
fluid.defaults("gpii.tests.productionConfigTesting.deleteTestRecordsFromDatabase", {
gradeNames: ["gpii.test.disruption"],
sequenceName: "gpii.tests.productionConfigTesting.deleteTestRecordsFromDatabase.sequence"
});
gpii.tests.productionConfigTesting.deleteTestRecordsFromDatabase.sequence = [
{
func: "{getSettingsUserKey}.send",
args: [null, { port: "5984" }]
}, {
event: "{getSettingsUserKey}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testGetForDeletion"
}, {
func: "{getSettingsUserPrefsSafe}.send",
args: [null, { port: "5984" }]
}, {
event: "{getSettingsUserPrefsSafe}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testGetForDeletion"
}, {
func: "{getChromeAndFirefoxKey}.send",
args: [null, { port: "5984" }]
}, {
event: "{getChromeAndFirefoxKey}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testGetForDeletion"
}, {
func: "{deleteInBulk}.send",
args: [
{"docs": [
"{getSettingsUserKey}.options.docToRemove",
"{getSettingsUserPrefsSafe}.options.docToRemove",
"{getChromeAndFirefoxKey}.options.docToRemove"
]},
{ port: "5984" }
]
}, {
event: "{deleteInBulk}.events.onComplete",
listener: "gpii.tests.productionConfigTesting.testStatusCode"
}
];
// GET /settings tests
fluid.defaults("gpii.tests.productionConfigTesting.settingsGet.testCaseHolder", {
gradeNames: ["gpii.test.cloudBased.oauth2.testCaseHolder"],
productionHostConfig: {
hostname: "flowmanager",
port: 9082
},
distributeOptions: {
"accessTokenRequest.hostConfig": {
source: "{that}.options.productionHostConfig",
target: "{that accessTokenRequest}.options"
},
"accessTokenRequest_settings.hostConfig": {
source: "{that}.options.productionHostConfig",
target: "{that accessTokenRequest_settings}.options"
},
"settingsRequest.hostConfig": {
source: "{that}.options.productionHostConfig",
target: "{that settingsRequest}.options"
}
}
});
// Add 'user' prefs to the database at the beginnning of the "get" settings test
// definitions.
gpii.tests.cloud.oauth2.settingsGet.disruptedTests.unshift(
gpii.tests.productionConfigTesting.addTestRecordsToDatabaseTests
);
fluid.each(gpii.tests.cloud.oauth2.settingsGet.disruptedTests, function (oneTest) {
gpii.test.cloudBased.oauth2.bootstrapDisruptedTest(
oneTest.testDef,
{},
oneTest.disruptions,
gpii.tests.productionConfigTesting.config,
"gpii.tests.productionConfigTesting.settingsGet.testCaseHolder"
);
});
// PUT /settings tests
fluid.defaults("gpii.tests.productionConfigTesting.settingsPut.testCaseHolder", {
gradeNames: ["gpii.test.cloudBased.oauth2.testCaseHolder"],
productionHostConfig: {
hostname: "flowmanager",
port: 9082
},
distributeOptions: {
"accessTokenRequest.hostConfig": {
source: "{that}.options.productionHostConfig",
target: "{that accessTokenRequest}.options"
},
"settingsPutRequest.hostConfig": {
source: "{that}.options.productionHostConfig",
target: "{that settingsPutRequest}.options"
}
}
});
// Add 'user' prefs to the database at the beginnning of the "put" settings test
// definitions. Note that if already present, these additions are a "no-op"
// resulting in a response status of 409 - already exists.
gpii.tests.cloud.oauth2.settingsPut.disruptedTests.unshift(
gpii.tests.productionConfigTesting.addTestRecordsToDatabaseTests
);
// Add deletion of 'user' prefs from the database at the end of the "put"
// settings test definitions.
gpii.tests.cloud.oauth2.settingsPut.disruptedTests.push(
gpii.tests.productionConfigTesting.deleteTestRecordsFromDatabaseTests
);
fluid.each(gpii.tests.cloud.oauth2.settingsPut.disruptedTests, function (oneTest) {
gpii.test.cloudBased.oauth2.bootstrapDisruptedTest(
oneTest.testDef,
{},
oneTest.disruptions,
gpii.tests.productionConfigTesting.config,
"gpii.tests.productionConfigTesting.settingsPut.testCaseHolder"
);
});