gpii-universal
Version:
Cross platform, core components of the GPII personalization infrastructure.
966 lines (902 loc) • 39 kB
JavaScript
/* Tests for the remote file settings handler.
*
* Copyright 2019 Raising the Floor - International
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* The R&D leading to these results received funding from the
* Department of Education - Grant H421A150005 (GPII-APCP). However,
* these results do not necessarily represent the policy of the
* Department of Education, and you should not assume endorsement by the
* Federal Government.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
;
var fluid = require("infusion"),
path = require("path"),
fs = require("fs"),
os = require("os"),
rimraf = require("rimraf"),
mkdirp = require("mkdirp"),
gpii = fluid.registerNamespace("gpii"),
jqUnit = fluid.registerNamespace("jqUnit"),
kettle = require("kettle");
fluid.require("%gpii-universal/gpii/node_modules/journal/src/SettingsDir.js");
fluid.require("%gpii-universal/gpii/node_modules/lifecycleManager");
fluid.registerNamespace("gpii.tests.remoteFileSettingsHandler");
kettle.loadTestingSupport();
fluid.contextAware.makeChecks({
"gpii.contexts.test": {
value: true
}
});
require("settingsHandlers");
fluid.defaults("gpii.tests.remoteFileSettingsHandler", {
gradeNames: ["fluid.test.testEnvironment"],
components: {
server: {
type: "gpii.tests.remoteFileSettingsHandler.server"
},
tester: {
type: "gpii.tests.remoteFileSettingsHandler.testCaseHolder"
}
}
});
// HTTP server serving the files in this directory
fluid.defaults("gpii.tests.remoteFileSettingsHandler.server", {
gradeNames: ["kettle.server"],
port: 28531,
components: {
app: {
type: "kettle.app",
options: {
requestHandlers: {
main: {
type: "gpii.tests.remoteFileSettingsHandler.requestHandler",
route: "/:type/:arg",
method: "get"
}
}
}
}
}
});
fluid.defaults("gpii.tests.remoteFileSettingsHandler.requestHandler", {
gradeNames: ["kettle.request.http"],
invokers: {
handleRequest: {
funcName: "gpii.tests.remoteFileSettingsHandler.handleRequest",
args: [ "{that}", "{that}.req.params.type", "{that}.req.params.arg" ]
}
}
});
/**
* Handles a http request.
*
* @param {Object} request The request object.
* @param {String} type The type of request: file, error, content, or delay-content.
* @param {String} arg Additional data for the request.
*/
gpii.tests.remoteFileSettingsHandler.handleRequest = function (request, type, arg) {
switch (type) {
case "file":
// Send a file
try {
var string = fs.readFileSync(path.join(__dirname, path.basename(arg)), "utf8");
request.events.onSuccess.fire(string);
} catch (e) {
request.events.onError.fire({
message: "Not found",
statusCode: 404
});
}
break;
case "error":
// Respond with an error.
request.events.onError.fire({
message: "Error response",
statusCode: arg
});
break;
case "content":
// Return the request data
request.events.onSuccess.fire(arg);
break;
case "delay-content":
// Return the request data, after a random delay.
setTimeout(request.events.onSuccess.fire, Math.random() * 1000, arg);
break;
}
};
gpii.tests.remoteFileSettingsHandler.testDir = os.tmpdir() + "/gpii-test-remoteFile" + Math.random();
/** Creates the temporary test directory */
gpii.tests.remoteFileSettingsHandler.createDir = function () {
mkdirp.sync(gpii.tests.remoteFileSettingsHandler.testDir);
};
/** Removes the temporary test directory */
gpii.tests.remoteFileSettingsHandler.cleanDir = function () {
rimraf.sync(gpii.tests.remoteFileSettingsHandler.testDir);
};
/**
* Tests for the remoteFileSettingsHandler.
*/
fluid.defaults("gpii.tests.remoteFileSettingsHandler.testCaseHolder", {
gradeNames: ["fluid.test.testCaseHolder"],
members: {
downloadTo: path.join(gpii.tests.remoteFileSettingsHandler.testDir, "downloads", "download"),
stashDir: path.join(gpii.tests.remoteFileSettingsHandler.testDir, "stash")
},
listeners: {
"onCreate.createDir": {
funcName: "gpii.tests.remoteFileSettingsHandler.createDir"
},
"onDestroy.clearDir": {
funcName: "gpii.tests.remoteFileSettingsHandler.cleanDir"
}
},
modules: [{
name: "remoteFileSettingsHandler transferFile tests",
tests: [{
name: "transferFile Existing to non-existing, remove source (move)",
task: "gpii.tests.remoteFileSettingsHandler.testTransferFile",
args: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-1"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "destination-file-1"),
false
],
expect: 3,
resolve: "gpii.tests.remoteFileSettingsHandler.assertTransferFile",
resolveArgs: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-1"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "destination-file-1"),
false
]
}, {
name: "transferFile Existing to non-existing, keep source (copy)",
task: "gpii.tests.remoteFileSettingsHandler.testTransferFile",
args: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-2"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "destination-file-2"),
true
],
expect: 3,
resolve: "gpii.tests.remoteFileSettingsHandler.assertTransferFile",
resolveArgs: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-2"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "destination-file-2"),
true
]
}, {
name: "transferFile Existing to existing, remove source (move)",
task: "gpii.tests.remoteFileSettingsHandler.testTransferFile",
args: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-3"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-dest-3"),
false
],
expect: 3,
resolve: "gpii.tests.remoteFileSettingsHandler.assertTransferFile",
resolveArgs: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-3"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-dest-3"),
false
]
}, {
name: "transferFile Existing to existing, keep source (copy)",
task: "gpii.tests.remoteFileSettingsHandler.testTransferFile",
args: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-4"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-dest-4"),
true
],
expect: 3,
resolve: "gpii.tests.remoteFileSettingsHandler.assertTransferFile",
resolveArgs: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-4"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-dest-4"),
true
]
}, {
name: "transferFile non-existing to existing, remove source (move)",
task: "gpii.tests.remoteFileSettingsHandler.testTransferFile",
args: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "non-existing-file-5"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-dest-5"),
false
],
expect: 3,
resolve: "gpii.tests.remoteFileSettingsHandler.assertTransferFile",
resolveArgs: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "non-existing-file-5"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-dest-5"),
false
]
}, {
name: "transferFile non-existing to existing, keep source (copy)",
task: "gpii.tests.remoteFileSettingsHandler.testTransferFile",
args: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "non-existing-file-6"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-dest-6"),
false
],
expect: 3,
resolve: "gpii.tests.remoteFileSettingsHandler.assertTransferFile",
resolveArgs: [
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "non-existing-file-6"),
path.join(gpii.tests.remoteFileSettingsHandler.testDir, "existing-file-dest-6"),
false
]
}]
}, {
name: "remoteFileSettingsHandler download",
tests: [{
// First download ensures it's downloaded.
name: "remoteFileSettingsHandler.downloadFile (initial)",
expect: 2,
task: "gpii.settingsHandlers.remoteFileSettingsHandler.downloadFile",
args: ["http://localhost:28531/file/" + path.basename(__filename), "{that}.downloadTo"],
resolve: "gpii.tests.remoteFileSettingsHandler.assertDownload",
resolveArgs: [__filename, "{that}.downloadTo", false]
}, {
// Download again to test overwriting the existing.
name: "remoteFileSettingsHandler.downloadFile (overwrite)",
expect: 2,
task: "gpii.settingsHandlers.remoteFileSettingsHandler.downloadFile",
args: ["http://localhost:28531/file/" + path.basename(__filename), "{that}.downloadTo"],
resolve: "gpii.tests.remoteFileSettingsHandler.assertDownload",
resolveArgs: [__filename, "{that}.downloadTo", true]
}, {
// The download was deleted in the previous test to check downloading a non-existing file.
name: "remoteFileSettingsHandler.downloadFile (new)",
expect: 2,
task: "gpii.settingsHandlers.remoteFileSettingsHandler.downloadFile",
args: ["http://localhost:28531/file/" + path.basename(__filename), "{that}.downloadTo"],
resolve: "gpii.tests.remoteFileSettingsHandler.assertDownload",
resolveArgs: [__filename, "{that}.downloadTo", true]
}, {
// Try a failing download
name: "remoteFileSettingsHandler.downloadFile (new)",
expect: 1,
task: "gpii.settingsHandlers.remoteFileSettingsHandler.downloadFile",
args: ["http://localhost:28531/error/404", "{that}.downloadTo"],
reject: "jqUnit.assertTrue",
rejectArgs: [
"Should reject with an error",
"{arguments}.0.isError"
]
}]
}, {
name: "remoteFileSettings handler Settings",
tests: [{
name: "setting single file",
task: "gpii.tests.remoteFileSettingsHandler.testSetting",
args: [{
payload: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: "value1"
},
options: {
settings: {
f1: {path: "~file1", url: "http://localhost:28531/content/file-one.%value"}
}
}
}]
},
result: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: {
oldValue: {
value: "value1",
stashPath: "file1.gpii-stashed",
restore: true
},
newValue: "value1"
}
}
}]
},
initialFiles: {},
expectedFiles: {
file1: "file-one.value1"
}
}],
resolve: "fluid.identity"
}, {
name: "setting single file, existing",
task: "gpii.tests.remoteFileSettingsHandler.testSetting",
args: [{
payload: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: "value1"
},
options: {
settings: {
f1: {path: "~file1", url: "http://localhost:28531/content/new-content.%value"}
}
}
}]
},
result: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: {
oldValue: {
value: "value1",
stashPath: "file1.gpii-stashed",
restore: true
},
newValue: "value1"
}
}
}]
},
initialFiles: {
file1: "original-content"
},
expectedFiles: {
file1: "new-content.value1"
}
}],
resolve: "fluid.identity"
}, {
name: "setting two files",
task: "gpii.tests.remoteFileSettingsHandler.testSetting",
args: [{
payload: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: "value1",
f2: "value2"
},
options: {
settings: {
f1: {path: "~file1", url: "http://localhost:28531/content/new-content1.%value"},
f2: {path: "~file2", url: "http://localhost:28531/content/new-content2.%value"}
}
}
}]
},
result: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: {
oldValue: {
value: "value1",
stashPath: "file1.gpii-stashed",
restore: true
},
newValue: "value1"
},
f2: {
oldValue: {
value: "value2",
stashPath: "file2.gpii-stashed",
restore: true
},
newValue: "value2"
}
}
}]
},
initialFiles: {},
expectedFiles: {
file1: "new-content1.value1",
file2: "new-content2.value2"
}
}],
resolve: "fluid.identity"
}, {
name: "setting two files, 1 exists",
task: "gpii.tests.remoteFileSettingsHandler.testSetting",
args: [{
payload: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: "value1",
f2: "value2"
},
options: {
settings: {
f1: {path: "~file1", url: "http://localhost:28531/content/new-content1.%value"},
f2: {path: "~file2", url: "http://localhost:28531/content/new-content2.%value"}
}
}
}]
},
result: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: {
oldValue: {
value: "value1",
stashPath: "file1.gpii-stashed",
restore: true
},
newValue: "value1"
},
f2: {
oldValue: {
value: "value2",
stashPath: "file2.gpii-stashed",
restore: true
},
newValue: "value2"
}
}
}]
},
initialFiles: {
file1: "original-content1"
},
expectedFiles: {
file1: "new-content1.value1",
file2: "new-content2.value2"
}
}],
resolve: "fluid.identity"
}, {
name: "setting ten files, half exist, different response times",
task: "gpii.tests.remoteFileSettingsHandler.testSetting",
args: [{
payload: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: "value1",
f2: "value2",
f3: "value3",
f4: "value4",
f5: "value5",
f6: "value6",
f7: "value7",
f8: "value8",
f9: "value9",
f10: "value10"
},
options: {
settings: {
f1: {path: "~file1", url: "http://localhost:28531/delay-content/new-content1.%value"},
f2: {path: "~file2", url: "http://localhost:28531/delay-content/new-content2.%value"},
f3: {path: "~file3", url: "http://localhost:28531/delay-content/new-content3.%value"},
f4: {path: "~file4", url: "http://localhost:28531/delay-content/new-content4.%value"},
f5: {path: "~file5", url: "http://localhost:28531/delay-content/new-content5.%value"},
f6: {path: "~file6", url: "http://localhost:28531/delay-content/new-content6.%value"},
f7: {path: "~file7", url: "http://localhost:28531/delay-content/new-content7.%value"},
f8: {path: "~file8", url: "http://localhost:28531/delay-content/new-content8.%value"},
f9: {path: "~file9", url: "http://localhost:28531/delay-content/new-content9.%value"},
f10: {path: "~file10", url: "http://localhost:28531/delay-content/new-content10.%value"}
}
}
}]
},
result: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: {
oldValue: {
value: "value1",
stashPath: "file1.gpii-stashed",
restore: true
},
newValue: "value1"
},
f2: {
oldValue: {
value: "value2",
stashPath: "file2.gpii-stashed",
restore: true
},
newValue: "value2"
},
f3: {
oldValue: {
value: "value3",
stashPath: "file3.gpii-stashed",
restore: true
},
newValue: "value3"
},
f4: {
oldValue: {
value: "value4",
stashPath: "file4.gpii-stashed",
restore: true
},
newValue: "value4"
},
f5: {
oldValue: {
value: "value5",
stashPath: "file5.gpii-stashed",
restore: true
},
newValue: "value5"
},
f6: {
oldValue: {
value: "value6",
stashPath: "file6.gpii-stashed",
restore: true
},
newValue: "value6"
},
f7: {
oldValue: {
value: "value7",
stashPath: "file7.gpii-stashed",
restore: true
},
newValue: "value7"
},
f8: {
oldValue: {
value: "value8",
stashPath: "file8.gpii-stashed",
restore: true
},
newValue: "value8"
},
f9: {
oldValue: {
value: "value9",
stashPath: "file9.gpii-stashed",
restore: true
},
newValue: "value9"
},
f10: {
oldValue: {
value: "value10",
stashPath: "file10.gpii-stashed",
restore: true
},
newValue: "value10"
}
}
}]
},
initialFiles: {
file4: "original-content4",
file5: "original-content5",
file7: "original-content7",
file8: "original-content8",
file9: "original-content9"
},
expectedFiles: {
file1: "new-content1.value1",
file2: "new-content2.value2",
file3: "new-content3.value3",
file4: "new-content4.value4",
file5: "new-content5.value5",
file6: "new-content6.value6",
file7: "new-content7.value7",
file8: "new-content8.value8",
file9: "new-content9.value9",
file10: "new-content10.value10"
}
}],
resolve: "fluid.identity"
}, {
name: "setting 1 file, 1 error",
task: "gpii.tests.remoteFileSettingsHandler.testSetting",
args: [{
payload: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: "value1"
},
options: {
settings: {
f1: {path: "~file1", url: "http://localhost:28531/error/404"}
}
}
}]
},
result: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: {
failed: true
}
}
}]
},
initialFiles: {
file1: "original-content1"
},
expectedFiles: {
file1: "original-content1"
}
}],
resolve: "fluid.identity"
}, {
name: "setting 3 files, 1 error",
task: "gpii.tests.remoteFileSettingsHandler.testSetting",
args: [{
payload: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: "value1",
f2: "value2",
f3: "value3"
},
options: {
settings: {
f1: {path: "~file1", url: "http://localhost:28531/content/new-content1.%value"},
f2: {path: "~file2", url: "http://localhost:28531/error/404"},
f3: {path: "~file3", url: "http://localhost:28531/error/404"}
}
}
}]
},
result: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: {
oldValue: {
value: "value1",
stashPath: "file1.gpii-stashed",
restore: true
},
newValue: "value1"
},
f2: {
failed: true
},
f3: {
failed: true
}
}
}]
},
initialFiles: {
file1: "original-content1",
file3: "original-content3"
},
expectedFiles: {
file1: "new-content1.value1",
file3: "original-content3"
}
}],
resolve: "fluid.identity"
}, {
name: "setting 3 files, 1 error",
task: "gpii.tests.remoteFileSettingsHandler.testSetting",
args: [{
payload: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: "value1",
f2: "value2",
f3: "value3"
},
options: {
settings: {
f1: {path: "~file1", url: "http://localhost:28531/content/new-content1.%value"},
f2: {path: "~file2", url: "http://localhost:28531/error/404"},
f3: {path: "~file3", url: "http://localhost:28531/content/new-content3.%value"}
}
}
}]
},
result: {
"gpii.test.remoteFileSetting": [{
settings: {
f1: {
oldValue: {
value: "value1",
stashPath: "file1.gpii-stashed",
restore: true
},
newValue: "value1"
},
f2: {
failed: true
},
f3: {
oldValue: {
value: "value3",
stashPath: "file3.gpii-stashed",
restore: true
},
newValue: "value3"
}
}
}]
},
initialFiles: {
file1: "original-content1",
file2: "original-content2",
file3: "original-content3"
},
expectedFiles: {
file1: "new-content1.value1",
file2: "original-content2",
file3: "new-content3.value3"
}
}],
resolve: "fluid.identity"
}]
}]
});
/**
* Check that a downloaded file is correct.
*
* @param {String} origFile The original file that was downloaded.
* @param {String} downloadedFile The resulting file of the download.
* @param {Boolean} deleteDownload true to delete the downloaded file.
*/
gpii.tests.remoteFileSettingsHandler.assertDownload = function (origFile, downloadedFile, deleteDownload) {
// Sanity check
jqUnit.assertNotEquals("The downloaded file and original file should be different file names",
origFile, downloadedFile);
var origContent = fs.readFileSync(origFile, "utf8");
var downloadContent = fs.readFileSync(downloadedFile, "utf8");
jqUnit.assertEquals("Downloaded content should match the original", origContent, downloadContent);
if (deleteDownload) {
fs.unlinkSync(downloadedFile);
fs.rmdirSync(path.dirname(downloadedFile));
}
};
/**
* Runs transferFile, after creating a test file if required.
*
* @param {String} source The path of the file to move.
* @param {String} destination The destination path.
* @return {Promise} Resolves when complete.
*/
gpii.tests.remoteFileSettingsHandler.testTransferFile = function (source, destination) {
if (path.basename(source).startsWith("existing-file")) {
fs.writeFileSync(source, "existing" + source);
}
return gpii.settingsHandlers.remoteFileSettingsHandler.transferFile(source, destination);
};
/**
* Calls transferFile, after creating some files if required.
*
* @param {String} source Path to the new file.
* @param {String} destination Path to where the new file should be placed.
* @param {Boolean} keepSource true if the source file needs to be kept (ie, the download should be cached).
* @return {Promise} Resolves when complete.
*/
gpii.tests.remoteFileSettingsHandler.testTransferFile = function (source, destination, keepSource) {
// Create the existing files as required.
if (path.basename(source).startsWith("existing-file")) {
fs.writeFileSync(source, "transferFile source" + source);
}
if (path.basename(destination).startsWith("existing-file")) {
fs.writeFileSync(destination, "transferFile destination" + source);
}
return gpii.settingsHandlers.remoteFileSettingsHandler.transferFile(source, destination, keepSource);
};
/**
* Checks the result of testTransferFile
* @param {String} source Path to the new file.
* @param {String} destination Path to where the new file should be placed.
* @param {Boolean} keepSource true if the source file needs to be kept (ie, the download should be cached).
*/
gpii.tests.remoteFileSettingsHandler.assertTransferFile = function (source, destination, keepSource) {
jqUnit.assertEquals("The source file should only exist if keepSource is true", keepSource, fs.existsSync(source));
var existingSource = path.basename(source).startsWith("existing-file");
jqUnit.assertEquals("The destination file should exist if the source did",
existingSource, fs.existsSync(destination));
if (existingSource) {
jqUnit.assertEquals("The destination file should have the correct content",
"transferFile source" + source, fs.readFileSync(destination, "utf8"));
} else {
jqUnit.assert("Destination does not exist");
}
};
/**
* Checks if a directory contains only the files described in the expectedFiles object.
*
* @param {String} dir The directory to inspect.
* @param {Object} expectedFiles Map of filename => content.
*/
gpii.tests.remoteFileSettingsHandler.checkFiles = function (dir, expectedFiles) {
// Check the new files are correct.
var actualFiles = fs.readdirSync(dir);
jqUnit.assertDeepEq("Only the expected files should exist",
Object.keys(expectedFiles).sort(), actualFiles.sort());
fluid.each(expectedFiles, function (content, file) {
var filepath = path.join(dir, file);
jqUnit.assertTrue("File '" + file + "' should exist after applying the setting.", fs.existsSync(filepath));
var actualContent = fs.readFileSync(filepath, "utf8");
jqUnit.assertEquals("Content for file '" + file + "' should be the expected content", content, actualContent);
});
};
/**
* Fixes all "path" members deep within an object to be child of the given directory.
* @param {Object} obj The object to work on.
* @param {String} dir The directory to prepend to the paths.
* @return {Object} The modified copy of obj.
*/
gpii.tests.remoteFileSettingsHandler.fixPath = function (obj, dir) {
return fluid.transform(obj, function (value, key) {
if (key === "path" && value.startsWith("~")) {
return path.join(dir, value.substr(1));
} else if (fluid.isPlainObject(value)) {
return gpii.tests.remoteFileSettingsHandler.fixPath(value, dir);
} else {
return value;
}
});
};
/**
* Removes the timestamp from the files identified in the "stashPath" members deep within an object, making the names
* more predictable.
* @param {Object} obj The object to work on.
* @return {Object} The modified copy of obj.
*/
gpii.tests.remoteFileSettingsHandler.fixStashPath = function (obj) {
return fluid.transform(obj, function (value, key) {
if (key === "stashPath") {
return path.basename(value).replace(/\.20..-.*(?=\.gpii-stashed)/, "");
} else if (fluid.isPlainObject(value)) {
return gpii.tests.remoteFileSettingsHandler.fixStashPath(value);
} else {
return value;
}
});
};
/**
* Recording of which custom invokers where invoked.
*/
gpii.tests.remoteFileSettingsHandler.invokedValues = null;
/**
* Performs an end-to-end test of the remote file settings handler.
* @param {Object} testData The test data.
* @return {Promise} Resolves when complete.
*/
gpii.tests.remoteFileSettingsHandler.testSetting = function (testData) {
var promise = fluid.promise();
var dir = path.join(gpii.tests.remoteFileSettingsHandler.testDir, "setting" + Math.random());
mkdirp.sync(dir);
// Fix the paths in the payload
var test = gpii.tests.remoteFileSettingsHandler.fixPath(testData, dir);
// Create the initial files.
fluid.each(test.initialFiles, function (content, file) {
fs.writeFileSync(path.join(dir, file), content);
});
// post-apply/restore file checks
jqUnit.expect(2 +
(Object.keys(test.expectedFiles).length * 2 + 1) +
(Object.keys(test.initialFiles).length * 2 + 1));
// Record the before/after invokes
gpii.tests.remoteFileSettingsHandler.invokedValues = {};
fluid.log("Applying setting");
// Apply the setting.
var settingPromise = gpii.settingsHandlers.remoteFileSettingsHandler.set(test.payload);
settingPromise.then(function (result) {
fluid.logObjectRenderChars = 0xffffff;
fluid.log(result);
var checkResult = gpii.tests.remoteFileSettingsHandler.fixStashPath(result);
jqUnit.assertDeepEq("Result should be what's expected", test.result, checkResult);
// Check the files have been correctly applied.
gpii.tests.remoteFileSettingsHandler.checkFiles(dir, test.expectedFiles);
// Restore the setting.
var restore = gpii.settingsHandlers.transformPayload(gpii.settingsHandlers.setResponseToSnapshot(result), function (setting, settingPath) {
var testSetting = fluid.get(test.payload, settingPath);
return {
settings: setting.settings,
options: testSetting.options
};
});
fluid.log("Restoring setting");
gpii.settingsHandlers.remoteFileSettingsHandler.set(restore).then(function () {
// Check the files have been correctly restored.
gpii.tests.remoteFileSettingsHandler.checkFiles(dir, test.initialFiles);
// Check the custom invokers were hit
jqUnit.assertDeepEq("custom invokers should have been invoked as expected",
test.expectedInvokes || {}, gpii.tests.remoteFileSettingsHandler.invokedValues);
promise.resolve();
});
});
return promise;
};
module.exports = kettle.test.bootstrap("gpii.tests.remoteFileSettingsHandler");