Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Introduce mocha + chai and implement some tests with it #225

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = {
extension: ['js'],
recursive: true,
exit: true,
inspect: true,
file: 'tests/init.js',
timeout: 10000
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"build:web:minify": "node-minify --compressor uglify-es --input './dist/dvf.js' --output './dist/dvf.min.js'",
"build:web:hash": "hash-filename ./dist/dvf*.js;del ./dist/{dvf.js,dvf.min.js}",
"build:web:run": "browserify --debug ./src/dvf.js --standalone EFX | browser-run --port 2222",
"test": "env-cmd -f ./env/test jest --testTimeout 10000",
"test": "env-cmd -f ./env/test mocha --config .mocharc.js tests/",
"test:watch": "env-cmd -f ./env/test jest --watch --testTimeout 10000 --verbose true",
"test:coverage": "env-cmd -f ./env/test jest --testTimeout 10000 --coverage"
},
Expand Down Expand Up @@ -58,14 +58,18 @@
"@truffle/hdwallet-provider": "^1.1.0",
"browser-run": "^5.0.0",
"browserify": "^16.2.2",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"del": "^3.0.0",
"del-cli": "^1.1.0",
"env-cmd": "^10.1.0",
"hash-filename": "^1.0.4",
"jest": "^26.4.2",
"mocha": "^8.3.2",
"mustache": "^4.0.0",
"nock": "^13.0.4",
"node-minify": "^3.6.0",
"sinon": "^10.0.0",
"solc": "^0.4.24",
"standard": "^11.0.1",
"supertest": "^4.0.2",
Expand Down
11 changes: 3 additions & 8 deletions src/dvf.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ module.exports = async (web3, userConfig = {}, sw) => {
dvf.config = Object.assign({}, defaultConfig, userConfig)

// ethfinex exchange config
const exchangeConf = dvf.config.autoLoadExchangeConf ?
await dvf.getConfig()
:
{}
const exchangeConf = dvf.config.autoLoadExchangeConf ? await dvf.getConfig() : {}

// user config has priority
dvf.config = Object.assign({}, defaultConfig, exchangeConf, userConfig)
Expand Down Expand Up @@ -58,8 +55,7 @@ module.exports = async (web3, userConfig = {}, sw) => {

try {
chainId = chainId || (await web3.eth.net.getId())
}
catch(e) {
} catch (e) {
console.log('error getting chainId')
}

Expand All @@ -71,8 +67,7 @@ module.exports = async (web3, userConfig = {}, sw) => {
if (!dvf.get('account')) {
console.warn('Please specify a valid account or account index')
}
}
else if (dvf.config.address){
} else if (dvf.config.address) {
dvf.set('account', dvf.config.address.toLowerCase())
}

Expand Down
2 changes: 1 addition & 1 deletion tests/00.complete-path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe.skip('00 - Complete Path', () => {

let dvf, address, privateKey, setupData, starkSignature, nonce

beforeAll(async () => {
before(async () => {
setupData = await setup()
address = setupData.account.address
privateKey = setupData.account.privateKey
Expand Down
89 changes: 89 additions & 0 deletions tests/cancelOrder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const nock = require('nock')
const sinon = require('sinon')
const mockGetConf = require('./fixtures/getConf')

describe('dvf.cancelOrder', () => {
before(async () => {
nock.cleanAll()
mockGetConf()
})

beforeEach(() => {
nock.cleanAll()
})

it('Posts to cancel order API and gets response', async () => {
const orderId = '1'
const apiResponse = { cancelOrder: 'success' }

const payloadValidator = sinon.spy(body => {
body.orderId.should.equal(orderId)

;(typeof body.orderId).should.equal('string')
;(typeof body.nonce).should.equal('string')
;(typeof body.signature).should.equal('string')

return true
})

nock(dvf.config.api)
.post('/v1/trading/w/cancelOrder', payloadValidator)
.reply(200, apiResponse)

const response = await dvf.cancelOrder(orderId)

payloadValidator.called.should.equal(true)
response.should.deep.equal(apiResponse)
})

it('Posts to cancel order API with { cid } and gets response', async () => {
const cid = 'cid-1'
const apiResponse = { cancelOrder: 'success' }

const payloadValidator = sinon.spy(body => {
body.cid.should.equal(cid)
;(typeof body.cid).should.equal('string')
;(typeof body.nonce).should.equal('string')
;(typeof body.signature).should.equal('string')

return true
})

nock(dvf.config.api)
.post('/v1/trading/w/cancelOrder', payloadValidator)
.reply(200, apiResponse)

const response = await dvf.cancelOrder({ cid })

payloadValidator.called.should.equal(true)
response.should.deep.equal(apiResponse)
})

it('Posts to cancel order API and gets error response', async () => {
const orderId = '1'
const apiErrorResponse = {
statusCode: 422,
error: 'Unprocessable Entity',
message:
'Please contact support if you believe there should not be an error here',
details: {
error: {
type: 'DVFError',
message: 'STARK_SIGNATURE_VERIFICATION_ERROR'
}
}
}
const payloadValidator = sinon.spy(() => true)

nock(dvf.config.api)
.post('/v1/trading/w/cancelOrder', payloadValidator)
.reply(422, apiErrorResponse)

try {
await dvf.cancelOrder(orderId)
} catch (e) {
e.error.should.deep.equal(apiErrorResponse)
payloadValidator.called.should.equal(true)
}
})
})
39 changes: 39 additions & 0 deletions tests/fixtures/deposit_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// deposit error for no token balance

const error = 'Error: Transaction has been reverted by the EVM:'

const web3DepositFailure = {
blockHash:
'0x9ce805a3b290df33697a06b1a0f589ab162d76301e09b0ed017401bdf53ff957',
blockNumber: 7774151,
contractAddress: null,
cumulativeGasUsed: 87847,
from: '0x2052ee5906cd4281d44222cf57503025f47ae32b',
gasUsed: 59011,
logsBloom:
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: false,
to: '0xa9f9cc1189b9d6051b26467b29629787c671905d',
transactionHash:
'0xbfc0dbbc1308ec2e2cd0d8c15c07d50687e9731ae486f5ba9e475848b745a949',
transactionIndex: 1,
events: {}
}

const web3DepositSuccess = {
blockHash:
'0x593704366c0f84c02fbfc93f2799978c3d2de03604c520fe203cf2244aa02a81',
blockNumber: 7774183,
contractAddress: null,
cumulativeGasUsed: 68425,
from: '0x2052ee5906cd4281d44222cf57503025f47ae32b',
gasUsed: 68425,
logsBloom:
'0x80000000400100000200000001000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000200000080000800000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000010000000000000000000000000000000000000000020000000000000000000000000100000020000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000008200000000000000000000010800000000000000000001000000000000000000000000000000000000000',
status: true,
to: '0xa9f9cc1189b9d6051b26467b29629787c671905d',
transactionHash:
'0x63851308c2f14cb27e131e72400cc6b7a7c0f48b7daf5f85c68c2ef510ae8d3f',
transactionIndex: 0,
events: { '0': [Object], '1': [Object], LogDeposit: [Object] }
}
18 changes: 18 additions & 0 deletions tests/fixtures/feeRate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const nock = require('nock')

module.exports = () => {
const httpResponse = {
address: '0x65CEEE596B2aba52Acc09f7B6C81955C1DB86404',
timestamp: 1568959208939,
fees: {
small: { threshold: 0, feeBps: 25 },
medium: { threshold: 500, feeBps: 21 },
large: { threshold: 2000, feeBps: 20 }
},
signature: '0x52f18b47494e465aa4ed0f0f123fae4d40d3ac0862b61862e6cc8e5a119dbfe1061a4ee381092a10350185071f4829dbfd6c5f2e26df76dee0593cbe3cbd87321b'
}

nock('https://api.deversifi.com')
.get('/api/v1/feeRate/' + '0x65CEEE596B2aba52Acc09f7B6C81955C1DB86404')
.reply(200, httpResponse)
}
123 changes: 123 additions & 0 deletions tests/fixtures/getConf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const nock = require('nock')

module.exports = () => {
const getConfResponse = {
starkKeyHex: '0x011869c13b32ab9b7ec84e2b31c1de58baaaa6bbb2443a33bbad8df739a6e957',
DVF: {
exchangeSymbols: ['ETH:USDT', 'ZRX:USDT', 'ZRX:ETH'],
exchangeAddress: '0xBd25cD867C304F079E696CBE44D958f3d3B683ba',
starkExContractAddress: '0xA9F9cC1189b9d6051b26467b29629787C671905d',
tempStarkVaultId: 1,
defaultFeeRate: 0.0025
},
tokenRegistry: {
ETH: {
decimals: 18,
quantization: 10000000000,
minOrderSize: 0.05,
settleSpread: 0,
starkTokenId:
'0xb333e3142fe16b78628f19bb15afddaef437e72d6d7f5c6c20c6801a27fba6'
},
USDT: {
decimals: 6,
quantization: 1,
minOrderSize: 10,
settleSpread: 0,
starkTokenId:
'0x180bef8ae3462e919489763b84dc1dc700c45a249dec4d1136814a639f2dd7b',
tokenAddress: '0x4c5f66596197a86fb30a2435e2ef4ddcb39342c9'
},
ZRX: {
decimals: 18,
quantization: 10000000000,
minOrderSize: 20,
settleSpread: 0,
starkTokenId:
'0x3901ee6a6c5ac0f6e284f4273b961b7e9f29d25367d31d90b75820473a202f7',
tokenAddress: '0xcd077abedd831a3443ffbe24fb76661bbb17eb69'
},
BTC: {
decimals: 18,
quantization: 10000000000,
minOrderSize: 0.0004,
settleSpread: 0,
starkTokenId:
'0x21ef21d6b234cd669edd702dd3d1d017be888337010b950ae3679eb4194b4bc',
tokenAddress: '0x40d8978500bf68324a51533cd6a21e3e59be324a'
}
}
}

nock('https://api.stg.deversifi.com')
.post('/v1/trading/r/getConf', {})
.reply(200, getConfResponse)

const mockGasResponse = {
cheap: 700000000,
average: 600000000,
fast: 500000000
}

nock('https://api.stg.deversifi.com')
.post('/v1/trading/r/getGasPrice', body => {
return true
})
.reply(200, mockGasResponse)

const getUserConfResponse = {
DVF: {
exchangeSymbols: ['ETH:USDT', 'ZRX:USDT', 'ZRX:ETH'],
exchangeAddress: '0xBd25cD867C304F079E696CBE44D958f3d3B683ba',
starkExContractAddress: '0xA9F9cC1189b9d6051b26467b29629787C671905d',
tempStarkVaultId: 1,
defaultFeeRate: 0.0025
},
tokenRegistry: {
ETH: {
decimals: 18,
quantization: 10000000000,
minOrderSize: 0.1,
starkTokenId:
'0xb333e3142fe16b78628f19bb15afddaef437e72d6d7f5c6c20c6801a27fba6',
starkVaultId: 1000001
},
USDT: {
decimals: 6,
quantization: 1,
minOrderSize: 25,
settleSpread: 0,
starkTokenId:
'0x180bef8ae3462e919489763b84dc1dc700c45a249dec4d1136814a639f2dd7b',
tokenAddress: '0x4c5f66596197a86fb30a2435e2ef4ddcb39342c9',
starkVaultId: 1000002
},
ZRX: {
decimals: 18,
quantization: 10000000000,
minOrderSize: 40,
starkTokenId:
'0x3901ee6a6c5ac0f6e284f4273b961b7e9f29d25367d31d90b75820473a202f7',
tokenAddress: '0xcd077abedd831a3443ffbe24fb76661bbb17eb69',
starkVaultId: 1000003
},
BTC: {
decimals: 18,
quantization: 10000000000,
minOrderSize: 0.0001,
starkTokenId:
'0x21ef21d6b234cd669edd702dd3d1d017be888337010b950ae3679eb4194b4bc',
tokenAddress: '0x40d8978500bf68324a51533cd6a21e3e59be324a',
starkVaultId: 1000004
}
},
isRegistered: true,
ethAddress: '0xf858c2f2ac6b96df8c801bce90a3124a52d1915a'
}

nock('https://api.stg.deversifi.com')
.post('/v1/trading/r/getUserConf', body => {
return true
})
.reply(200, getUserConfResponse)
}
Loading