From 41879a983582c832fe3b6b19df3c057a1cc13d2a Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Mon, 16 Jul 2018 16:08:19 -0400 Subject: added test for preferences controller --- .../app/controllers/preferences-controller-test.js | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'test') diff --git a/test/unit/app/controllers/preferences-controller-test.js b/test/unit/app/controllers/preferences-controller-test.js index e5e751b57..e055500b1 100644 --- a/test/unit/app/controllers/preferences-controller-test.js +++ b/test/unit/app/controllers/preferences-controller-test.js @@ -52,6 +52,31 @@ describe('preferences controller', function () { }) }) + describe('removeAddress', function () { + it('should remove an address from state', function () { + preferencesController.setAddresses([ + '0xda22le', + '0x7e57e2', + ]) + + preferencesController.removeAddress('0xda22le') + + assert.equal(preferencesController.store.getState().identities['0xda22le'], undefined) + }) + + it('should switch accounts if the selected address is removed', function () { + preferencesController.setAddresses([ + '0xda22le', + '0x7e57e2', + ]) + + preferencesController.setSelectedAddress('0x7e57e2') + preferencesController.removeAddress('0x7e57e2') + + assert.equal(preferencesController.getSelectedAddress(), '0xda22le') + }) + }) + describe('setAccountLabel', function () { it('should update a label for the given account', function () { preferencesController.setAddresses([ -- cgit From e5512c306ded1d2a521a0ba0d2c3cdd5878e53bb Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Mon, 16 Jul 2018 19:36:08 -0400 Subject: added unit tests for metamaskcontroller --- .../app/controllers/metamask-controller-test.js | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'test') diff --git a/test/unit/app/controllers/metamask-controller-test.js b/test/unit/app/controllers/metamask-controller-test.js index 0dda4609b..bef126f1f 100644 --- a/test/unit/app/controllers/metamask-controller-test.js +++ b/test/unit/app/controllers/metamask-controller-test.js @@ -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 () { const customRPC = 'https://custom.rpc/' let rpcTarget -- cgit From de4265c629f8e68d882c2ded0e20417327cf4d2f Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Tue, 17 Jul 2018 01:17:18 -0400 Subject: added more unit tests --- .../app/controllers/metamask-controller-test.js | 57 +++++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/unit/app/controllers/metamask-controller-test.js b/test/unit/app/controllers/metamask-controller-test.js index bef126f1f..a08371f86 100644 --- a/test/unit/app/controllers/metamask-controller-test.js +++ b/test/unit/app/controllers/metamask-controller-test.js @@ -233,10 +233,12 @@ describe('MetaMaskController', function () { }) it('should add the Trezor Hardware keyring', async function () { + sinon.spy(metamaskController.keyringController, 'addNewKeyring') await metamaskController.connectHardware('trezor', 0).catch((e) => null) const keyrings = await metamaskController.keyringController.getKeyringsByType( 'Trezor Hardware' ) + assert.equal(metamaskController.keyringController.addNewKeyring.getCall(0).args, 'Trezor Hardware') assert.equal(keyrings.length, 1) }) @@ -281,15 +283,66 @@ describe('MetaMaskController', function () { }) describe('unlockTrezorAccount', function () { - it('should set accountToUnlock in the keyring', async function () { + let accountToUnlock + let windowOpenStub + let addNewAccountStub + let getAccountsStub + beforeEach(async function () { + accountToUnlock = 10 + windowOpenStub = sinon.stub(window, 'open') + windowOpenStub.returns(noop) + + addNewAccountStub = sinon.stub(metamaskController.keyringController, 'addNewAccount') + addNewAccountStub.returns({}) + + getAccountsStub = sinon.stub(metamaskController.keyringController, 'getAccounts') + // Need to return different address to mock the behavior of + // adding a new account from the keyring + getAccountsStub.onCall(0).returns(Promise.resolve(['0x1'])) + getAccountsStub.onCall(1).returns(Promise.resolve(['0x2'])) + getAccountsStub.onCall(2).returns(Promise.resolve(['0x3'])) + getAccountsStub.onCall(3).returns(Promise.resolve(['0x4'])) + sinon.spy(metamaskController.preferencesController, 'setAddresses') + sinon.spy(metamaskController.preferencesController, 'setSelectedAddress') + sinon.spy(metamaskController.preferencesController, 'setAccountLabel') await metamaskController.connectHardware('trezor', 0).catch((e) => null) - const accountToUnlock = 10 await metamaskController.unlockTrezorAccount(accountToUnlock).catch((e) => null) + }) + + afterEach(function () { + metamaskController.keyringController.addNewAccount.restore() + window.open.restore() + }) + + it('should set accountToUnlock in the keyring', async function () { const keyrings = await metamaskController.keyringController.getKeyringsByType( 'Trezor Hardware' ) assert.equal(keyrings[0].unlockedAccount, accountToUnlock) }) + + + it('should call keyringController.addNewAccount', async function () { + assert(metamaskController.keyringController.addNewAccount.calledOnce) + }) + + it('should call keyringController.getAccounts ', async function () { + assert(metamaskController.keyringController.getAccounts.called) + }) + + it('should call preferencesController.setAddresses', async function () { + assert(metamaskController.preferencesController.setAddresses.calledOnce) + }) + + it('should call preferencesController.setSelectedAddress', async function () { + assert(metamaskController.preferencesController.setSelectedAddress.calledOnce) + }) + + it('should call preferencesController.setAccountLabel', async function () { + assert(metamaskController.preferencesController.setAccountLabel.calledOnce) + }) + + }) describe('#setCustomRpc', function () { -- cgit From e89350b19fdac56968303e5c48806a4605fb4b22 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Tue, 17 Jul 2018 01:44:28 -0400 Subject: added tests for removeAccount --- .../app/controllers/metamask-controller-test.js | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'test') diff --git a/test/unit/app/controllers/metamask-controller-test.js b/test/unit/app/controllers/metamask-controller-test.js index a08371f86..9164fe246 100644 --- a/test/unit/app/controllers/metamask-controller-test.js +++ b/test/unit/app/controllers/metamask-controller-test.js @@ -485,6 +485,39 @@ describe('MetaMaskController', function () { }) }) + describe('#removeAccount', function () { + let ret + const addressToRemove = '0x1' + + beforeEach(async function () { + sinon.stub(metamaskController.preferencesController, 'removeAddress') + sinon.stub(metamaskController.accountTracker, 'removeAccount') + sinon.stub(metamaskController.keyringController, 'removeAccount') + + ret = await metamaskController.removeAccount(addressToRemove) + + }) + + afterEach(function () { + metamaskController.keyringController.removeAccount.restore() + metamaskController.accountTracker.removeAccount.restore() + metamaskController.preferencesController.removeAddress.restore() + }) + + it('should call preferencesController.removeAddress', async function () { + assert(metamaskController.preferencesController.removeAddress.calledWith(addressToRemove)) + }) + it('should call accountTracker.removeAccount', async function () { + assert(metamaskController.accountTracker.removeAccount.calledWith(addressToRemove)) + }) + it('should call keyringController.removeAccount', async function () { + assert(metamaskController.keyringController.removeAccount.calledWith(addressToRemove)) + }) + it('should return address', async function () { + assert.equal(ret, '0x1') + }) + }) + describe('#clearSeedWordCache', function () { it('should have set seed words', function () { -- cgit From aa5a987765677b4945e9eefe03cae8dcc93318cd Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Tue, 17 Jul 2018 21:54:04 -0400 Subject: added some e2e tests --- test/e2e/beta/from-import-beta-ui.spec.js | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'test') diff --git a/test/e2e/beta/from-import-beta-ui.spec.js b/test/e2e/beta/from-import-beta-ui.spec.js index b396dc5b9..938d22cd9 100644 --- a/test/e2e/beta/from-import-beta-ui.spec.js +++ b/test/e2e/beta/from-import-beta-ui.spec.js @@ -321,4 +321,51 @@ describe('Using MetaMask with an existing account', function () { }) }) + describe('Connects to a Hardware wallet', () => { + it('choose Connect Hardware Wallet from the account menu', async () => { + await driver.findElement(By.css('.account-menu__icon')).click() + await delay(regularDelayMs) + + const [connectAccount] = await findElements(driver, By.xpath(`//div[contains(text(), 'Connect Hardware Wallet')]`)) + await connectAccount.click() + await delay(regularDelayMs) + }) + + it('should open the TREZOR Connect popup', async () => { + const connectButtons = await findElements(driver, By.xpath(`//button[contains(text(), 'Connect to Trezor')]`)) + await connectButtons[0].click() + await delay(regularDelayMs) + const allWindows = await driver.getAllWindowHandles() + switch (process.env.SELENIUM_BROWSER) { + case 'chrome': + assert.equal(allWindows.length, 2) + break + default: + assert.equal(allWindows.length, 1) + } + }) + + it('should show the "Browser not supported" screen for non Chrome browsers', async () => { + if (process.env.SELENIUM_BROWSER !== 'chrome') { + const title = await findElements(driver, By.xpath(`//h3[contains(text(), 'Bummer! Your Browser is not supported...')]`)) + assert.equal(title.length, 1) + + const downloadChromeButtons = await findElements(driver, By.xpath(`//button[contains(text(), 'Download Google Chrome')]`)) + assert.equal(downloadChromeButtons.length, 1) + + await downloadChromeButtons[0].click() + await delay(regularDelayMs) + + const [newUITab, downloadChromeTab] = await driver.getAllWindowHandles() + + await driver.switchTo().window(downloadChromeTab) + await delay(regularDelayMs) + const tabUrl = await driver.getCurrentUrl() + assert.equal(tabUrl, 'https://www.google.com/chrome/') + await driver.close() + await delay(regularDelayMs) + await driver.switchTo().window(newUITab) + } + }) + }) }) -- cgit From cbb14f1d5e50c10865838a98452ecfb4b6cb8d6a Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Tue, 17 Jul 2018 21:57:19 -0400 Subject: fix browser not supported screen --- test/e2e/beta/from-import-beta-ui.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/e2e/beta/from-import-beta-ui.spec.js b/test/e2e/beta/from-import-beta-ui.spec.js index 938d22cd9..32f57b157 100644 --- a/test/e2e/beta/from-import-beta-ui.spec.js +++ b/test/e2e/beta/from-import-beta-ui.spec.js @@ -347,7 +347,7 @@ describe('Using MetaMask with an existing account', function () { it('should show the "Browser not supported" screen for non Chrome browsers', async () => { if (process.env.SELENIUM_BROWSER !== 'chrome') { - const title = await findElements(driver, By.xpath(`//h3[contains(text(), 'Bummer! Your Browser is not supported...')]`)) + const title = await findElements(driver, By.xpath(`//h3[contains(text(), 'Your Browser is not supported...')]`)) assert.equal(title.length, 1) const downloadChromeButtons = await findElements(driver, By.xpath(`//button[contains(text(), 'Download Google Chrome')]`)) -- cgit