Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move test into the appropriate directory. Refactor tests to use common fixture. Add exception notification based on error type. Detect wrapped errors and report them correctly.
- Loading branch information
Oliver Esser
committed
Dec 4, 2016
1 parent
15930ec
commit fc2eb59
Showing
4 changed files
with
76 additions
and
41 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 was deleted.
Oops, something went wrong.
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,59 @@ | ||
const buster = require('buster') | ||
const VError = require('verror').VError | ||
const postApiScore = require('../../lib/server/post-api-score') | ||
const scoreManager = require('../../lib/score-manager') | ||
const config = require('../../config') | ||
const InvalidInputError = require('../../lib/invalid-input-error') | ||
|
||
buster.testCase('postApiScore', { | ||
setUp: function () { | ||
this.fakeScoreManager = { score: this.stub() } | ||
this.create = this.stub(scoreManager, 'create').returns(this.fakeScoreManager) | ||
|
||
this.req = { body: {} } | ||
this.res = { send: this.stub(), json: this.stub() } | ||
}, | ||
|
||
'should use scoreManager\'s create function': function () { | ||
postApiScore(this.req, this.res) | ||
buster.assert.called(this.create) | ||
}, | ||
|
||
'should pass default config when request has no config': function () { | ||
postApiScore(this.req, this.res) | ||
buster.assert.calledWith(this.create, config.scoreManager) | ||
}, | ||
|
||
'should pass request config when available': function () { | ||
this.req.body.config = {} | ||
postApiScore(this.req, this.res) | ||
buster.assert.calledWith(this.create, {}) | ||
}, | ||
|
||
'should wrap result in object with success flag set to true': function () { | ||
this.fakeScoreManager.score.returns('my result') | ||
postApiScore(this.req, this.res) | ||
buster.assert.calledWith(this.res.json, { success: true, result: 'my result' }) | ||
}, | ||
|
||
'should pass along error message on input error': function () { | ||
this.fakeScoreManager.score.throws(new InvalidInputError('There was a problem')) | ||
postApiScore(this.req, this.res) | ||
buster.assert.calledWith(this.res.json, { success: false, error: 'There was a problem' }) | ||
}, | ||
|
||
'should pass along error message on wrapped input error': function () { | ||
this.fakeScoreManager.score.throws( | ||
new VError( | ||
new InvalidInputError('There was a specific problem'), | ||
'There was a general problem')) | ||
postApiScore(this.req, this.res) | ||
buster.assert.calledWith(this.res.json, { success: false, error: 'There was a general problem: There was a specific problem' }) | ||
}, | ||
|
||
'should signal an internal server error on error conditions other than input and rethrow exception': function () { | ||
this.fakeScoreManager.score.throws(new Error('There was an internal problem')) | ||
buster.assert.exception(() => postApiScore(this.req, this.res)) | ||
buster.assert.calledWith(this.res.json, { success: false, error: 'Internal Server Error' }) | ||
} | ||
}) |