-
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.
Merge pull request #6 from denis-vanat/master
add(decisions): apply decisions by accountId, userId and sessionId
- Loading branch information
Showing
9 changed files
with
357 additions
and
1 deletion.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
179 changes: 179 additions & 0 deletions
179
tests/lib/client/decision/applyByAccountIdAndSessionId-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,179 @@ | ||
const _ = require('lodash'); | ||
const should = require('should'); | ||
const sinon = require('sinon'); | ||
|
||
let SiftScienceClient; | ||
let v3HttpClient; | ||
let sandbox; | ||
|
||
before(() => { | ||
SiftScienceClient = require('../../../../lib'); | ||
v3HttpClient = require('../../../../lib/client/v3HttpClient'); | ||
|
||
sandbox = sinon.sandbox.create(); | ||
}); | ||
|
||
describe('lib', () => { | ||
describe('client', () => { | ||
describe('decisions', () => { | ||
describe('applyByAccountIdAndSessionId', () => { | ||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
const key = '123'; | ||
let siftScienceClient; | ||
|
||
before('create siftScienceClient', () => { | ||
siftScienceClient = new SiftScienceClient(key); | ||
}); | ||
|
||
describe('when called without accountId', () => { | ||
const accountId = null; | ||
const userId = '123'; | ||
const sessionId = '123'; | ||
|
||
it('should reject with an error', () => { | ||
return siftScienceClient.decisions.applyByAccountIdAndSessionId(accountId, userId, sessionId) | ||
.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 sessionId = '123'; | ||
const userId = null; | ||
|
||
it('should reject with an error', () => { | ||
return siftScienceClient.decisions.applyByAccountIdAndSessionId(accountId, userId, sessionId) | ||
.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 sessionId', () => { | ||
const accountId = '123'; | ||
const sessionId = null; | ||
const userId = '123'; | ||
|
||
it('should reject with an error', () => { | ||
return siftScienceClient.decisions.applyByAccountIdAndSessionId(accountId, userId, sessionId) | ||
.then(should.not.exist) | ||
.catch(err => { | ||
should.exist(err); | ||
err.should.be.instanceOf(Error); | ||
err.message.should.equal('sessionId is required'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when called with an accountId, userId and sessionId', () => { | ||
const accountId = '123'; | ||
const userId = '123'; | ||
const sessionId = '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.applyByAccountIdAndSessionId(accountId, userId, sessionId, 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}/sessions/${sessionId}/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 sessionId = '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.applyByAccountIdAndSessionId(accountId, userId, sessionId, 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}/sessions/${sessionId}/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 | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when call return an error', () => { | ||
const accountId = '123'; | ||
const userId = '123'; | ||
const sessionId = '123'; | ||
const error = new Error('Some error'); | ||
|
||
|
||
before('stub v3HttpClient.post()', () => { | ||
sandbox.stub(v3HttpClient, 'post') | ||
.returns(Promise.reject(error)); | ||
}); | ||
|
||
it('should also throw error if v3HttpClient.post() throw it', () => { | ||
return siftScienceClient.decisions.applyByAccountIdAndSessionId(accountId, userId, sessionId) | ||
.then(should.not.exist) | ||
.catch((err) => { | ||
should.exist(err); | ||
err.should.be.instanceOf(Error); | ||
err.message.should.equal(error.message); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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
Oops, something went wrong.