Skip to content

Commit

Permalink
Add apply decision to orders functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
grantgasp committed Nov 2, 2018
1 parent ed53c24 commit e2fe605
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/client/decisions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ function getDecisionsApi(siftScienceClient) {
throw error;
});
},

/**
* @param {String} accountId
* @param {String} orderId
* @param {String} userId
* @param {Object} [data]
* @param {Object} [params]
* https://siftscience.com/developers/docs/curl/decisions-api/apply-decisions
*/
applyByAccountIdAndOrderId(accountId, userId, orderId, data = {}, params = {}) {
if (!accountId) return Promise.reject(new Error('accountId is required'));
if (!orderId) return Promise.reject(new Error('orderId is required'));
if (!userId) return Promise.reject(new Error('userId is required'));

return Promise.resolve(v3HttpClient.post(`/accounts/${accountId}/users/${userId}/orders/${orderId}/decisions`, data, params, {
auth: {
username: key
}
}))
.then(response => response.data)
.catch(error => {
logger.logRequestError(error);
throw error;
});
},
/**
* @param {Object} [data]
* @param {Object} [params]
Expand Down
156 changes: 156 additions & 0 deletions tests/lib/client/decision/applyByAccountIdAndOrderId-test.js
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
}
});
});
});
});
});
});
});
});

0 comments on commit e2fe605

Please sign in to comment.