1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

added unit tests for metamaskcontroller

This commit is contained in:
brunobar79 2018-07-16 19:36:08 -04:00
parent 41879a9835
commit e5512c306d
2 changed files with 87 additions and 3 deletions

View File

@ -561,10 +561,15 @@ module.exports = class MetamaskController extends EventEmitter {
return accounts return accounts
default: default:
throw new Error('MetamaskController - Unknown device') throw new Error('MetamaskController:connectHardware - Unknown device')
} }
} }
/**
* Check if the device is unlocked
*
* @returns {Promise<boolean>}
*/
async checkHardwareStatus (deviceName) { async checkHardwareStatus (deviceName) {
switch (deviceName) { switch (deviceName) {
@ -574,12 +579,19 @@ module.exports = class MetamaskController extends EventEmitter {
'Trezor Hardware' 'Trezor Hardware'
)[0] )[0]
if (!keyring) { if (!keyring) {
return false throw new Error('MetamaskController:checkHardwareStatus - Trezor Hardware keyring not found')
} }
return keyring.isUnlocked() return keyring.isUnlocked()
default:
throw new Error('MetamaskController:checkHardwareStatus - Unknown device')
} }
} }
/**
* Clear
*
* @returns {Promise<boolean>}
*/
async forgetDevice (deviceName) { async forgetDevice (deviceName) {
switch (deviceName) { switch (deviceName) {
@ -589,10 +601,12 @@ module.exports = class MetamaskController extends EventEmitter {
'Trezor Hardware' 'Trezor Hardware'
)[0] )[0]
if (!keyring) { if (!keyring) {
return false throw new Error('MetamaskController:forgetDevice - Trezor Hardware keyring not found')
} }
keyring.forgetDevice() keyring.forgetDevice()
return true return true
default:
throw new Error('MetamaskController:forgetDevice - Unknown device')
} }
} }

View File

@ -222,6 +222,76 @@ describe('MetaMaskController', function () {
}) })
}) })
describe('connectHardware', function () {
it('should throw if it receives an unknown device name', async function () {
try {
await metamaskController.connectHardware('Some random device name', 0)
} catch (e) {
assert.equal(e, 'Error: MetamaskController:connectHardware - Unknown device')
}
})
it('should add the Trezor Hardware keyring', async function () {
await metamaskController.connectHardware('trezor', 0).catch((e) => null)
const keyrings = await metamaskController.keyringController.getKeyringsByType(
'Trezor Hardware'
)
assert.equal(keyrings.length, 1)
})
})
describe('checkHardwareStatus', function () {
it('should throw if it receives an unknown device name', async function () {
try {
await metamaskController.checkHardwareStatus('Some random device name')
} catch (e) {
assert.equal(e, 'Error: MetamaskController:checkHardwareStatus - Unknown device')
}
})
it('should be locked by default', async function () {
await metamaskController.connectHardware('trezor', 0).catch((e) => null)
const status = await metamaskController.checkHardwareStatus('trezor')
assert.equal(status, false)
})
})
describe('forgetDevice', function () {
it('should throw if it receives an unknown device name', async function () {
try {
await metamaskController.forgetDevice('Some random device name')
} catch (e) {
assert.equal(e, 'Error: MetamaskController:forgetDevice - Unknown device')
}
})
it('should wipe all the keyring info', async function () {
await metamaskController.connectHardware('trezor', 0).catch((e) => null)
await metamaskController.forgetDevice('trezor')
const keyrings = await metamaskController.keyringController.getKeyringsByType(
'Trezor Hardware'
)
assert.deepEqual(keyrings[0].accounts, [])
assert.deepEqual(keyrings[0].page, 0)
assert.deepEqual(keyrings[0].isUnlocked(), false)
})
})
describe('unlockTrezorAccount', function () {
it('should set accountToUnlock in the keyring', async function () {
await metamaskController.connectHardware('trezor', 0).catch((e) => null)
const accountToUnlock = 10
await metamaskController.unlockTrezorAccount(accountToUnlock).catch((e) => null)
const keyrings = await metamaskController.keyringController.getKeyringsByType(
'Trezor Hardware'
)
assert.equal(keyrings[0].unlockedAccount, accountToUnlock)
})
})
describe('#setCustomRpc', function () { describe('#setCustomRpc', function () {
const customRPC = 'https://custom.rpc/' const customRPC = 'https://custom.rpc/'
let rpcTarget let rpcTarget