2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
import configureMockStore from 'redux-mock-store';
|
2022-08-03 22:30:43 +02:00
|
|
|
import { fireEvent, screen } from '@testing-library/react';
|
|
|
|
import { renderWithProvider } from '../../../../test/lib/render-helpers';
|
2021-03-16 22:00:08 +01:00
|
|
|
import AccountMenu from '.';
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Account Menu', () => {
|
2020-01-30 20:34:45 +01:00
|
|
|
const mockStore = {
|
|
|
|
metamask: {
|
|
|
|
provider: {
|
|
|
|
type: 'test',
|
|
|
|
},
|
|
|
|
preferences: {
|
|
|
|
useNativeCurrencyAsPrimaryCurrency: true,
|
|
|
|
},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const store = configureMockStore()(mockStore);
|
2020-01-30 20:34:45 +01:00
|
|
|
|
|
|
|
const props = {
|
|
|
|
isAccountMenuOpen: true,
|
Permission System 2.0 (#12243)
# Permission System 2.0
## Background
This PR migrates the extension permission system to [the new `PermissionController`](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions).
The original permission system, based on [`rpc-cap`](https://github.com/MetaMask/rpc-cap), introduced [`ZCAP-LD`](https://w3c-ccg.github.io/zcap-ld/)-like permissions to our JSON-RPC stack.
We used it to [implement](https://github.com/MetaMask/metamask-extension/pull/7004) what we called "LoginPerSite" in [version 7.7.0](https://github.com/MetaMask/metamask-extension/releases/tag/v7.7.0) of the extension, which enabled the user to choose which accounts, if any, should be exposed to each dapp.
While that was a worthwhile feature in and of itself, we wanted a permission _system_ in order to enable everything we are going to with Snaps.
Unfortunately, the original permission system was difficult to use, and necessitated the creation of the original `PermissionsController` (note the "s"), which was more or less a wrapper for `rpc-cap`.
With this PR, we shake off the yoke of the original permission system, in favor of the modular, self-contained, ergonomic, and more mature permission system 2.0.
Note that [the `PermissionController` readme](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions/README.md) explains how the new permission system works.
The `PermissionController` and `SubjectMetadataController` are currently shipped via `@metamask/snap-controllers`. This is a temporary state of affairs, and we'll move them to `@metamask/controllers` once they've landed in prod.
## Changes in Detail
First, the changes in this PR are not as big as they seem. Roughly half of the additions in this PR are fixtures in the test for the new migration (number 68), and a significant portion of the remaining ~2500 lines are due to find-and-replace changes in other test fixtures and UI files.
- The extension `PermissionsController` has been deleted, and completely replaced with the new `PermissionController` from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers).
- The original `PermissionsController` "domain metadata" functionality is now managed by the new `SubjectMetadataController`, also from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers).
- The permission activity and history log controller has been renamed `PermissionLogController` and has its own top-level state key, but is otherwise functionally equivalent to the existing implementation.
- Migration number 68 has been added to account for the new state changes.
- The tests in `app/scripts/controllers/permissions` have been migrated from `mocha` to `jest`.
Reviewers should focus their attention on the following files:
- `app/scripts/`
- `metamask-controller.js`
- This is where most of the integration work for the new `PermissionController` occurs.
Some functions that were internal to the original controller were moved here.
- `controllers/permissions/`
- `selectors.js`
- These selectors are for `ControllerMessenger` selector subscriptions. The actual subscriptions occur in `metamask-controller.js`. See the `ControllerMessenger` implementation for details.
- `specifications.js`
- The caveat and permission specifications are required by the new `PermissionController`, and are used to specify the `eth_accounts` permission and its JSON-RPC method implementation.
See the `PermissionController` readme for details.
- `migrations/068.js`
- The new state should be cross-referenced with the controllers that manage it.
The accompanying tests should also be thoroughly reviewed.
Some files may appear new but have just moved and/or been renamed:
- `app/scripts/lib/rpc-method-middleware/handlers/request-accounts.js`
- This was previously implemented in `controllers/permissions/permissionsMethodMiddleware.js`.
- `test/mocks/permissions.js`
- A truncated version of `test/mocks/permission-controller.js`.
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2021-12-07 04:16:49 +01:00
|
|
|
addressConnectedSubjectMap: {},
|
2020-01-30 20:34:45 +01:00
|
|
|
accounts: [
|
|
|
|
{
|
2021-04-16 17:05:13 +02:00
|
|
|
address: '0x00',
|
2020-01-30 20:34:45 +01:00
|
|
|
name: 'Account 1',
|
|
|
|
balance: '0x0',
|
|
|
|
},
|
|
|
|
{
|
2021-04-16 17:05:13 +02:00
|
|
|
address: '0x1',
|
2020-01-30 20:34:45 +01:00
|
|
|
name: 'Imported Account 1',
|
|
|
|
balance: '0x0',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
keyrings: [
|
|
|
|
{
|
|
|
|
type: 'HD Key Tree',
|
2020-11-03 00:41:28 +01:00
|
|
|
accounts: ['0xAdress'],
|
2020-01-30 20:34:45 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'Simple Key Pair',
|
2021-04-16 17:05:13 +02:00
|
|
|
accounts: ['0x1'],
|
2020-01-30 20:34:45 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
prevIsAccountMenuOpen: false,
|
|
|
|
lockMetamask: sinon.spy(),
|
|
|
|
showAccountDetail: sinon.spy(),
|
|
|
|
showRemoveAccountConfirmationModal: sinon.spy(),
|
|
|
|
toggleAccountMenu: sinon.spy(),
|
|
|
|
history: {
|
|
|
|
push: sinon.spy(),
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2022-08-03 22:30:43 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
renderWithProvider(<AccountMenu.WrappedComponent {...props} />, store);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
afterEach(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
props.toggleAccountMenu.resetHistory();
|
|
|
|
props.history.push.resetHistory();
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Render Content', () => {
|
|
|
|
it('returns account name from identities', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const accountName = screen.queryAllByTestId('account-menu__account');
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(accountName).toHaveLength(2);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('renders user preference currency display balance from account balance', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const accountBalance = screen.queryAllByTestId('account-menu__balance');
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(accountBalance).toHaveLength(2);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('simulate click', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const click = screen.getAllByTestId('account-menu__account');
|
|
|
|
fireEvent.click(click[0]);
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(props.showAccountDetail.calledOnce).toStrictEqual(true);
|
2021-04-16 17:05:13 +02:00
|
|
|
expect(props.showAccountDetail.getCall(0).args[0]).toStrictEqual('0x00');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('render imported account label', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const importedAccount = screen.getByText('Imported');
|
|
|
|
expect(importedAccount).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Log Out', () => {
|
|
|
|
it('logout', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const logout = screen.getByText('Lock');
|
|
|
|
expect(logout).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('simulate click', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const logout = screen.getByText('Lock');
|
|
|
|
fireEvent.click(logout);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(props.lockMetamask.calledOnce).toStrictEqual(true);
|
|
|
|
expect(props.history.push.getCall(0).args[0]).toStrictEqual('/');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Create Account', () => {
|
|
|
|
it('renders create account item', () => {
|
2022-08-08 21:01:38 +02:00
|
|
|
const createAccount = screen.getByText('Create account');
|
2022-08-03 22:30:43 +02:00
|
|
|
expect(createAccount).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('calls toggle menu and push new-account route to history', () => {
|
2022-08-08 21:01:38 +02:00
|
|
|
const createAccount = screen.getByText('Create account');
|
2022-08-03 22:30:43 +02:00
|
|
|
fireEvent.click(createAccount);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(props.toggleAccountMenu.calledOnce).toStrictEqual(true);
|
|
|
|
expect(props.history.push.getCall(0).args[0]).toStrictEqual(
|
|
|
|
'/new-account',
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Import Account', () => {
|
|
|
|
it('renders import account item', () => {
|
2022-08-08 21:01:38 +02:00
|
|
|
const importAccount = screen.getByText('Import account');
|
2022-08-03 22:30:43 +02:00
|
|
|
expect(importAccount).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('calls toggle menu and push /new-account/import route to history', () => {
|
2022-08-08 21:01:38 +02:00
|
|
|
const importAccount = screen.getByText('Import account');
|
2022-08-03 22:30:43 +02:00
|
|
|
fireEvent.click(importAccount);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(props.toggleAccountMenu.calledOnce).toStrictEqual(true);
|
|
|
|
expect(props.history.push.getCall(0).args[0]).toStrictEqual(
|
|
|
|
'/new-account/import',
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2022-08-08 21:01:38 +02:00
|
|
|
describe('Connect hardware wallet', () => {
|
2021-04-15 20:01:46 +02:00
|
|
|
it('renders import account item', () => {
|
2022-08-08 21:01:38 +02:00
|
|
|
const connectHardwareWallet = screen.getByText('Connect hardware wallet');
|
2022-08-03 22:30:43 +02:00
|
|
|
expect(connectHardwareWallet).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('calls toggle menu and push /new-account/connect route to history', () => {
|
2022-08-08 21:01:38 +02:00
|
|
|
const connectHardwareWallet = screen.getByText('Connect hardware wallet');
|
2022-08-03 22:30:43 +02:00
|
|
|
fireEvent.click(connectHardwareWallet);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(props.toggleAccountMenu.calledOnce).toStrictEqual(true);
|
|
|
|
expect(props.history.push.getCall(0).args[0]).toStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
'/new-account/connect',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-23 00:31:13 +02:00
|
|
|
describe('Support', () => {
|
|
|
|
global.platform = { openTab: sinon.spy() };
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('renders import account item', () => {
|
2022-08-08 21:01:38 +02:00
|
|
|
const support = screen.getByText('Submit a ticket');
|
2022-08-03 22:30:43 +02:00
|
|
|
expect(support).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-23 00:31:13 +02:00
|
|
|
it('opens support link when clicked', () => {
|
2022-08-08 21:01:38 +02:00
|
|
|
const support = screen.getByText('Submit a ticket');
|
2022-08-03 22:30:43 +02:00
|
|
|
fireEvent.click(support);
|
2021-04-23 00:31:13 +02:00
|
|
|
expect(global.platform.openTab.calledOnce).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('Settings', () => {
|
|
|
|
it('renders import account item', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const settings = screen.getByText('Settings');
|
|
|
|
expect(settings).toBeInTheDocument();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('calls toggle menu and push /new-account/connect route to history', () => {
|
2022-08-03 22:30:43 +02:00
|
|
|
const settings = screen.getByText('Settings');
|
|
|
|
fireEvent.click(settings);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(props.toggleAccountMenu.calledOnce).toStrictEqual(true);
|
|
|
|
expect(props.history.push.getCall(0).args[0]).toStrictEqual('/settings');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|