1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-01 00:28:06 +01:00
metamask-extension/ui/app/selectors/tests/permissions.test.js

429 lines
15 KiB
JavaScript
Raw Normal View History

2020-05-02 21:41:17 +02:00
import assert from 'assert'
2020-05-15 20:53:52 +02:00
import {
getConnectedDomainsForSelectedAddress,
getOrderedConnectedAccountsForActiveTab,
getPermissionsForActiveTab,
} from '../permissions'
2020-05-02 21:41:17 +02:00
describe('selectors', function () {
describe('getConnectedDomainsForSelectedAddress', function () {
it('should return the list of connected domains when there is 1 connected account', function () {
const mockState = {
metamask: {
selectedAddress: '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
domainMetadata: {
'peepeth.com': {
2020-11-03 00:41:28 +01:00
icon: 'https://peepeth.com/favicon-32x32.png',
name: 'Peepeth',
host: 'peepeth.com',
2020-05-02 21:41:17 +02:00
},
'https://remix.ethereum.org': {
2020-11-03 00:41:28 +01:00
icon: 'https://remix.ethereum.org/icon.png',
name: 'Remix - Ethereum IDE',
host: 'remix.ethereum.org',
2020-05-02 21:41:17 +02:00
},
},
domains: {
'peepeth.com': {
2020-11-03 00:41:28 +01:00
permissions: [
2020-05-02 21:41:17 +02:00
{
2020-11-03 00:41:28 +01:00
'@context': ['https://github.com/MetaMask/rpc-cap'],
caveats: [
2020-05-02 21:41:17 +02:00
{
2020-11-03 00:41:28 +01:00
name: 'exposedAccounts',
type: 'filterResponse',
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
2020-05-02 21:41:17 +02:00
},
],
2020-11-03 00:41:28 +01:00
date: 1585676177970,
id: '840d72a0-925f-449f-830a-1aa1dd5ce151',
invoker: 'peepeth.com',
parentCapability: 'eth_accounts',
2020-05-02 21:41:17 +02:00
},
],
},
'https://remix.ethereum.org': {
2020-11-03 00:41:28 +01:00
permissions: [
2020-05-02 21:41:17 +02:00
{
2020-11-03 00:41:28 +01:00
'@context': ['https://github.com/MetaMask/rpc-cap'],
caveats: [
2020-05-02 21:41:17 +02:00
{
2020-11-03 00:41:28 +01:00
type: 'filterResponse',
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
name: 'exposedAccounts',
2020-05-02 21:41:17 +02:00
},
],
2020-11-03 00:41:28 +01:00
date: 1585685128948,
id: '6b9615cc-64e4-4317-afab-3c4f8ee0244a',
invoker: 'https://remix.ethereum.org',
parentCapability: 'eth_accounts',
2020-05-02 21:41:17 +02:00
},
],
},
},
},
}
const extensionId = undefined
assert.deepStrictEqual(getConnectedDomainsForSelectedAddress(mockState), [
2020-11-03 00:41:28 +01:00
{
extensionId,
icon: 'https://peepeth.com/favicon-32x32.png',
origin: 'peepeth.com',
name: 'Peepeth',
host: 'peepeth.com',
},
{
extensionId,
name: 'Remix - Ethereum IDE',
icon: 'https://remix.ethereum.org/icon.png',
origin: 'https://remix.ethereum.org',
host: 'remix.ethereum.org',
},
])
2020-05-02 21:41:17 +02:00
})
it('should return the list of connected domains when there are 2 connected accounts', function () {
const mockState = {
metamask: {
selectedAddress: '0x7250739de134d33ec7ab1ee592711e15098c9d2d',
domainMetadata: {
'peepeth.com': {
2020-11-03 00:41:28 +01:00
icon: 'https://peepeth.com/favicon-32x32.png',
name: 'Peepeth',
host: 'peepeth.com',
2020-05-02 21:41:17 +02:00
},
'https://remix.ethereum.org': {
2020-11-03 00:41:28 +01:00
icon: 'https://remix.ethereum.org/icon.png',
name: 'Remix - Ethereum IDE',
host: 'remix.ethereum.com',
2020-05-02 21:41:17 +02:00
},
},
domains: {
'peepeth.com': {
2020-11-03 00:41:28 +01:00
permissions: [
2020-05-02 21:41:17 +02:00
{
2020-11-03 00:41:28 +01:00
'@context': ['https://github.com/MetaMask/rpc-cap'],
caveats: [
2020-05-02 21:41:17 +02:00
{
2020-11-03 00:41:28 +01:00
name: 'exposedAccounts',
type: 'filterResponse',
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
2020-05-02 21:41:17 +02:00
},
],
2020-11-03 00:41:28 +01:00
date: 1585676177970,
id: '840d72a0-925f-449f-830a-1aa1dd5ce151',
invoker: 'peepeth.com',
parentCapability: 'eth_accounts',
2020-05-02 21:41:17 +02:00
},
],
},
'https://remix.ethereum.org': {
2020-11-03 00:41:28 +01:00
permissions: [
2020-05-02 21:41:17 +02:00
{
2020-11-03 00:41:28 +01:00
'@context': ['https://github.com/MetaMask/rpc-cap'],
caveats: [
2020-05-02 21:41:17 +02:00
{
2020-11-03 00:41:28 +01:00
type: 'filterResponse',
value: [
2020-05-02 21:41:17 +02:00
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
'0x7250739de134d33ec7ab1ee592711e15098c9d2d',
],
2020-11-03 00:41:28 +01:00
name: 'exposedAccounts',
2020-05-02 21:41:17 +02:00
},
],
2020-11-03 00:41:28 +01:00
date: 1585685128948,
id: '6b9615cc-64e4-4317-afab-3c4f8ee0244a',
invoker: 'https://remix.ethereum.org',
parentCapability: 'eth_accounts',
2020-05-02 21:41:17 +02:00
},
],
},
},
},
}
const extensionId = undefined
assert.deepStrictEqual(getConnectedDomainsForSelectedAddress(mockState), [
2020-11-03 00:41:28 +01:00
{
extensionId,
name: 'Remix - Ethereum IDE',
icon: 'https://remix.ethereum.org/icon.png',
origin: 'https://remix.ethereum.org',
host: 'remix.ethereum.com',
},
])
2020-05-02 21:41:17 +02:00
})
})
2020-05-15 20:53:52 +02:00
describe('getConnectedAccountsForActiveTab', function () {
const mockState = {
activeTab: {
2020-11-03 00:41:28 +01:00
title: 'Eth Sign Tests',
origin: 'https://remix.ethereum.org',
protocol: 'https:',
url: 'https://remix.ethereum.org/',
2020-05-15 20:53:52 +02:00
},
metamask: {
accounts: {
0x7250739de134d33ec7ab1ee592711e15098c9d2d: {
address: '0x7250739de134d33ec7ab1ee592711e15098c9d2d',
2020-05-15 20:53:52 +02:00
},
0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5: {
address: '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
2020-05-15 20:53:52 +02:00
},
0xb3958fb96c8201486ae20be1d5c9f58083df343a: {
address: '0xb3958fb96c8201486ae20be1d5c9f58083df343a',
},
0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc: {
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
},
0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4: {
address: '0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
2020-05-15 20:53:52 +02:00
},
},
cachedBalances: {},
2020-05-15 20:53:52 +02:00
domains: {
'https://remix.ethereum.org': {
2020-11-03 00:41:28 +01:00
permissions: [
2020-05-15 20:53:52 +02:00
{
2020-11-03 00:41:28 +01:00
'@context': ['https://github.com/MetaMask/rpc-cap'],
caveats: [
2020-05-15 20:53:52 +02:00
{
2020-11-03 00:41:28 +01:00
name: 'exposedAccounts',
type: 'filterResponse',
value: [
2020-05-15 20:53:52 +02:00
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
'0x7250739de134d33ec7ab1ee592711e15098c9d2d',
'0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
'0xb3958fb96c8201486ae20be1d5c9f58083df343a',
2020-05-15 20:53:52 +02:00
],
},
],
2020-11-03 00:41:28 +01:00
date: 1586359844177,
id: '3aa65a8b-3bcb-4944-941b-1baa5fe0ed8b',
invoker: 'https://remix.ethereum.org',
parentCapability: 'eth_accounts',
2020-05-15 20:53:52 +02:00
},
],
},
'peepeth.com': {
2020-11-03 00:41:28 +01:00
permissions: [
2020-05-15 20:53:52 +02:00
{
2020-11-03 00:41:28 +01:00
'@context': ['https://github.com/MetaMask/rpc-cap'],
caveats: [
2020-05-15 20:53:52 +02:00
{
2020-11-03 00:41:28 +01:00
name: 'exposedAccounts',
type: 'filterResponse',
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
2020-05-15 20:53:52 +02:00
},
],
2020-11-03 00:41:28 +01:00
date: 1585676177970,
id: '840d72a0-925f-449f-830a-1aa1dd5ce151',
invoker: 'peepeth.com',
parentCapability: 'eth_accounts',
2020-05-15 20:53:52 +02:00
},
],
},
},
identities: {
'0x7250739de134d33ec7ab1ee592711e15098c9d2d': {
2020-11-03 00:41:28 +01:00
address: '0x7250739de134d33ec7ab1ee592711e15098c9d2d',
name: 'Really Long Name That Should Be Truncated',
},
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5': {
2020-11-03 00:41:28 +01:00
address: '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
lastSelected: 1586359844192,
2020-11-03 00:41:28 +01:00
name: 'Account 1',
},
'0xb3958fb96c8201486ae20be1d5c9f58083df343a': {
lastSelected: 1586359844193,
2020-11-03 00:41:28 +01:00
address: '0xb3958fb96c8201486ae20be1d5c9f58083df343a',
name: 'Account 2',
},
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc': {
2020-11-03 00:41:28 +01:00
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
lastSelected: 1586359844192,
2020-11-03 00:41:28 +01:00
name: 'Account 3',
},
'0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4': {
address: '0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
name: 'Account 4',
2020-05-15 20:53:52 +02:00
},
},
2020-11-03 00:41:28 +01:00
keyrings: [
{
accounts: [
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
'0x7250739de134d33ec7ab1ee592711e15098c9d2d',
'0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
'0xb3958fb96c8201486ae20be1d5c9f58083df343a',
],
},
],
2020-05-15 20:53:52 +02:00
permissionsHistory: {
'https://remix.ethereum.org': {
2020-11-03 00:41:28 +01:00
eth_accounts: {
accounts: {
2020-05-15 20:53:52 +02:00
'0x7250739de134d33ec7ab1ee592711e15098c9d2d': 1586359844192,
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5': 1586359844192,
'0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4': 1586359844192,
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc': 1586359844192,
'0xb3958fb96c8201486ae20be1d5c9f58083df343a': 1586359844192,
2020-05-15 20:53:52 +02:00
},
2020-11-03 00:41:28 +01:00
lastApproved: 1586359844192,
2020-05-15 20:53:52 +02:00
},
},
},
},
}
it('should return connected accounts sorted by last selected, then by keyring controller order', function () {
assert.deepStrictEqual(
getOrderedConnectedAccountsForActiveTab(mockState),
[
{
address: '0xb3958fb96c8201486ae20be1d5c9f58083df343a',
name: 'Account 2',
lastActive: 1586359844192,
lastSelected: 1586359844193,
},
{
address: '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
name: 'Account 1',
lastActive: 1586359844192,
lastSelected: 1586359844192,
},
{
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
name: 'Account 3',
lastActive: 1586359844192,
lastSelected: 1586359844192,
},
{
address: '0x7250739de134d33ec7ab1ee592711e15098c9d2d',
name: 'Really Long Name That Should Be Truncated',
lastActive: 1586359844192,
},
{
address: '0x617b3f8050a0bd94b6b1da02b4384ee5b4df13f4',
name: 'Account 4',
lastActive: 1586359844192,
},
],
)
2020-05-15 20:53:52 +02:00
})
})
describe('getPermissionsForActiveTab', function () {
const mockState = {
activeTab: {
2020-11-03 00:41:28 +01:00
title: 'Eth Sign Tests',
origin: 'https://remix.ethereum.org',
protocol: 'https:',
url: 'https://remix.ethereum.org/',
2020-05-15 20:53:52 +02:00
},
metamask: {
identities: {
'0x7250739de134d33ec7ab1ee592711e15098c9d2d': {
2020-11-03 00:41:28 +01:00
address: '0x7250739de134d33ec7ab1ee592711e15098c9d2d',
name: 'Really Long Name That Should Be Truncated',
2020-05-15 20:53:52 +02:00
},
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5': {
2020-11-03 00:41:28 +01:00
address: '0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
name: 'Account 1',
2020-05-15 20:53:52 +02:00
},
'0xb3958fb96c8201486ae20be1d5c9f58083df343a': {
2020-11-03 00:41:28 +01:00
address: '0xb3958fb96c8201486ae20be1d5c9f58083df343a',
name: 'Account 2',
2020-05-15 20:53:52 +02:00
},
},
domains: {
'https://remix.ethereum.org': {
2020-11-03 00:41:28 +01:00
permissions: [
2020-05-15 20:53:52 +02:00
{
2020-11-03 00:41:28 +01:00
'@context': ['https://github.com/MetaMask/rpc-cap'],
caveats: [
2020-05-15 20:53:52 +02:00
{
2020-11-03 00:41:28 +01:00
name: 'exposedAccounts',
type: 'filterResponse',
value: [
2020-05-15 20:53:52 +02:00
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5',
'0x7250739de134d33ec7ab1ee592711e15098c9d2d',
],
},
],
2020-11-03 00:41:28 +01:00
date: 1586359844177,
id: '3aa65a8b-3bcb-4944-941b-1baa5fe0ed8b',
invoker: 'https://remix.ethereum.org',
parentCapability: 'eth_accounts',
2020-05-15 20:53:52 +02:00
},
],
},
'peepeth.com': {
2020-11-03 00:41:28 +01:00
permissions: [
2020-05-15 20:53:52 +02:00
{
2020-11-03 00:41:28 +01:00
'@context': ['https://github.com/MetaMask/rpc-cap'],
caveats: [
2020-05-15 20:53:52 +02:00
{
2020-11-03 00:41:28 +01:00
name: 'exposedAccounts',
type: 'filterResponse',
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
2020-05-15 20:53:52 +02:00
},
],
2020-11-03 00:41:28 +01:00
date: 1585676177970,
id: '840d72a0-925f-449f-830a-1aa1dd5ce151',
invoker: 'peepeth.com',
parentCapability: 'eth_accounts',
2020-05-15 20:53:52 +02:00
},
],
},
'uniswap.exchange': {
2020-11-03 00:41:28 +01:00
permissions: [
2020-05-15 20:53:52 +02:00
{
2020-11-03 00:41:28 +01:00
'@context': ['https://github.com/MetaMask/rpc-cap'],
caveats: [
2020-05-15 20:53:52 +02:00
{
2020-11-03 00:41:28 +01:00
name: 'exposedAccounts',
type: 'filterResponse',
value: ['0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5'],
2020-05-15 20:53:52 +02:00
},
],
2020-11-03 00:41:28 +01:00
date: 1585616816623,
id: 'ce625215-f2e9-48e7-93ca-21ba193244ff',
invoker: 'uniswap.exchange',
parentCapability: 'eth_accounts',
2020-05-15 20:53:52 +02:00
},
],
},
},
domainMetadata: {
'https://remix.ethereum.org': {
2020-11-03 00:41:28 +01:00
icon: 'https://remix.ethereum.org/icon.png',
name: 'Remix - Ethereum IDE',
2020-05-15 20:53:52 +02:00
},
},
permissionsHistory: {
'https://remix.ethereum.org': {
2020-11-03 00:41:28 +01:00
eth_accounts: {
accounts: {
2020-05-15 20:53:52 +02:00
'0x7250739de134d33ec7ab1ee592711e15098c9d2d': 1586359844192,
'0x8e5d75d60224ea0c33d0041e75de68b1c3cb6dd5': 1586359844192,
},
2020-11-03 00:41:28 +01:00
lastApproved: 1586359844192,
2020-05-15 20:53:52 +02:00
},
},
},
},
}
it('should return a list of permissions strings', function () {
assert.deepStrictEqual(getPermissionsForActiveTab(mockState), [
2020-11-03 00:41:28 +01:00
{
key: 'eth_accounts',
},
])
2020-05-15 20:53:52 +02:00
})
})
2020-05-02 21:41:17 +02:00
})