From 70dc68485b75bf049c7077750bc1b40b4a724c7c Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 14 Aug 2014 16:26:24 -0400 Subject: [PATCH] trying to get tests working --- API.js | 184 ++++++++++++++++++++++++++++ js/models/storage/LocalEncrypted.js | 2 + test/test.storage.File.js | 2 +- test/test.storage.LocalEncrypted.js | 16 +-- 4 files changed, 188 insertions(+), 16 deletions(-) create mode 100644 API.js diff --git a/API.js b/API.js new file mode 100644 index 000000000..5b4a969ed --- /dev/null +++ b/API.js @@ -0,0 +1,184 @@ +var WalletFactory = require('./js/models/core/WalletFactory'); + +var API = function(opts) { + this._init(opts); +}; + +API.prototype._init = function(opts) { + var self = this; + + opts = opts || {}; + self.opts = opts; + + this.walletFactory = new WalletFactory(opts); +}; + +API._coerceArgTypes = function(args, argTypes) { + for (var i in args) { + var arg = args[i]; + var argType = argTypes[i][1]; + if (typeof arg == 'string') { + switch (argType) { + case 'object': + args[i] = JSON.parse(arg); + break; + case 'number': + args[i] = Number(arg); + break; + } + } + } + + return args; +}; + +API.prototype._command = function(command, args, callback) { + var self = this; + + if (!command || command[0] == "_") + return callback(new Error('invalid command')); + + if (!API._checkArgTypes(command, args)) { + var argTypes = API.prototype[command].argTypes; + API._coerceArgTypes(args, argTypes) + if (!API._checkArgTypes(command, args)) + throw new Error('invalid arguments'); + } + + if (typeof self["_cmd_" + command] == 'function') { + var f = API.prototype[command]; + if (f.argTypes[f.argTypes.length - 1][1] == 'function') + return self["_cmd_" + command].apply(self, args.concat([callback])); + else + return callback(null, self["_cmd_" + command].apply(self, args)); + }; + + return callback(new Error('invalid command')); +}; + +API._checkArgTypes = function(command, args) { + var f = API.prototype[command]; + + if (f.argTypes.length != args.length) { + + //if the function doesn't have a callback + if (!(f.argTypes.length == args.length + 1 && f.argTypes[f.argTypes.length - 1][1] == 'function')) + return false; + } + + for (var i in args) { + if (typeof args[i] != f.argTypes[i][1]) + return false; + } + return true; +}; + +function decorate(command, argTypes) { + var d = function() { + API.prototype._command.call(this, command, Array.prototype.slice.call(arguments, 0)); + }; + + d.argTypes = argTypes; + + return d; +}; + +API.prototype._cmd_echo = function(str, callback) { + var self = this; + + return callback(null, str); +}; + +API.prototype.echo = decorate('echo', [ + ['str', 'string'], + ['callback', 'function'] +]); + +API.prototype._cmd_echoNumber = function(num, callback) { + var self = this; + + return callback(null, num); +}; + +API.prototype.echoNumber = decorate('echoNumber', [ + ['num', 'number'], + ['callback', 'function'] +]); + +API.prototype._cmd_echoObject = function(obj, callback) { + var self = this; + + return callback(null, obj); +}; + +API.prototype.echoObject = decorate('echoObject', [ + ['obj', 'object'], + ['callback', 'function'] +]); + +/* +API.prototype.getBalance = function(callback) { + var self = this; + + return callback(null, self.wallet.getBalance([])); +}; + +API.prototype.getBalance.argTypes = + [ + ['callback', 'function'] + ]; +*/ + +API.prototype._cmd_getArgTypes = function(command, callback) { + var self = this; + + if (command[0] == '_' || typeof API.prototype[command] != 'function') + return callback(new Error('Invalid command')); + + var argTypes = API.prototype[command].argTypes; + + return callback(null, argTypes); +}; + +API.prototype.getArgTypes = decorate('getArgTypes', [ + ['command', 'string'], + ['callback', 'function'] +]); + +API.prototype._cmd_getCommands = function(callback) { + var self = this; + + var fs = []; + + for (var i in API.prototype) { + var f = API.prototype[i]; + if (typeof f == 'function' && i[0] != "_") + fs.push(i); + }; + + return callback(null, fs); +}; + +API.prototype.getCommands = decorate('getCommands', [ + ['callback', 'function'] +]); + +API.prototype._cmd_getWallets = function(callback) { + var self = this; + + return callback(null, self.walletFactory.getWallets()); +}; + +API.prototype.getWallets = decorate('getWallets', [ + ['callback', 'function'] +]); + +API.prototype._cmd_help = function(callback) { + this._cmd_getCommands.apply(this, arguments); +}; + +API.prototype.help = decorate('help', [ + ['callback', 'function'] +]); + +module.exports = API; diff --git a/js/models/storage/LocalEncrypted.js b/js/models/storage/LocalEncrypted.js index cf51494b1..da1d99742 100644 --- a/js/models/storage/LocalEncrypted.js +++ b/js/models/storage/LocalEncrypted.js @@ -1,5 +1,7 @@ 'use strict'; +var CryptoJS = require('node-cryptojs-aes').CryptoJS; + var id = 0; function Storage(opts) { diff --git a/test/test.storage.File.js b/test/test.storage.File.js index edf25b5d5..b008af350 100644 --- a/test/test.storage.File.js +++ b/test/test.storage.File.js @@ -5,7 +5,7 @@ var should = chai.should(); var Storage = require('../js/models/storage/File.js'); var sinon = require('sinon'); var crypto = require('crypto'); -ar CryptoJS = require('node-cryptojs-aes').CryptoJS; +var CryptoJS = require('node-cryptojs-aes').CryptoJS; var mock = require('mock-fs'); diff --git a/test/test.storage.LocalEncrypted.js b/test/test.storage.LocalEncrypted.js index 28c272b3e..ecc946df1 100644 --- a/test/test.storage.LocalEncrypted.js +++ b/test/test.storage.LocalEncrypted.js @@ -1,15 +1,13 @@ 'use strict'; var chai = chai || require('chai'); var should = chai.should(); -var is_browser = typeof process == 'undefined' - || typeof process.versions === 'undefined'; +var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined'; if (is_browser) { var copay = require('copay'); //browser } else { var copay = require('../copay'); //node } var LocalEncrypted = copay.StorageLocalEncrypted; -var CryptoJS = require('node-cryptojs-aes').CryptoJS; var fakeWallet = 'fake-wallet-id'; @@ -88,18 +86,6 @@ describe('Storage/LocalEncrypted model', function() { //encrypted.slice(0,6).should.equal("53616c"); }); }); - describe('#_decryptObj', function() { - it('should decrypt and Obj', function() { - var storage = new LocalEncrypted({ - password: 'password', - localStorage: localMock, - }); - storage._decryptObj('{"a":"2"}').should.deep.equal({ - a: "2" - }); - }); - }); - describe('#remove', function() { it('should remove an item', function() {