embark-area-51/test/accountParser.js

118 lines
3.7 KiB
JavaScript
Raw Normal View History

/*global describe, it*/
2018-05-14 18:04:26 +00:00
const assert = require('assert');
const sinon = require('sinon');
2018-07-19 07:34:56 +00:00
const utils = require('../lib/utils/utils');
2018-07-27 18:43:39 +00:00
const AccountParser = require('../lib/utils/accountParser');
2018-05-14 18:04:26 +00:00
let TestLogger = require('../lib/tests/test_logger.js');
2018-06-07 15:30:33 +00:00
const Web3 = require('web3');
2018-07-27 16:30:15 +00:00
const i18n = require('../lib/core/i18n/i18n.js');
2018-06-07 15:30:33 +00:00
i18n.setOrDetectLocale('en');
2018-05-14 18:04:26 +00:00
describe('embark.AccountParser', function () {
2018-05-14 18:04:26 +00:00
describe('#getAccount', function () {
const web3 = {
eth: {
accounts: {
privateKeyToAccount: sinon.stub().callsFake((key) => {
return {key};
})
2018-05-14 18:04:26 +00:00
}
2018-05-18 17:44:03 +00:00
},
utils: {
isHexStrict: sinon.stub().returns(true)
}
};
const testLogger = new TestLogger({});
2018-05-14 18:04:26 +00:00
it('should return one account with the key', function () {
const account = AccountParser.getAccount({
2018-05-14 18:04:26 +00:00
privateKey: 'myKey'
}, web3, testLogger);
2018-05-14 18:04:26 +00:00
2018-06-07 15:30:33 +00:00
assert.deepEqual(account, {key: '0xmyKey', hexBalance: null});
2018-05-14 18:04:26 +00:00
});
it('should return two accounts from the keys in the file', function () {
const account = AccountParser.getAccount({
2018-05-14 18:04:26 +00:00
privateKeyFile: 'test/keyFiles/twoKeys'
}, web3, testLogger);
2018-05-14 18:04:26 +00:00
assert.deepEqual(account, [
2018-06-07 15:30:33 +00:00
{key: '0xkey1', hexBalance: null},
{key: '0xkey2', hexBalance: null}
2018-05-14 18:04:26 +00:00
]);
});
it('should return one account from the mnemonic', function () {
const account = AccountParser.getAccount({
2018-05-14 18:04:26 +00:00
mnemonic: 'example exile argue silk regular smile grass bomb merge arm assist farm'
}, web3, testLogger);
2018-05-14 18:04:26 +00:00
assert.deepEqual(account,
2018-06-07 15:30:33 +00:00
[{key: "0xf942d5d524ec07158df4354402bfba8d928c99d0ab34d0799a6158d56156d986", hexBalance: null}]);
2018-05-14 18:04:26 +00:00
});
it('should return two accounts from the mnemonic using numAddresses', function () {
const account = AccountParser.getAccount({
2018-05-14 18:04:26 +00:00
mnemonic: 'example exile argue silk regular smile grass bomb merge arm assist farm',
numAddresses: 2
}, web3, testLogger);
2018-05-14 18:04:26 +00:00
assert.deepEqual(account,
[
2018-06-07 15:30:33 +00:00
{key: "0xf942d5d524ec07158df4354402bfba8d928c99d0ab34d0799a6158d56156d986", hexBalance: null},
{key: "0x88f37cfbaed8c0c515c62a17a3a1ce2f397d08bbf20dcc788b69f11b5a5c9791", hexBalance: null}
2018-05-14 18:04:26 +00:00
]);
});
it('should return nothing with bad config', function () {
const account = AccountParser.getAccount({
2018-05-14 18:04:26 +00:00
badConfig: 'not working'
}, web3, testLogger);
2018-05-14 18:04:26 +00:00
assert.strictEqual(account, null);
});
2018-06-07 15:30:33 +00:00
});
describe('getHexBalance', () => {
it('should return default if no balance', () => {
2018-07-19 07:34:56 +00:00
const hexBalance = utils.getHexBalanceFromString(null, Web3);
2018-06-07 15:30:33 +00:00
assert.strictEqual(hexBalance, 0xFFFFFFFFFFFFFFFFFF);
});
it('should return the balance string if already hexadecimal', () => {
2018-07-19 07:34:56 +00:00
const hexBalance = utils.getHexBalanceFromString('0xFFF', Web3);
2018-06-07 15:30:33 +00:00
assert.strictEqual(hexBalance, '0xFFF');
});
it('should convert to hex when decimal', () => {
2018-07-19 07:34:56 +00:00
const hexBalance = utils.getHexBalanceFromString('500', Web3);
2018-06-07 15:30:33 +00:00
assert.strictEqual(hexBalance, '0x1f4');
});
it('should convert to hex with eth string', () => {
2018-07-19 07:34:56 +00:00
const hexBalance = utils.getHexBalanceFromString('4ether', Web3);
2018-06-07 15:30:33 +00:00
assert.strictEqual(hexBalance, '0x3782dace9d900000');
});
it('should convert to hex with eth string with space', () => {
2018-07-19 07:34:56 +00:00
const hexBalance = utils.getHexBalanceFromString('673 shannon', Web3);
2018-05-14 18:04:26 +00:00
2018-06-07 15:30:33 +00:00
assert.strictEqual(hexBalance, '0x9cb1ed0a00');
});
it('should fail when string is not good', () => {
try {
2018-07-19 07:34:56 +00:00
utils.getHexBalanceFromString('nogood', Web3);
2018-06-07 15:30:33 +00:00
assert.fail('Should have failed at getHexBalance');
} catch (e) {
// Ok
}
});
2018-05-14 18:04:26 +00:00
});
});