Last active
December 10, 2015 06:08
-
-
Save joshtwist/4392688 to your computer and use it in GitHub Desktop.
Unit testing two Mobile Services scripts that encrypt on insert and decrypt on read.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // the insert script that encrypts the text column | |
| var crypto = require('crypto'); | |
| function insert(item, user, request) { | |
| // your encryption key - choose something better and randomly generated in real-life | |
| var password = 'zumoisawesome' | |
| var cipher = crypto.createCipher('aes256', password); | |
| var output = cipher.update(item.text, 'utf-8', 'base64'); | |
| item.text = output + cipher.final('base64'); | |
| // go ahead, insert the modified data | |
| request.execute(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // the read script that decrypts the text column | |
| var crypto = require('crypto'); | |
| function read(query, user, request) { | |
| // your encryption key - choose something better and randomly generated in real-life | |
| var password = 'zumoisawesome' | |
| // go ahead and fetch the data, but invoke my success function | |
| // and I'll respond to the device | |
| request.execute({ | |
| success: function(results) { | |
| // for each item in the result set | |
| results.forEach(function(row) { | |
| // decrypt the text column | |
| var decipher = crypto.createDecipher('aes256', password); | |
| var output = decipher.update(row.text, 'base64', 'binary'); | |
| output += decipher.final('binary'); | |
| row.text = new Buffer(output, 'binary').toString('utf-8'); | |
| }); | |
| // send the response to the device | |
| request.respond(); | |
| } | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // we need the assert module for testing | |
| var assert = require('assert'); | |
| // we need the fs module to load the scripts from disk (assuming in ./table folder) | |
| var fs = require('fs'); | |
| // suite of tests for my 'items' table | |
| suite('items', function() { | |
| // test to ensure data is encrypted before it hits the disk | |
| test('should encrypt data on insert', function(done) { | |
| // load the insert script | |
| eval(fs.readFileSync('./table/items.insert.js', 'utf8')); | |
| // create an item with text to be encrypted | |
| var item = { text : "boo" }; | |
| // create the mock request object | |
| var request = { | |
| // and the mock execute function | |
| execute : function() { | |
| // assertion | |
| assert.notEqual("boo", item.text); | |
| // test complete | |
| done(); | |
| } | |
| } | |
| // kick off the test by using the loaded insert method | |
| insert(item, null, request); | |
| }); | |
| // test to ensure that an insert followed by read yields the original value | |
| test('insert followed by read should return same value', function(done) { | |
| // load the insert and read scripts | |
| eval(fs.readFileSync('./table/items.insert.js', 'utf8')); | |
| eval(fs.readFileSync('./table/items.read.js', 'utf8')); | |
| // create a fake item with text to be encrypted | |
| var item = { text : "boo" }; | |
| // the request object passed to insert | |
| var insertRequest = { | |
| // and mock execute | |
| execute : function() { | |
| // assert our text is encrypted | |
| assert.notEqual(item.text, "boo"); | |
| // after insert call the read function | |
| read(null, null, readRequest); | |
| } | |
| } | |
| // the request object passed to read | |
| var readRequest = { | |
| // specify the mock execute function of request | |
| execute: function(options) { | |
| // call success, to go back into the script | |
| // as though we've just loaded | |
| options.success([item]); | |
| }, | |
| // create the mock respond function | |
| respond: function() { | |
| // assert that our text is now decrypted | |
| assert.equal(item.text, "boo"); | |
| // test is over | |
| done(); | |
| } | |
| } | |
| // kick off the flow | |
| insert(item, null, insertRequest); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment