forked from bitpay/bitcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
274 additions
and
5 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
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
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
148 changes: 148 additions & 0 deletions
148
packages/bitcore-wallet-service/test/integration/oneInch.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,148 @@ | ||
'use strict'; | ||
|
||
const chai = require('chai'); | ||
const should = chai.should(); | ||
const { WalletService } = require('../../ts_build/lib/server'); | ||
const TestData = require('../testdata'); | ||
const helpers = require('./helpers'); | ||
|
||
let config = require('../../ts_build/config.js'); | ||
let server, wallet, fakeRequest, req; | ||
|
||
describe('OneInch integration', () => { | ||
before((done) => { | ||
helpers.before((res) => { | ||
done(); | ||
}); | ||
}); | ||
beforeEach((done) => { | ||
config.suspendedChains = []; | ||
config.oneInch = { | ||
api: 'xxxx', | ||
referrerAddress: 'referrerAddress', | ||
referrerFee: 'referrerFee1' | ||
} | ||
|
||
fakeRequest = { | ||
post: (_url, _opts, _cb) => { return _cb(null, { body: 'data'}) }, | ||
get: (_url, _opts, _cb) => { return _cb(null, { body: 'data'}) }, | ||
}; | ||
|
||
helpers.beforeEach((res) => { | ||
helpers.createAndJoinWallet(1, 1, (s, w) => { | ||
wallet = w; | ||
const priv = TestData.copayers[0].privKey_1H_0; | ||
const sig = helpers.signMessage('hello world', priv); | ||
|
||
WalletService.getInstanceWithAuth({ | ||
// test assumes wallet's copayer[0] is TestData's copayer[0] | ||
copayerId: wallet.copayers[0].id, | ||
message: 'hello world', | ||
signature: sig, | ||
clientVersion: 'bwc-2.0.0', | ||
walletId: '123', | ||
}, (err, s) => { | ||
should.not.exist(err); | ||
server = s; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
after((done) => { | ||
helpers.after(done); | ||
}); | ||
|
||
describe('#oneInchGetReferrerFee', () => { | ||
beforeEach(() => { | ||
req = {} | ||
}); | ||
|
||
it('should get referrel fee if it is defined in config', () => { | ||
server.request = fakeRequest; | ||
server.oneInchGetReferrerFee(req).then(data => { | ||
should.exist(data); | ||
data.referrerFee.should.equal('referrerFee1'); | ||
}).catch(err => { | ||
should.not.exist(err); | ||
}); | ||
}); | ||
|
||
it('should return error if oneInch is commented in config', () => { | ||
config.oneInch = undefined; | ||
|
||
server.request = fakeRequest; | ||
server.oneInchGetReferrerFee(req).then(data => { | ||
should.not.exist(data); | ||
}).catch(err => { | ||
should.exist(err); | ||
err.message.should.equal('1Inch missing credentials'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#oneInchGetSwap', () => { | ||
beforeEach(() => { | ||
req = { | ||
headers: {}, | ||
body: { | ||
fromTokenAddress: 'fromTokenAddress1', | ||
toTokenAddress: 'toTokenAddress1', | ||
amount: 100, | ||
fromAddress: 'fromAddress1', | ||
slippage: 0.5, | ||
destReceiver: 'destReceiver1' | ||
} | ||
} | ||
}); | ||
|
||
it('should work properly if req is OK', () => { | ||
server.request = fakeRequest; | ||
server.oneInchGetSwap(req).then(data => { | ||
should.exist(data); | ||
}).catch(err => { | ||
should.not.exist(err); | ||
}); | ||
}); | ||
|
||
it('should return error if there is some missing arguments', () => { | ||
delete req.body.fromTokenAddress; | ||
|
||
server.request = fakeRequest; | ||
server.oneInchGetSwap(req).then(data => { | ||
should.not.exist(data); | ||
}).catch(err => { | ||
should.exist(err); | ||
err.message.should.equal('oneInchGetSwap request missing arguments'); | ||
}); | ||
}); | ||
|
||
it('should return error if request returns error', () => { | ||
req.body.fromTokenAddress = 'fromTokenAddress1'; | ||
const fakeRequest2 = { | ||
post: (_url, _opts, _cb) => { return _cb(new Error('Error')) }, | ||
get: (_url, _opts, _cb) => { return _cb(new Error('Error')) } | ||
}; | ||
|
||
server.request = fakeRequest2; | ||
server.oneInchGetSwap(req).then(data => { | ||
should.not.exist(data); | ||
}).catch(err => { | ||
should.exist(err); | ||
err.message.should.equal('Error'); | ||
}); | ||
}); | ||
|
||
it('should return error if oneInch is commented in config', () => { | ||
config.oneInch = undefined; | ||
|
||
server.request = fakeRequest; | ||
server.oneInchGetSwap(req).then(data => { | ||
should.not.exist(data); | ||
}).catch(err => { | ||
should.exist(err); | ||
err.message.should.equal('1Inch missing credentials'); | ||
}); | ||
}); | ||
}); | ||
}); |