@ -37,15 +37,15 @@ const {
ALL _ACCOUNTS ,
ALL _ACCOUNTS ,
ACCOUNTS ,
ACCOUNTS ,
DUMMY _ACCOUNT ,
DUMMY _ACCOUNT ,
ORIG INS,
DOMA INS,
PERM _NAMES ,
PERM _NAMES ,
REQUEST _IDS ,
REQUEST _IDS ,
EXTRA _ACCOUNT ,
EXTRA _ACCOUNT ,
} = constants
} = constants
const initNotifications = ( ) => {
const initNotifications = ( ) => {
return Object . values ( ORIG INS) . reduce ( ( acc , domain ) => {
return Object . values ( DOMA INS) . reduce ( ( acc , domain ) => {
acc [ domain ] = [ ]
acc [ domain . origin ] = [ ]
return acc
return acc
} , { } )
} , { } )
}
}
@ -73,19 +73,19 @@ describe('permissions controller', function () {
beforeEach ( function ( ) {
beforeEach ( function ( ) {
permController = initPermController ( )
permController = initPermController ( )
grantPermissions (
grantPermissions (
permController , ORIG INS. a ,
permController , DOMA INS. a . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
)
)
grantPermissions (
grantPermissions (
permController , ORIG INS. b ,
permController , DOMA INS. b . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
)
)
} )
} )
it ( 'gets permitted accounts for permitted origins' , async function ( ) {
it ( 'gets permitted accounts for permitted origins' , async function ( ) {
const aAccounts = await permController . getAccounts ( ORIG INS. a )
const aAccounts = await permController . getAccounts ( DOMA INS. a . origin )
const bAccounts = await permController . getAccounts ( ORIG INS. b )
const bAccounts = await permController . getAccounts ( DOMA INS. b . origin )
assert . deepEqual (
assert . deepEqual (
aAccounts , [ ACCOUNTS . a . primary ] ,
aAccounts , [ ACCOUNTS . a . primary ] ,
@ -98,7 +98,7 @@ describe('permissions controller', function () {
} )
} )
it ( 'does not get accounts for unpermitted origins' , async function ( ) {
it ( 'does not get accounts for unpermitted origins' , async function ( ) {
const cAccounts = await permController . getAccounts ( ORIG INS. c )
const cAccounts = await permController . getAccounts ( DOMA INS. c . origin )
assert . deepEqual ( cAccounts , [ ] , 'origin should have no accounts' )
assert . deepEqual ( cAccounts , [ ] , 'origin should have no accounts' )
} )
} )
@ -114,29 +114,29 @@ describe('permissions controller', function () {
const permController = initPermController ( )
const permController = initPermController ( )
grantPermissions (
grantPermissions (
permController , ORIG INS. a ,
permController , DOMA INS. a . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
)
)
grantPermissions (
grantPermissions (
permController , ORIG INS. b ,
permController , DOMA INS. b . origin ,
PERMS . finalizedRequests . test _method ( )
PERMS . finalizedRequests . test _method ( )
)
)
assert . ok (
assert . ok (
permController . hasPermission ( ORIG INS. a , 'eth_accounts' ) ,
permController . hasPermission ( DOMA INS. a . origin , 'eth_accounts' ) ,
'should return true for granted permission'
'should return true for granted permission'
)
)
assert . ok (
assert . ok (
permController . hasPermission ( ORIG INS. b , 'test_method' ) ,
permController . hasPermission ( DOMA INS. b . origin , 'test_method' ) ,
'should return true for granted permission'
'should return true for granted permission'
)
)
assert . ok (
assert . ok (
! permController . hasPermission ( ORIG INS. a , 'test_method' ) ,
! permController . hasPermission ( DOMA INS. a . origin , 'test_method' ) ,
'should return false for non-granted permission'
'should return false for non-granted permission'
)
)
assert . ok (
assert . ok (
! permController . hasPermission ( ORIG INS. b , 'eth_accounts' ) ,
! permController . hasPermission ( DOMA INS. b . origin , 'eth_accounts' ) ,
'should return true for non-granted permission'
'should return true for non-granted permission'
)
)
@ -145,7 +145,7 @@ describe('permissions controller', function () {
'should return false for unknown origin'
'should return false for unknown origin'
)
)
assert . ok (
assert . ok (
! permController . hasPermission ( ORIG INS. b , 'foo' ) ,
! permController . hasPermission ( DOMA INS. b . origin , 'foo' ) ,
'should return false for unknown permission'
'should return false for unknown permission'
)
)
} )
} )
@ -159,21 +159,21 @@ describe('permissions controller', function () {
const permController = initPermController ( notifications )
const permController = initPermController ( notifications )
grantPermissions (
grantPermissions (
permController , ORIG INS. a ,
permController , DOMA INS. a . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
)
)
grantPermissions (
grantPermissions (
permController , ORIG INS. b ,
permController , DOMA INS. b . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
)
)
grantPermissions (
grantPermissions (
permController , ORIG INS. c ,
permController , DOMA INS. c . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . c . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . c . permitted )
)
)
let aAccounts = await permController . getAccounts ( ORIG INS. a )
let aAccounts = await permController . getAccounts ( DOMA INS. a . origin )
let bAccounts = await permController . getAccounts ( ORIG INS. b )
let bAccounts = await permController . getAccounts ( DOMA INS. b . origin )
let cAccounts = await permController . getAccounts ( ORIG INS. c )
let cAccounts = await permController . getAccounts ( DOMA INS. c . origin )
assert . deepEqual (
assert . deepEqual (
@ -199,9 +199,9 @@ describe('permissions controller', function () {
)
)
} )
} )
aAccounts = await permController . getAccounts ( ORIG INS. a )
aAccounts = await permController . getAccounts ( DOMA INS. a . origin )
bAccounts = await permController . getAccounts ( ORIG INS. b )
bAccounts = await permController . getAccounts ( DOMA INS. b . origin )
cAccounts = await permController . getAccounts ( ORIG INS. c )
cAccounts = await permController . getAccounts ( DOMA INS. c . origin )
assert . deepEqual ( aAccounts , [ ] , 'first origin should have no accounts' )
assert . deepEqual ( aAccounts , [ ] , 'first origin should have no accounts' )
assert . deepEqual ( bAccounts , [ ] , 'second origin should have no accounts' )
assert . deepEqual ( bAccounts , [ ] , 'second origin should have no accounts' )
@ -230,19 +230,19 @@ describe('permissions controller', function () {
notifications = initNotifications ( )
notifications = initNotifications ( )
permController = initPermController ( notifications )
permController = initPermController ( notifications )
grantPermissions (
grantPermissions (
permController , ORIG INS. a ,
permController , DOMA INS. a . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
)
)
grantPermissions (
grantPermissions (
permController , ORIG INS. b ,
permController , DOMA INS. b . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
)
)
} )
} )
it ( 'removes permissions for multiple domains' , async function ( ) {
it ( 'removes permissions for multiple domains' , async function ( ) {
let aAccounts = await permController . getAccounts ( ORIG INS. a )
let aAccounts = await permController . getAccounts ( DOMA INS. a . origin )
let bAccounts = await permController . getAccounts ( ORIG INS. b )
let bAccounts = await permController . getAccounts ( DOMA INS. b . origin )
assert . deepEqual (
assert . deepEqual (
aAccounts , [ ACCOUNTS . a . primary ] ,
aAccounts , [ ACCOUNTS . a . primary ] ,
@ -254,22 +254,22 @@ describe('permissions controller', function () {
)
)
permController . removePermissionsFor ( {
permController . removePermissionsFor ( {
[ ORIG INS. a ] : [ PERM _NAMES . eth _accounts ] ,
[ DOMA INS. a . origin ] : [ PERM _NAMES . eth _accounts ] ,
[ ORIG INS. b ] : [ PERM _NAMES . eth _accounts ] ,
[ DOMA INS. b . origin ] : [ PERM _NAMES . eth _accounts ] ,
} )
} )
aAccounts = await permController . getAccounts ( ORIG INS. a )
aAccounts = await permController . getAccounts ( DOMA INS. a . origin )
bAccounts = await permController . getAccounts ( ORIG INS. b )
bAccounts = await permController . getAccounts ( DOMA INS. b . origin )
assert . deepEqual ( aAccounts , [ ] , 'first origin should have no accounts' )
assert . deepEqual ( aAccounts , [ ] , 'first origin should have no accounts' )
assert . deepEqual ( bAccounts , [ ] , 'second origin should have no accounts' )
assert . deepEqual ( bAccounts , [ ] , 'second origin should have no accounts' )
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. a ] , [ NOTIFICATIONS . removedAccounts ( ) ] ,
notifications [ DOMA INS. a . origin ] , [ NOTIFICATIONS . removedAccounts ( ) ] ,
'first origin should have correct notification'
'first origin should have correct notification'
)
)
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. b ] , [ NOTIFICATIONS . removedAccounts ( ) ] ,
notifications [ DOMA INS. b . origin ] , [ NOTIFICATIONS . removedAccounts ( ) ] ,
'second origin should have correct notification'
'second origin should have correct notification'
)
)
@ -282,10 +282,10 @@ describe('permissions controller', function () {
it ( 'only removes targeted permissions from single domain' , async function ( ) {
it ( 'only removes targeted permissions from single domain' , async function ( ) {
grantPermissions (
grantPermissions (
permController , ORIG INS. b , PERMS . finalizedRequests . test _method ( )
permController , DOMA INS. b . origin , PERMS . finalizedRequests . test _method ( )
)
)
let bPermissions = permController . permissions . getPermissionsForDomain ( ORIG INS. b )
let bPermissions = permController . permissions . getPermissionsForDomain ( DOMA INS. b . origin )
assert . ok (
assert . ok (
(
(
@ -297,10 +297,10 @@ describe('permissions controller', function () {
)
)
permController . removePermissionsFor ( {
permController . removePermissionsFor ( {
[ ORIG INS. b ] : [ PERM _NAMES . test _method ] ,
[ DOMA INS. b . origin ] : [ PERM _NAMES . test _method ] ,
} )
} )
bPermissions = permController . permissions . getPermissionsForDomain ( ORIG INS. b )
bPermissions = permController . permissions . getPermissionsForDomain ( DOMA INS. b . origin )
assert . ok (
assert . ok (
(
(
@ -314,11 +314,11 @@ describe('permissions controller', function () {
it ( 'removes permissions for a single domain, without affecting another' , async function ( ) {
it ( 'removes permissions for a single domain, without affecting another' , async function ( ) {
permController . removePermissionsFor ( {
permController . removePermissionsFor ( {
[ ORIG INS. b ] : [ PERM _NAMES . eth _accounts ] ,
[ DOMA INS. b . origin ] : [ PERM _NAMES . eth _accounts ] ,
} )
} )
const aAccounts = await permController . getAccounts ( ORIG INS. a )
const aAccounts = await permController . getAccounts ( DOMA INS. a . origin )
const bAccounts = await permController . getAccounts ( ORIG INS. b )
const bAccounts = await permController . getAccounts ( DOMA INS. b . origin )
assert . deepEqual (
assert . deepEqual (
aAccounts , [ ACCOUNTS . a . primary ] ,
aAccounts , [ ACCOUNTS . a . primary ] ,
@ -327,16 +327,16 @@ describe('permissions controller', function () {
assert . deepEqual ( bAccounts , [ ] , 'second origin should have no accounts' )
assert . deepEqual ( bAccounts , [ ] , 'second origin should have no accounts' )
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. a ] , [ ] ,
notifications [ DOMA INS. a . origin ] , [ ] ,
'first origin should have no notifications'
'first origin should have no notifications'
)
)
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. b ] , [ NOTIFICATIONS . removedAccounts ( ) ] ,
notifications [ DOMA INS. b . origin ] , [ NOTIFICATIONS . removedAccounts ( ) ] ,
'second origin should have correct notification'
'second origin should have correct notification'
)
)
assert . deepEqual (
assert . deepEqual (
Object . keys ( permController . permissions . getDomains ( ) ) , [ ORIG INS. a ] ,
Object . keys ( permController . permissions . getDomains ( ) ) , [ DOMA INS. a . origin ] ,
'only first origin should remain'
'only first origin should remain'
)
)
} )
} )
@ -345,16 +345,16 @@ describe('permissions controller', function () {
// it knows nothing of this origin
// it knows nothing of this origin
permController . removePermissionsFor ( {
permController . removePermissionsFor ( {
[ ORIG INS. c ] : [ PERM _NAMES . eth _accounts ] ,
[ DOMA INS. c . origin ] : [ PERM _NAMES . eth _accounts ] ,
} )
} )
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. c ] , [ NOTIFICATIONS . removedAccounts ( ) ] ,
notifications [ DOMA INS. c . origin ] , [ NOTIFICATIONS . removedAccounts ( ) ] ,
'unknown origin should have notification'
'unknown origin should have notification'
)
)
const aAccounts = await permController . getAccounts ( ORIG INS. a )
const aAccounts = await permController . getAccounts ( DOMA INS. a . origin )
const bAccounts = await permController . getAccounts ( ORIG INS. b )
const bAccounts = await permController . getAccounts ( DOMA INS. b . origin )
assert . deepEqual (
assert . deepEqual (
aAccounts , [ ACCOUNTS . a . primary ] ,
aAccounts , [ ACCOUNTS . a . primary ] ,
@ -367,7 +367,7 @@ describe('permissions controller', function () {
assert . deepEqual (
assert . deepEqual (
Object . keys ( permController . permissions . getDomains ( ) ) ,
Object . keys ( permController . permissions . getDomains ( ) ) ,
[ ORIGINS . a , ORIG INS. b ] ,
[ DOMAINS . a . origin , DOMA INS. b . origin ] ,
'should have correct domains'
'should have correct domains'
)
)
} )
} )
@ -380,11 +380,11 @@ describe('permissions controller', function () {
beforeEach ( function ( ) {
beforeEach ( function ( ) {
permController = initPermController ( )
permController = initPermController ( )
grantPermissions (
grantPermissions (
permController , ORIG INS. a ,
permController , DOMA INS. a . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
)
)
grantPermissions (
grantPermissions (
permController , ORIG INS. b ,
permController , DOMA INS. b . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
)
)
} )
} )
@ -470,18 +470,18 @@ describe('permissions controller', function () {
notifications = initNotifications ( )
notifications = initNotifications ( )
permController = initPermController ( notifications )
permController = initPermController ( notifications )
grantPermissions (
grantPermissions (
permController , ORIG INS. a ,
permController , DOMA INS. a . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
)
)
grantPermissions (
grantPermissions (
permController , ORIG INS. b ,
permController , DOMA INS. b . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
)
)
} )
} )
it ( 'should throw if account is not a string' , async function ( ) {
it ( 'should throw if account is not a string' , async function ( ) {
await assert . rejects (
await assert . rejects (
( ) => permController . addPermittedAccount ( ORIG INS. a , { } ) ,
( ) => permController . addPermittedAccount ( DOMA INS. a . origin , { } ) ,
ERRORS . validatePermittedAccounts . nonKeyringAccount ( { } ) ,
ERRORS . validatePermittedAccounts . nonKeyringAccount ( { } ) ,
'should throw on non-string account param'
'should throw on non-string account param'
)
)
@ -489,7 +489,7 @@ describe('permissions controller', function () {
it ( 'should throw if given account is not in keyring' , async function ( ) {
it ( 'should throw if given account is not in keyring' , async function ( ) {
await assert . rejects (
await assert . rejects (
( ) => permController . addPermittedAccount ( ORIG INS. a , DUMMY _ACCOUNT ) ,
( ) => permController . addPermittedAccount ( DOMA INS. a . origin , DUMMY _ACCOUNT ) ,
ERRORS . validatePermittedAccounts . nonKeyringAccount ( DUMMY _ACCOUNT ) ,
ERRORS . validatePermittedAccounts . nonKeyringAccount ( DUMMY _ACCOUNT ) ,
'should throw on non-keyring account'
'should throw on non-keyring account'
)
)
@ -505,7 +505,7 @@ describe('permissions controller', function () {
it ( 'should throw if origin lacks any permissions' , async function ( ) {
it ( 'should throw if origin lacks any permissions' , async function ( ) {
await assert . rejects (
await assert . rejects (
( ) => permController . addPermittedAccount ( ORIG INS. c , EXTRA _ACCOUNT ) ,
( ) => permController . addPermittedAccount ( DOMA INS. c . origin , EXTRA _ACCOUNT ) ,
ERRORS . addPermittedAccount . invalidOrigin ( ) ,
ERRORS . addPermittedAccount . invalidOrigin ( ) ,
'should throw on origin without permissions'
'should throw on origin without permissions'
)
)
@ -513,12 +513,12 @@ describe('permissions controller', function () {
it ( 'should throw if origin lacks eth_accounts permission' , async function ( ) {
it ( 'should throw if origin lacks eth_accounts permission' , async function ( ) {
grantPermissions (
grantPermissions (
permController , ORIG INS. c ,
permController , DOMA INS. c . origin ,
PERMS . finalizedRequests . test _method ( )
PERMS . finalizedRequests . test _method ( )
)
)
await assert . rejects (
await assert . rejects (
( ) => permController . addPermittedAccount ( ORIG INS. c , EXTRA _ACCOUNT ) ,
( ) => permController . addPermittedAccount ( DOMA INS. c . origin , EXTRA _ACCOUNT ) ,
ERRORS . addPermittedAccount . noEthAccountsPermission ( ) ,
ERRORS . addPermittedAccount . noEthAccountsPermission ( ) ,
'should throw on origin without eth_accounts permission'
'should throw on origin without eth_accounts permission'
)
)
@ -526,16 +526,16 @@ describe('permissions controller', function () {
it ( 'should throw if account is already permitted' , async function ( ) {
it ( 'should throw if account is already permitted' , async function ( ) {
await assert . rejects (
await assert . rejects (
( ) => permController . addPermittedAccount ( ORIG INS. a , ACCOUNTS . a . permitted [ 0 ] ) ,
( ) => permController . addPermittedAccount ( DOMA INS. a . origin , ACCOUNTS . a . permitted [ 0 ] ) ,
ERRORS . addPermittedAccount . alreadyPermitted ( ) ,
ERRORS . addPermittedAccount . alreadyPermitted ( ) ,
'should throw if account is already permitted'
'should throw if account is already permitted'
)
)
} )
} )
it ( 'should successfully add permitted account' , async function ( ) {
it ( 'should successfully add permitted account' , async function ( ) {
await permController . addPermittedAccount ( ORIG INS. a , EXTRA _ACCOUNT )
await permController . addPermittedAccount ( DOMA INS. a . origin , EXTRA _ACCOUNT )
const accounts = await permController . _getPermittedAccounts ( ORIG INS. a )
const accounts = await permController . _getPermittedAccounts ( DOMA INS. a . origin )
assert . deepEqual (
assert . deepEqual (
accounts , [ ... ACCOUNTS . a . permitted , EXTRA _ACCOUNT ] ,
accounts , [ ... ACCOUNTS . a . permitted , EXTRA _ACCOUNT ] ,
@ -543,7 +543,7 @@ describe('permissions controller', function () {
)
)
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. a ] [ 0 ] ,
notifications [ DOMA INS. a . origin ] [ 0 ] ,
NOTIFICATIONS . newAccounts ( [ ACCOUNTS . a . primary ] ) ,
NOTIFICATIONS . newAccounts ( [ ACCOUNTS . a . primary ] ) ,
'origin should have correct notification'
'origin should have correct notification'
)
)
@ -557,18 +557,18 @@ describe('permissions controller', function () {
notifications = initNotifications ( )
notifications = initNotifications ( )
permController = initPermController ( notifications )
permController = initPermController ( notifications )
grantPermissions (
grantPermissions (
permController , ORIG INS. a ,
permController , DOMA INS. a . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
)
)
grantPermissions (
grantPermissions (
permController , ORIG INS. b ,
permController , DOMA INS. b . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . b . permitted )
)
)
} )
} )
it ( 'should throw if account is not a string' , async function ( ) {
it ( 'should throw if account is not a string' , async function ( ) {
await assert . rejects (
await assert . rejects (
( ) => permController . removePermittedAccount ( ORIG INS. a , { } ) ,
( ) => permController . removePermittedAccount ( DOMA INS. a . origin , { } ) ,
ERRORS . validatePermittedAccounts . nonKeyringAccount ( { } ) ,
ERRORS . validatePermittedAccounts . nonKeyringAccount ( { } ) ,
'should throw on non-string account param'
'should throw on non-string account param'
)
)
@ -576,7 +576,7 @@ describe('permissions controller', function () {
it ( 'should throw if given account is not in keyring' , async function ( ) {
it ( 'should throw if given account is not in keyring' , async function ( ) {
await assert . rejects (
await assert . rejects (
( ) => permController . removePermittedAccount ( ORIG INS. a , DUMMY _ACCOUNT ) ,
( ) => permController . removePermittedAccount ( DOMA INS. a . origin , DUMMY _ACCOUNT ) ,
ERRORS . validatePermittedAccounts . nonKeyringAccount ( DUMMY _ACCOUNT ) ,
ERRORS . validatePermittedAccounts . nonKeyringAccount ( DUMMY _ACCOUNT ) ,
'should throw on non-keyring account'
'should throw on non-keyring account'
)
)
@ -592,7 +592,7 @@ describe('permissions controller', function () {
it ( 'should throw if origin lacks any permissions' , async function ( ) {
it ( 'should throw if origin lacks any permissions' , async function ( ) {
await assert . rejects (
await assert . rejects (
( ) => permController . removePermittedAccount ( ORIG INS. c , EXTRA _ACCOUNT ) ,
( ) => permController . removePermittedAccount ( DOMA INS. c . origin , EXTRA _ACCOUNT ) ,
ERRORS . removePermittedAccount . invalidOrigin ( ) ,
ERRORS . removePermittedAccount . invalidOrigin ( ) ,
'should throw on origin without permissions'
'should throw on origin without permissions'
)
)
@ -600,12 +600,12 @@ describe('permissions controller', function () {
it ( 'should throw if origin lacks eth_accounts permission' , async function ( ) {
it ( 'should throw if origin lacks eth_accounts permission' , async function ( ) {
grantPermissions (
grantPermissions (
permController , ORIG INS. c ,
permController , DOMA INS. c . origin ,
PERMS . finalizedRequests . test _method ( )
PERMS . finalizedRequests . test _method ( )
)
)
await assert . rejects (
await assert . rejects (
( ) => permController . removePermittedAccount ( ORIG INS. c , EXTRA _ACCOUNT ) ,
( ) => permController . removePermittedAccount ( DOMA INS. c . origin , EXTRA _ACCOUNT ) ,
ERRORS . removePermittedAccount . noEthAccountsPermission ( ) ,
ERRORS . removePermittedAccount . noEthAccountsPermission ( ) ,
'should throw on origin without eth_accounts permission'
'should throw on origin without eth_accounts permission'
)
)
@ -613,16 +613,16 @@ describe('permissions controller', function () {
it ( 'should throw if account is not permitted' , async function ( ) {
it ( 'should throw if account is not permitted' , async function ( ) {
await assert . rejects (
await assert . rejects (
( ) => permController . removePermittedAccount ( ORIG INS. b , ACCOUNTS . c . permitted [ 0 ] ) ,
( ) => permController . removePermittedAccount ( DOMA INS. b . origin , ACCOUNTS . c . permitted [ 0 ] ) ,
ERRORS . removePermittedAccount . notPermitted ( ) ,
ERRORS . removePermittedAccount . notPermitted ( ) ,
'should throw if account is not permitted'
'should throw if account is not permitted'
)
)
} )
} )
it ( 'should successfully remove permitted account' , async function ( ) {
it ( 'should successfully remove permitted account' , async function ( ) {
await permController . removePermittedAccount ( ORIG INS. a , ACCOUNTS . a . permitted [ 1 ] )
await permController . removePermittedAccount ( DOMA INS. a . origin , ACCOUNTS . a . permitted [ 1 ] )
const accounts = await permController . _getPermittedAccounts ( ORIG INS. a )
const accounts = await permController . _getPermittedAccounts ( DOMA INS. a . origin )
assert . deepEqual (
assert . deepEqual (
accounts , ACCOUNTS . a . permitted . filter ( ( acc ) => acc !== ACCOUNTS . a . permitted [ 1 ] ) ,
accounts , ACCOUNTS . a . permitted . filter ( ( acc ) => acc !== ACCOUNTS . a . permitted [ 1 ] ) ,
@ -630,16 +630,16 @@ describe('permissions controller', function () {
)
)
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. a ] [ 0 ] ,
notifications [ DOMA INS. a . origin ] [ 0 ] ,
NOTIFICATIONS . newAccounts ( [ ACCOUNTS . a . primary ] ) ,
NOTIFICATIONS . newAccounts ( [ ACCOUNTS . a . primary ] ) ,
'origin should have correct notification'
'origin should have correct notification'
)
)
} )
} )
it ( 'should remove eth_accounts permission if removing only permitted account' , async function ( ) {
it ( 'should remove eth_accounts permission if removing only permitted account' , async function ( ) {
await permController . removePermittedAccount ( ORIG INS. b , ACCOUNTS . b . permitted [ 0 ] )
await permController . removePermittedAccount ( DOMA INS. b . origin , ACCOUNTS . b . permitted [ 0 ] )
const accounts = await permController . getAccounts ( ORIG INS. b )
const accounts = await permController . getAccounts ( DOMA INS. b . origin )
assert . deepEqual (
assert . deepEqual (
accounts , [ ] ,
accounts , [ ] ,
@ -647,13 +647,13 @@ describe('permissions controller', function () {
)
)
const permission = await permController . permissions . getPermission (
const permission = await permController . permissions . getPermission (
ORIG INS. b , PERM _NAMES . eth _accounts
DOMA INS. b . origin , PERM _NAMES . eth _accounts
)
)
assert . equal ( permission , undefined , 'origin should not have eth_accounts permission' )
assert . equal ( permission , undefined , 'origin should not have eth_accounts permission' )
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. b ] [ 0 ] ,
notifications [ DOMA INS. b . origin ] [ 0 ] ,
NOTIFICATIONS . removedAccounts ( ) ,
NOTIFICATIONS . removedAccounts ( ) ,
'origin should have correct notification'
'origin should have correct notification'
)
)
@ -744,11 +744,11 @@ describe('permissions controller', function () {
preferences ,
preferences ,
} )
} )
grantPermissions (
grantPermissions (
permController , ORIG INS. b ,
permController , DOMA INS. b . origin ,
PERMS . finalizedRequests . eth _accounts ( [ ... ACCOUNTS . a . permitted , EXTRA _ACCOUNT ] )
PERMS . finalizedRequests . eth _accounts ( [ ... ACCOUNTS . a . permitted , EXTRA _ACCOUNT ] )
)
)
grantPermissions (
grantPermissions (
permController , ORIG INS. c ,
permController , DOMA INS. c . origin ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
PERMS . finalizedRequests . eth _accounts ( ACCOUNTS . a . permitted )
)
)
} )
} )
@ -774,11 +774,11 @@ describe('permissions controller', function () {
await onPreferencesUpdate ( { selectedAddress : DUMMY _ACCOUNT } )
await onPreferencesUpdate ( { selectedAddress : DUMMY _ACCOUNT } )
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. b ] , [ ] ,
notifications [ DOMA INS. b . origin ] , [ ] ,
'should not have emitted notification'
'should not have emitted notification'
)
)
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. c ] , [ ] ,
notifications [ DOMA INS. c . origin ] , [ ] ,
'should not have emitted notification'
'should not have emitted notification'
)
)
} )
} )
@ -792,12 +792,12 @@ describe('permissions controller', function () {
await onPreferencesUpdate ( { selectedAddress : ACCOUNTS . a . permitted [ 0 ] } )
await onPreferencesUpdate ( { selectedAddress : ACCOUNTS . a . permitted [ 0 ] } )
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. b ] ,
notifications [ DOMA INS. b . origin ] ,
[ NOTIFICATIONS . newAccounts ( [ ACCOUNTS . a . primary ] ) ] ,
[ NOTIFICATIONS . newAccounts ( [ ACCOUNTS . a . primary ] ) ] ,
'should not have emitted notification'
'should not have emitted notification'
)
)
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. c ] ,
notifications [ DOMA INS. c . origin ] ,
[ NOTIFICATIONS . newAccounts ( [ ACCOUNTS . a . primary ] ) ] ,
[ NOTIFICATIONS . newAccounts ( [ ACCOUNTS . a . primary ] ) ] ,
'should not have emitted notification'
'should not have emitted notification'
)
)
@ -812,12 +812,12 @@ describe('permissions controller', function () {
await onPreferencesUpdate ( { selectedAddress : EXTRA _ACCOUNT } )
await onPreferencesUpdate ( { selectedAddress : EXTRA _ACCOUNT } )
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. b ] ,
notifications [ DOMA INS. b . origin ] ,
[ NOTIFICATIONS . newAccounts ( [ EXTRA _ACCOUNT ] ) ] ,
[ NOTIFICATIONS . newAccounts ( [ EXTRA _ACCOUNT ] ) ] ,
'should have emitted notification'
'should have emitted notification'
)
)
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. c ] , [ ] ,
notifications [ DOMA INS. c . origin ] , [ ] ,
'should not have emitted notification'
'should not have emitted notification'
)
)
} )
} )
@ -831,12 +831,12 @@ describe('permissions controller', function () {
await onPreferencesUpdate ( { selectedAddress : ACCOUNTS . a . permitted [ 1 ] } )
await onPreferencesUpdate ( { selectedAddress : ACCOUNTS . a . permitted [ 1 ] } )
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. b ] ,
notifications [ DOMA INS. b . origin ] ,
[ NOTIFICATIONS . newAccounts ( [ ACCOUNTS . a . permitted [ 1 ] ] ) ] ,
[ NOTIFICATIONS . newAccounts ( [ ACCOUNTS . a . permitted [ 1 ] ] ) ] ,
'should have emitted notification'
'should have emitted notification'
)
)
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. c ] ,
notifications [ DOMA INS. c . origin ] ,
[ NOTIFICATIONS . newAccounts ( [ ACCOUNTS . c . primary ] ) ] ,
[ NOTIFICATIONS . newAccounts ( [ ACCOUNTS . c . primary ] ) ] ,
'should have emitted notification'
'should have emitted notification'
)
)
@ -1113,7 +1113,7 @@ describe('permissions controller', function () {
let middleware
let middleware
assert . doesNotThrow (
assert . doesNotThrow (
( ) => {
( ) => {
middleware = permController . createMiddleware ( { origin : ORIG INS. a } )
middleware = permController . createMiddleware ( { origin : DOMA INS. a . origin } )
} ,
} ,
'should not throw'
'should not throw'
)
)
@ -1137,7 +1137,7 @@ describe('permissions controller', function () {
assert . doesNotThrow (
assert . doesNotThrow (
( ) => {
( ) => {
middleware = permController . createMiddleware ( {
middleware = permController . createMiddleware ( {
origin : ORIG INS. a ,
origin : DOMA INS. a . origin ,
extensionId ,
extensionId ,
} )
} )
} ,
} ,
@ -1157,13 +1157,13 @@ describe('permissions controller', function () {
const metadataStore = permController . store . getState ( ) [ METADATA _STORE _KEY ]
const metadataStore = permController . store . getState ( ) [ METADATA _STORE _KEY ]
assert . deepEqual (
assert . deepEqual (
metadataStore [ ORIG INS. a ] , { extensionId , lastUpdated : 1 } ,
metadataStore [ DOMA INS. a . origin ] , { extensionId , lastUpdated : 1 } ,
'metadata should be stored'
'metadata should be stored'
)
)
} )
} )
} )
} )
describe ( 'notifyDomain ' , function ( ) {
describe ( 'notifyAccountsChanged ' , function ( ) {
let notifications , permController
let notifications , permController
@ -1173,11 +1173,11 @@ describe('permissions controller', function () {
sinon . spy ( permController . permissionsLog , 'updateAccountsHistory' )
sinon . spy ( permController . permissionsLog , 'updateAccountsHistory' )
} )
} )
it ( 'notifyDomain handles accountsChanged ' , async function ( ) {
it ( 'notifyAccountsChanged records history and sends notification ' , async function ( ) {
permController . notifyDomain (
permController . notifyAccountsChanged (
ORIG INS. a ,
DOMA INS. a . origin ,
NOTIFICATIONS . newAccounts ( ACCOUNTS . a . permitted ) ,
ACCOUNTS . a . permitted ,
)
)
assert . ok (
assert . ok (
@ -1186,25 +1186,51 @@ describe('permissions controller', function () {
)
)
assert . deepEqual (
assert . deepEqual (
notifications [ ORIG INS. a ] ,
notifications [ DOMA INS. a . origin ] ,
[ NOTIFICATIONS . newAccounts ( ACCOUNTS . a . permitted ) ] ,
[ NOTIFICATIONS . newAccounts ( ACCOUNTS . a . permitted ) ] ,
'origin should have correct notification'
'origin should have correct notification'
)
)
} )
} )
it ( 'notifyDomain handles notifications other than accountsChanged ' , async function ( ) {
it ( 'notifyAccountsChanged throws on invalid origin ' , async function ( ) {
permController . notifyDomain ( ORIGINS . a , NOTIFICATIONS . test ( ) )
assert . throws (
( ) => permController . notifyAccountsChanged (
4 ,
ACCOUNTS . a . permitted ,
) ,
ERRORS . notifyAccountsChanged . invalidOrigin ( 4 ) ,
'should throw expected error for non-string origin'
)
assert . ok (
assert . throws (
permController . permissionsLog . updateAccountsHistory . notCalled ,
( ) => permController . notifyAccountsChanged (
'permissionsLog.updateAccountsHistory should not have been called'
'' ,
ACCOUNTS . a . permitted ,
) ,
ERRORS . notifyAccountsChanged . invalidOrigin ( '' ) ,
'should throw expected error for empty string origin'
)
)
} )
assert . deepEqual (
it ( 'notifyAccountsChanged throws on invalid accounts' , async function ( ) {
notifications [ ORIGINS . a ] ,
[ NOTIFICATIONS . test ( ) ] ,
assert . throws (
'origin should have correct notification'
( ) => permController . notifyAccountsChanged (
DOMAINS . a . origin ,
4 ,
) ,
ERRORS . notifyAccountsChanged . invalidAccounts ( ) ,
'should throw expected error for truthy non-array accounts'
)
assert . throws (
( ) => permController . notifyAccountsChanged (
DOMAINS . a . origin ,
null ,
) ,
ERRORS . notifyAccountsChanged . invalidAccounts ( ) ,
'should throw expected error for falsy non-array accounts'
)
)
} )
} )
} )
} )
@ -1236,13 +1262,13 @@ describe('permissions controller', function () {
permController . store . getState = sinon . fake . returns ( {
permController . store . getState = sinon . fake . returns ( {
[ METADATA _STORE _KEY ] : {
[ METADATA _STORE _KEY ] : {
[ ORIG INS. a ] : {
[ DOMA INS. a . origin ] : {
foo : 'bar' ,
foo : 'bar' ,
} ,
} ,
} ,
} ,
} )
} )
permController . addDomainMetadata ( ORIG INS. b , { foo : 'bar' } )
permController . addDomainMetadata ( DOMA INS. b . origin , { foo : 'bar' } )
assert . ok (
assert . ok (
permController . store . getState . called ,
permController . store . getState . called ,
@ -1255,11 +1281,12 @@ describe('permissions controller', function () {
assert . deepEqual (
assert . deepEqual (
permController . _setDomainMetadata . lastCall . args ,
permController . _setDomainMetadata . lastCall . args ,
[ {
[ {
[ ORIG INS. a ] : {
[ DOMA INS. a . origin ] : {
foo : 'bar' ,
foo : 'bar' ,
} ,
} ,
[ ORIG INS. b ] : {
[ DOMA INS. b . origin ] : {
foo : 'bar' ,
foo : 'bar' ,
host : DOMAINS . b . host ,
lastUpdated : 1 ,
lastUpdated : 1 ,
} ,
} ,
} ]
} ]
@ -1270,16 +1297,16 @@ describe('permissions controller', function () {
permController . store . getState = sinon . fake . returns ( {
permController . store . getState = sinon . fake . returns ( {
[ METADATA _STORE _KEY ] : {
[ METADATA _STORE _KEY ] : {
[ ORIG INS. a ] : {
[ DOMA INS. a . origin ] : {
foo : 'bar' ,
foo : 'bar' ,
} ,
} ,
[ ORIG INS. b ] : {
[ DOMA INS. b . origin ] : {
bar : 'baz' ,
bar : 'baz' ,
} ,
} ,
} ,
} ,
} )
} )
permController . addDomainMetadata ( ORIG INS. b , { foo : 'bar' } )
permController . addDomainMetadata ( DOMA INS. b . origin , { foo : 'bar' } )
assert . ok (
assert . ok (
permController . store . getState . called ,
permController . store . getState . called ,
@ -1292,12 +1319,13 @@ describe('permissions controller', function () {
assert . deepEqual (
assert . deepEqual (
permController . _setDomainMetadata . lastCall . args ,
permController . _setDomainMetadata . lastCall . args ,
[ {
[ {
[ ORIG INS. a ] : {
[ DOMA INS. a . origin ] : {
foo : 'bar' ,
foo : 'bar' ,
} ,
} ,
[ ORIG INS. b ] : {
[ DOMA INS. b . origin ] : {
foo : 'bar' ,
foo : 'bar' ,
bar : 'baz' ,
bar : 'baz' ,
host : DOMAINS . b . host ,
lastUpdated : 1 ,
lastUpdated : 1 ,
} ,
} ,
} ]
} ]
@ -1321,7 +1349,7 @@ describe('permissions controller', function () {
permController . _pendingSiteMetadata . add ( origin )
permController . _pendingSiteMetadata . add ( origin )
} )
} )
permController . addDomainMetadata ( ORIG INS. a , { foo : 'bar' } )
permController . addDomainMetadata ( DOMA INS. a . origin , { foo : 'bar' } )
assert . ok (
assert . ok (
permController . store . getState . called ,
permController . store . getState . called ,
@ -1330,8 +1358,9 @@ describe('permissions controller', function () {
const expectedMetadata = {
const expectedMetadata = {
... mockMetadata ,
... mockMetadata ,
[ ORIG INS. a ] : {
[ DOMA INS. a . origin ] : {
foo : 'bar' ,
foo : 'bar' ,
host : DOMAINS . a . host ,
lastUpdated : 1 ,
lastUpdated : 1 ,
} ,
} ,
}
}
@ -1359,12 +1388,12 @@ describe('permissions controller', function () {
it ( 'trims domain metadata for domains without permissions' , function ( ) {
it ( 'trims domain metadata for domains without permissions' , function ( ) {
const metadataArg = {
const metadataArg = {
[ ORIG INS. a ] : { } ,
[ DOMA INS. a . origin ] : { } ,
[ ORIG INS. b ] : { } ,
[ DOMA INS. b . origin ] : { } ,
}
}
permController . permissions . getDomains = sinon . fake . returns ( {
permController . permissions . getDomains = sinon . fake . returns ( {
[ ORIG INS. a ] : { } ,
[ DOMA INS. a . origin ] : { } ,
} )
} )
const metadataResult = permController . _trimDomainMetadata ( metadataArg )
const metadataResult = permController . _trimDomainMetadata ( metadataArg )
@ -1376,7 +1405,7 @@ describe('permissions controller', function () {
assert . deepEqual (
assert . deepEqual (
metadataResult ,
metadataResult ,
{
{
[ ORIG INS. a ] : { } ,
[ DOMA INS. a . origin ] : { } ,
} ,
} ,
'should have produced expected state'
'should have produced expected state'
)
)
@ -1404,7 +1433,7 @@ describe('permissions controller', function () {
it ( '_addPendingApproval: should throw if adding origin twice' , function ( ) {
it ( '_addPendingApproval: should throw if adding origin twice' , function ( ) {
const id = nanoid ( )
const id = nanoid ( )
const origin = ORIG INS. a
const origin = DOMA INS. a
permController . _addPendingApproval ( id , origin , noop , noop )
permController . _addPendingApproval ( id , origin , noop , noop )