Skip to content

Commit

Permalink
add function to remove character 2 for operations get and post
Browse files Browse the repository at this point in the history
  • Loading branch information
jesuspg committed Feb 3, 2016
1 parent eac651c commit d38365b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/routes/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ function validateKey(name) {
return result;
}

/**
* check if a string ends with a suffix
* @param {String} suffix
* @returns {boolean}
*/
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

/**
* remove character 2 at the end of region name
* @param {String} region
* @returns {String}
*/
function removeCharacter2(region) {
var newName = region;
if (newName.endsWith('2')) {
newName = newName.substring(0, newName.length - 1);
}

return newName;
}

/**
* get the key for region and type of key
* @param {Object} req
Expand All @@ -71,6 +94,8 @@ function validateKey(name) {
function getKey(req, res) {

var region = req.params.region.toLowerCase();
region = removeCharacter2(region);

var key = req.params.key;
logger.info('Request ' + key + ' for ' + region);
var statusCode = 404;
Expand Down Expand Up @@ -125,6 +150,7 @@ function saveKeyToFile(region, content, res) {
throw error;
}

region = removeCharacter2(region);
var filename = keysPath + region.toLowerCase() + '.' + key + 'key';

fs.writeFile(filename, content, function (err) {
Expand Down
52 changes: 52 additions & 0 deletions test/unit/test_v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,57 @@ suite('v1', function () {

});

test('should_return_a_valid_ssh_key_for_valid_region_ended_with_2', function() {
var req = sinon.stub(),
res = sinon.stub();

req.params = sinon.stub();
req.params.region = 'region12';
req.params.key = 'sshkey';
res.write = sinon.spy();
res.setHeader = sinon.spy();
res.type = sinon.spy();
res.end = sinon.spy();

v1.getKey(req, res);

assert(res.write.calledOnce);
assert(res.setHeader.calledOnce);
assert(res.type.calledOnce);
assert(res.end.calledOnce);

});

test('should_save_key_to_disk_when_post_a_key_for_region_ended_with_2', function() {
//given
var body = 'ssh-rsa fBIqA5CALsR/gF6ITbjnSSc5pYTDZ/T0JwIb5Z admin@domain.com';
var res = sinon.stub();

var fsStub = sinon.stub(fs, 'writeFile', function (path, content, callbackWriteFile) {
console.log('fake fs.writeFile');
assert(path === basePath + 'region1.sshkey');
callbackWriteFile();
fs.writeFile.restore();
});
res.status = sinon.stub();
res.write = sinon.stub();
res.type = sinon.stub();
res.setHeader = sinon.stub();
res.end = sinon.spy();

//when
v1.saveKeyToFile('region12', body, res);

//then
assert(fsStub.calledOnce);
assert(res.status.withArgs(201).calledOnce);
assert(res.setHeader.withArgs().calledOnce);
assert(res.type.calledOnce);
assert(res.write.withArgs(body, 'utf8').calledOnce);
assert(res.end.calledOnce);


});


});

0 comments on commit d38365b

Please sign in to comment.