-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add apply decision to orders functionality
- Loading branch information
Showing
2 changed files
with
181 additions
and
0 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
156 changes: 156 additions & 0 deletions
156
tests/lib/client/decision/applyByAccountIdAndOrderId-test.js
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,156 @@ | ||
const _ = require('lodash'); | ||
const should = require('should'); | ||
const sinon = require('sinon'); | ||
|
||
let siftScience; | ||
let v3HttpClient; | ||
let sandbox; | ||
|
||
before(() => { | ||
siftScience = require('../../../../lib'); | ||
v3HttpClient = require('../../../../lib/client/v3HttpClient'); | ||
|
||
sandbox = sinon.sandbox.create(); | ||
}); | ||
|
||
describe('lib', () => { | ||
describe('client', () => { | ||
describe('decisions', () => { | ||
describe('applyByAccountIdAndOrderId', () => { | ||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
const key = '123'; | ||
let siftScienceClient; | ||
|
||
before('create siftScienceClient', () => { | ||
siftScienceClient = siftScience.init(key); | ||
}); | ||
|
||
describe('when called without accountId', () => { | ||
const accountId = null; | ||
const userId = '123'; | ||
const orderId = '123'; | ||
|
||
it('should reject with an error', () => { | ||
return siftScienceClient.decisions.applyByAccountIdAndOrderId(accountId, userId, orderId) | ||
.then(should.not.exist) | ||
.catch(err => { | ||
should.exist(err); | ||
err.should.be.instanceOf(Error); | ||
err.message.should.equal('accountId is required'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when called without userId', () => { | ||
const accountId = '123'; | ||
const orderId = '123'; | ||
const userId = null; | ||
|
||
it('should reject with an error', () => { | ||
return siftScienceClient.decisions.applyByAccountIdAndOrderId(accountId, userId, orderId) | ||
.then(should.not.exist) | ||
.catch(err => { | ||
should.exist(err); | ||
err.should.be.instanceOf(Error); | ||
err.message.should.equal('userId is required'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when called without orderId', () => { | ||
const accountId = '123'; | ||
const orderId = null; | ||
const userId = '123'; | ||
|
||
it('should reject with an error', () => { | ||
return siftScienceClient.decisions.applyByAccountIdAndOrderId(accountId, userId, orderId) | ||
.then(should.not.exist) | ||
.catch(err => { | ||
should.exist(err); | ||
err.should.be.instanceOf(Error); | ||
err.message.should.equal('orderId is required'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when called with an accountId, userId and orderId', () => { | ||
const accountId = '123'; | ||
const userId = '123'; | ||
const orderId = '123'; | ||
const data = {}; | ||
const response = { | ||
data: {} | ||
}; | ||
|
||
let postStub; | ||
|
||
before('stub v3HttpClient.post()', () => { | ||
postStub = sandbox.stub(v3HttpClient, 'post') | ||
.returns(Promise.resolve(response)); | ||
}); | ||
|
||
it('should call v3HttpClient.post()', () => { | ||
return siftScienceClient.decisions.applyByAccountIdAndOrderId(accountId, userId, orderId, data) | ||
.then(result => { | ||
should.exist(result); | ||
result.should.deepEqual(response.data); | ||
|
||
postStub.callCount.should.equal(1); | ||
postStub.args[0][0].should.equal(`/accounts/${accountId}/users/${userId}/orders/${orderId}/decisions`); | ||
postStub.args[0][1].should.deepEqual(data); | ||
postStub.args[0][2].should.deepEqual({}); | ||
postStub.args[0][3].should.deepEqual({ | ||
auth: { | ||
username: siftScienceClient._key | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when called with params', () => { | ||
const accountId = '123'; | ||
const userId = '123'; | ||
const orderId = '123'; | ||
const data = {}; | ||
const params = { | ||
foo: 'bar' | ||
}; | ||
const response = { | ||
data: {} | ||
}; | ||
|
||
let postStub; | ||
|
||
before('stub v3HttpClient.post()', () => { | ||
postStub = sandbox.stub(v3HttpClient, 'post') | ||
.returns(Promise.resolve(response)); | ||
}); | ||
|
||
it('should call v3HttpClient.post()', () => { | ||
return siftScienceClient.decisions.applyByAccountIdAndOrderId(accountId, userId, orderId, data, params) | ||
.then(result => { | ||
should.exist(result); | ||
result.should.deepEqual(response.data); | ||
|
||
postStub.callCount.should.equal(1); | ||
postStub.args[0][0].should.equal(`/accounts/${accountId}/users/${userId}/orders/${orderId}/decisions`); | ||
postStub.args[0][1].should.deepEqual(_.extend(data, { | ||
$api_key: key | ||
})); | ||
postStub.args[0][2].should.deepEqual(params); | ||
postStub.args[0][3].should.deepEqual({ | ||
auth: { | ||
username: siftScienceClient._key | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |