Skip to content

Commit

Permalink
Update API test
Browse files Browse the repository at this point in the history
  • Loading branch information
hickeyma committed Dec 13, 2016
1 parent 0f4902d commit 9785df2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
23 changes: 16 additions & 7 deletions test/unit/api/i18n/_get.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
define(function (require) {
var Promise = require('bluebird');
var expect = require('intern/dojo/node!expect.js');
var testData = require('intern/dojo/node!../../../unit/api/i18n/data');

return function (bdd, scenarioManager, request) {
bdd.describe('GET translations', function postIngest() {
Expand All @@ -9,13 +9,22 @@ define(function (require) {
return scenarioManager.reload('emptyKibana');
});

bdd.it('should return 200 for an valid payload', function validPayload() {
return Promise.all([
request.get('/i18n/translations').expect(200),
]);
bdd.before(function () {
return testData.createTestTranslations();
});
//TODO (hickeyma): Extend test cases once I have refactored the module and hapi APIs after feedback on removeing plugin namd and language from API

bdd.it('should return 200 and a valid response payload', function validPayload() {
return request.get('/i18n/translations')
.expect(200)
.then(function (res) {
var translationsReturned = JSON.parse(res.text);
expect(testData.checkReturnedTranslations(translationsReturned)).to.be(true);
});
});

bdd.after(function () {
return testData.deleteTestTranslations();
});
});
};
}
});
52 changes: 52 additions & 0 deletions test/unit/api/i18n/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var fs = require('fs');
var mkdirp = require('mkdirp');
var process = require('child_process');

var TRANSLATION_STORE_PATH = __dirname + '/../../../../data/translations';

var checkReturnedTranslations = function (actualTranslationJson) {
var language = 'en';
var expectedTranslationJsonFile = __dirname + '/data/reference/' + language + '.json';
var expectedTranslationJson = require(expectedTranslationJsonFile);

return compareTranslations(actualTranslationJson, expectedTranslationJson);
};

var createTestTranslations = function () {
var translationStorePath = TRANSLATION_STORE_PATH;
var language = 'en';
var srcFile = __dirname + '/data/reference/' + language + '.json';
var destFile = translationStorePath + '/' + language + '.json';

if (!fs.existsSync(translationStorePath)) {
mkdirp.sync(translationStorePath);
}
console.log('Write file: ' + srcFile + ' to file: ' + destFile);
fs.writeFileSync(destFile, fs.readFileSync(srcFile));
};

var deleteTestTranslations = function () {
var translationStorePath = TRANSLATION_STORE_PATH;
process.execSync('rm -rf ' + translationStorePath);
};

function compareTranslations(actual, expected) {
var equal = true;

for (var key in expected) {
if (!actual.hasOwnProperty(key)) {
equal = false;
break;
}
if (actual[key] !== expected[key]) {
equal = false;
break;
}
}

return equal;
}

module.exports.checkReturnedTranslations = checkReturnedTranslations;
module.exports.createTestTranslations = createTestTranslations;
module.exports.deleteTestTranslations = deleteTestTranslations;
4 changes: 4 additions & 0 deletions test/unit/api/i18n/data/reference/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"test_plugin_1-NO_SSL": "Dont run the DE dev server using HTTPS",
"test_plugin_1-DEV": "Run the DE server with development mode defaults"
}
10 changes: 10 additions & 0 deletions test/unit/api/i18n/data/reference/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"test_plugin_1-NO_SSL": "Dont run the dev server using HTTPS",
"test_plugin_1-DEV": "Run the server with development mode defaults",
"test_plugin_1-NO_RUN_SERVER": "Dont run the dev server",
"test_plugin_1-HOME": "Run along home now!",
"test_plugin_2-XXXXXX": "This is XXXXXX string",
"test_plugin_2-YYYY_PPPP": "This is YYYY_PPPP string",
"test_plugin_2-FFFFFFFFFFFF": "This is FFFFFFFFFFFF string",
"test_plugin_2-ZZZ": "This is ZZZ string"
}

0 comments on commit 9785df2

Please sign in to comment.