From dc446c5f2ba60f70fd19919b79bae43d96fa3d08 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 19 Feb 2015 12:32:10 -0300 Subject: [PATCH] api tests working --- lib/client/api.js | 6 +- lib/expressapp.js | 5 ++ test/integration/clientApi.js | 155 +++++++++++++++++++++++----------- test/integration/server.js | 1 - 4 files changed, 117 insertions(+), 50 deletions(-) diff --git a/lib/client/api.js b/lib/client/api.js index 9020dbb..e964507 100644 --- a/lib/client/api.js +++ b/lib/client/api.js @@ -60,8 +60,11 @@ function API(opts) { this.verbose = !!opts.verbose; this.request = request || opts.request; this.baseUrl = opts.baseUrl || BASE_URL; + this.basePath = this.baseUrl.replace(/http.?:\/\/[a-zA-Z0-9:-]*\//,'/'); if (this.verbose) { log.level = 'debug'; + } else { + log.level = 'info'; } }; @@ -127,7 +130,8 @@ API.prototype._doRequest = function(method, url, args, data, cb) { var absUrl = this.baseUrl + url; var args = { - relUrl: url, + // relUrl: only for testing with `supertest` + relUrl: this.basePath + url, headers: { 'x-identity': data.copayerId, 'x-signature': reqSignature, diff --git a/lib/expressapp.js b/lib/expressapp.js index b39d61e..07f9de3 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -83,6 +83,11 @@ ExpressApp.start = function(opts) { function getServerWithAuth(req, res, cb) { var credentials = getCredentials(req); + if (!credentials) + return returnError(new CopayServer.ClientError({ + code: 'NOTAUTHORIZED' + }), res, req); + var auth = { copayerId: credentials.copayerId, message: req.method.toLowerCase() + '|' + req.url + '|' + JSON.stringify(req.body), diff --git a/test/integration/clientApi.js b/test/integration/clientApi.js index 6c6e84a..09a0a1d 100644 --- a/test/integration/clientApi.js +++ b/test/integration/clientApi.js @@ -6,6 +6,7 @@ var sinon = require('sinon'); var should = chai.should(); var levelup = require('levelup'); var memdown = require('memdown'); +var async = require('async'); var request = require('supertest'); var Client = require('../../lib/client'); var API = Client.API; @@ -15,66 +16,124 @@ var WalletUtils = require('../../lib/walletutils'); var ExpressApp = require('../../lib/expressapp'); var Storage = require('../../lib/storage'); -describe('client API ', function() { - var client, app; - beforeEach(function() { - var fsmock = {};; - fsmock.readFile = sinon.mock().yields(null, JSON.stringify(TestData.storage.wallet11)); - fsmock.writeFile = sinon.mock().yields(); - var storage = new Client.FileStorage({ - filename: 'dummy', - fs: fsmock, +var helpers = {}; + +helpers.getRequest = function(app) { + return function(args, cb) { + var req = request(app); + var r = req[args.method](args.relUrl); + + if (args.headers) { + _.each(args.headers, function(v, k) { + r.set(k, v); + }) + } + if (!_.isEmpty(args.body)) { + r.send(args.body); + }; + r.end(function(err, res) { + return cb(err, res, res.body); }); - client = new Client({ - storage: storage + }; +}; + +helpers.createAndJoinWallet = function(clients, m, n, cb) { + clients[0].createWallet('wallet name', 'creator copayer', m, n, 'testnet', + function(err, secret) { + if (err) return cb(err); + if (n == 1) return cb(); + + should.exist(secret); + async.each(_.range(n-1), function(i, cb) { + clients[i + 1].joinWallet(secret, 'copayer ' + (i + 1), function(err, result) { + should.not.exist(err); + return cb(err); + }); + }, function(err) { + if (err) return new Error('Could not generate wallet'); + return cb(); + }); }); +}; - var db = levelup(memdown, { - valueEncoding: 'json' +var fsmock = {}; +var content = {}; +fsmock.readFile = function(name, enc, cb) { + if (!content || _.isEmpty(content[name])) + return cb('empty'); + + return cb(null, content[name]); +}; +fsmock.writeFile = function(name, data, cb) { + content[name] = data; + return cb(); +}; + +describe('client API ', function() { + var clients; + + beforeEach(function() { + clients = []; + // Generates 5 clients + _.each(_.range(5), function(i) { + var storage = new Client.FileStorage({ + filename: 'client' + i, + fs: fsmock, + }); + var client = new Client({ + storage: storage, + }); + var db = levelup(memdown, { + valueEncoding: 'json' + }); + var storage = new Storage({ + db: db + }); + var app = ExpressApp.start({ + CopayServer: { + storage: storage + } + }); + client.request = helpers.getRequest(app); + clients.push(client); }); - var storage = new Storage({ - db: db + content={}; + }); + + describe.only('#getBalance', function() { + it('should check balance in a 1-1 ', function(done) { + helpers.createAndJoinWallet(clients, 1, 1, function(err) { + should.not.exist(err); + clients[0].getBalance(function(err, x) { + should.not.exist(err); + done(); + }) + }); }); - app = ExpressApp.start({ - CopayServer: { - storage: storage - } + it('should be able to check balance in a 2-3 wallet ', function(done) { + helpers.createAndJoinWallet(clients, 2, 3, function(err) { + should.not.exist(err); + clients[0].getBalance(function(err, x) { + should.not.exist(err); + clients[1].getBalance(function(err, x) { + should.not.exist(err); + clients[2].getBalance(function(err, x) { + should.not.exist(err); + done(); + }) + }) + }) + }); }); }); - var helpers = {}; - - helpers.request = function(args) { - if (args.method == 'get') { - request(app) - .get(relUrl) - .end(cb); - } else { - request(app) - .post(relUrl) - .send(body) - .end(function(err, res) { - console.log('[clientApi.js.59:err:]', err, res); //TODO - return cb(err, res); - }); - } - }; - describe('#_tryToComplete ', function() { - it.only('should complete a wallet ', function(done) { - var request = sinon.stub(); + it('should complete a wallet ', function(done) { + client.storage.fs.readFile = + sinon.stub().yields(null, JSON.stringify(TestData.storage.incompleteWallet22)); - // Wallet request - request.onCall(0).yields(null, { - statusCode: 200, - }, TestData.serverResponse.completeWallet); - request.onCall(1).yields(null, { - statusCode: 200, - }, "pepe"); - client.request = request; - client.storage.fs.readFile = sinon.stub().yields(null, JSON.stringify(TestData.storage.incompleteWallet22)); client.getBalance(function(err, x) { should.not.exist(err); done(); diff --git a/test/integration/server.js b/test/integration/server.js index 92490db..cf5e799 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -20,7 +20,6 @@ var Copayer = require('../../lib/model/copayer'); var CopayServer = require('../../lib/server'); var TestData = require('../testdata'); - var helpers = {}; helpers.getAuthServer = function(copayerId, cb) { var signatureStub = sinon.stub(CopayServer.prototype, '_verifySignature');