Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #286 from bookchin/master
Browse files Browse the repository at this point in the history
v2.2.0 - Exchange Reporting
  • Loading branch information
braydonf committed Nov 14, 2016
2 parents b391791 + 89bdbb8 commit 66e5ade
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/server/routefactory.js
Expand Up @@ -6,7 +6,8 @@ module.exports = function RouteFactory(options) {
require('./routes/pubkeys'),
require('./routes/users'),
require('./routes/frames'),
require('./routes/contacts')
require('./routes/contacts'),
require('./routes/reports')
]).map(function(Router) {
return Router({
config: options.config,
Expand Down
54 changes: 54 additions & 0 deletions lib/server/routes/reports.js
@@ -0,0 +1,54 @@
'use strict';

const Router = require('./index');
const log = require('../../logger');
const errors = require('storj-service-error-types');
const inherits = require('util').inherits;

/**
* Handles endpoints for reporting
* @constructor
* @extends {Router}
*/
function ReportsRouter(options) {
if (!(this instanceof ReportsRouter)) {
return new ReportsRouter(options);
}

Router.apply(this, arguments);
}

inherits(ReportsRouter, Router);

/**
* Creates an exchange report
* @param {http.IncomingMessage} req
* @param {http.ServerResponse} res
* @param {Function} next
*/
ReportsRouter.prototype.createExchangeReport = function(req, res, next) {
var exchangeReport = new this.storage.models.ExchangeReport(req.body);

// TODO: Add signature/identity verification

log.info('received exchange report');
exchangeReport.save(function(err) {
if (err) {
return next(new errors.BadRequestError(err.message));
}

log.info('exchange report saved');
res.status(201).send({});
});
};

/**
* @private
*/
ReportsRouter.prototype._definitions = function() {
return [
['POST', '/reports/exchanges', this.createExchangeReport.bind(this)]
];
};

module.exports = ReportsRouter;
6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "storj-bridge",
"version": "2.1.0",
"version": "2.2.0",
"description": "Access the Storj network using a simple REST API.",
"main": "index.js",
"directories": {
Expand Down Expand Up @@ -69,12 +69,12 @@
"nodemailer": "^2.0.0",
"readable-stream": "^2.0.5",
"storj-complex": "^2.0.1",
"storj-lib": "^5.0.0",
"storj-lib": "^5.1.1",
"storj-mongodb-adapter": "^4.0.1",
"storj-service-error-types": "^1.0.0",
"storj-service-mailer": "^1.0.0",
"storj-service-middleware": "^1.1.0",
"storj-service-storage-models": "^5.0.2",
"storj-service-storage-models": "^5.1.2",
"through": "^2.3.8"
}
}
79 changes: 79 additions & 0 deletions test/server/routes/reports.unit.js
@@ -0,0 +1,79 @@
'use strict';

const httpMocks = require('node-mocks-http');
const sinon = require('sinon');
const storj = require('storj-lib');
const expect = require('chai').expect;
const EventEmitter = require('events').EventEmitter;
const ReportsRouter = require('../../../lib/server/routes/reports');

describe('ReportsRouter', function() {

var reportsRouter = new ReportsRouter(
require('../../_fixtures/router-opts')
);

describe('#createExchangeReport', function() {

it('should return internal error if save fails', function(done) {
var request = httpMocks.createRequest({
method: 'POST',
url: '/reports/exchanges',
body: {
reporterId: storj.utils.rmd160('client'),
farmerId: storj.utils.rmd160('farmer'),
clientId: storj.utils.rmd160('client'),
dataHash: storj.utils.rmd160('data'),
exchangeTime: Date.now(),
exchangeResultCode: 1000,
exchangeResultMessage: 'SUCCESS'
}
});
var response = httpMocks.createResponse({
req: request,
eventEmitter: EventEmitter
});
var _reportSave = sinon.stub(
reportsRouter.storage.models.ExchangeReport.prototype,
'save'
).callsArgWith(0, new Error('Failed to save report'));
reportsRouter.createExchangeReport(request, response, function(err) {
_reportSave.restore();
expect(err.message).to.equal('Failed to save report');
done();
});
});

it('should send 201 if report saved', function(done) {
var request = httpMocks.createRequest({
method: 'POST',
url: '/reports/exchanges',
body: {
reporterId: storj.utils.rmd160('client'),
farmerId: storj.utils.rmd160('farmer'),
clientId: storj.utils.rmd160('client'),
dataHash: storj.utils.rmd160('data'),
exchangeTime: Date.now(),
exchangeResultCode: 1000,
exchangeResultMessage: 'SUCCESS'
}
});
var response = httpMocks.createResponse({
req: request,
eventEmitter: EventEmitter
});
var _reportSave = sinon.stub(
reportsRouter.storage.models.ExchangeReport.prototype,
'save'
).callsArgWith(0, null);
response.on('end', function() {
_reportSave.restore();
expect(response.statusCode).to.equal(201);
done();
});
reportsRouter.createExchangeReport(request, response);
});

});

});

0 comments on commit 66e5ade

Please sign in to comment.