This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from bookchin/master
v2.2.0 - Exchange Reporting
- Loading branch information
Showing
4 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
|
||
}); | ||
|
||
}); |