@ -38,6 +38,7 @@ const {
ORIGINS ,
ORIGINS ,
PERM _NAMES ,
PERM _NAMES ,
REQUEST _IDS ,
REQUEST _IDS ,
EXTRA _ACCOUNT ,
} = constants
} = constants
const initNotifications = ( ) => {
const initNotifications = ( ) => {
@ -737,83 +738,116 @@ describe('permissions controller', function () {
} )
} )
} )
} )
describe ( 'handleNewAccountSelected ' , function ( ) {
describe ( 'preferences state update ' , function ( ) {
let permController , notifications
let permController , notifications , preferences
beforeEach ( function ( ) {
beforeEach ( function ( ) {
preferences = {
getState : ( ) => {
return { selectedAddress : DUMMY _ACCOUNT }
} ,
subscribe : sinon . stub ( ) ,
}
notifications = initNotifications ( )
notifications = initNotifications ( )
permController = initPermController ( notifications )
permController = new PermissionsController ( {
... getPermControllerOpts ( ) ,
notifyDomain : getNotifyDomain ( notifications ) ,
notifyAllDomains : getNotifyAllDomains ( notifications ) ,
preferences ,
} )
grantPermissions (
grantPermissions (
permController , ORIGINS . a ,
permController , ORIGINS . b ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNT _ARRAYS . a )
PERMS . finalizedRequests . eth _accounts ( [ ... ACCOUNT _ARRAYS . a , EXTRA _ACCOUNT ] )
)
)
grantPermissions (
grantPermissions (
permController , ORIGINS . b ,
permController , ORIGINS . c ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNT _ARRAYS . b )
PERMS . finalizedRequests . eth _accounts ( ACCOUNT _ARRAYS . a )
)
)
} )
} )
it ( 'throws if invalid origin or account' , async function ( ) {
it ( 'should throw if given invalid account' , async function ( ) {
assert ( preferences . subscribe . calledOnce )
assert ( preferences . subscribe . firstCall . args . length === 1 )
const onPreferencesUpdate = preferences . subscribe . firstCall . args [ 0 ]
await assert . rejects (
await assert . rejects (
permController . handleNewAccountSelected ( { } , DUMMY _ACCOUNT ) ,
( ) => onPreferencesUpdate ( { selectedAddress : { } } ) ,
ERRORS . handleNew AccountSelected . invalidParams ( ) ,
ERRORS . _ handleAccountSelected. invalidParams ( ) ,
'should throw if origin non- string'
'should throw if account is not a string'
)
)
} )
await assert . rejects (
it ( 'should do nothing if account not permitted for any origins' , async function ( ) {
permController . handleNewAccountSelected ( '' , DUMMY _ACCOUNT ) ,
assert ( preferences . subscribe . calledOnce )
ERRORS . handleNewAccountSelected . invalidParams ( ) ,
assert ( preferences . subscribe . firstCall . args . length === 1 )
'should throw if origin empty string'
const onPreferencesUpdate = preferences . subscribe . firstCall . args [ 0 ]
)
await assert . rejects (
await onPreferencesUpdate ( { selectedAddress : DUMMY _ACCOUNT } )
permController . handleNewAccountSelected ( ORIGINS . a , { } ) ,
ERRORS . handleNewAccountSelected . invalidParams ( ) ,
'should throw if account non-string'
)
await assert . rejects (
assert . deepEqual (
permController . handleNewAccountSelected ( ORIGINS . a , '' ) ,
notifications [ ORIGINS . b ] , [ ] ,
ERRORS . handleNewAccountSelected . invalidParams ( ) ,
'should not have emitted notification'
'should throw if account empty string'
)
assert . deepEqual (
notifications [ ORIGINS . c ] , [ ] ,
'should not have emitted notification'
)
)
} )
} )
it ( 'does nothing if account not permitted for origin' , async function ( ) {
it ( 'should do nothing if account already first in array for each connected site' , async function ( ) {
assert ( preferences . subscribe . calledOnce )
assert ( preferences . subscribe . firstCall . args . length === 1 )
const onPreferencesUpdate = preferences . subscribe . firstCall . args [ 0 ]
await permController . handleNewAccountSelected (
await onPreferencesUpdate ( { selectedAddress : ACCOUNT _ARRAYS . a [ 0 ] } )
ORIGINS . b , ACCOUNT _ARRAYS . c [ 0 ]
)
assert . deepEqual (
assert . deepEqual (
notifications [ ORIGINS . b ] , [ ] ,
notifications [ ORIGINS . b ] , [ ] ,
'should not have emitted notification'
'should not have emitted notification'
)
)
assert . deepEqual (
notifications [ ORIGINS . c ] , [ ] ,
'should not have emitted notification'
)
} )
} )
it ( 'does nothing if account already first in array' , async function ( ) {
it ( 'should emit notification just for connected domains' , async function ( ) {
assert ( preferences . subscribe . calledOnce )
assert ( preferences . subscribe . firstCall . args . length === 1 )
const onPreferencesUpdate = preferences . subscribe . firstCall . args [ 0 ]
await permController . handleNewAccountSelected (
await onPreferencesUpdate ( { selectedAddress : EXTRA _ACCOUNT } )
ORIGINS . a , ACCOUNT _ARRAYS . a [ 0 ]
)
assert . deepEqual (
assert . deepEqual (
notifications [ ORIGINS . a ] , [ ] ,
notifications [ ORIGINS . b ] ,
[ NOTIFICATIONS . newAccounts ( [ EXTRA _ACCOUNT , ... ACCOUNT _ARRAYS . a ] ) ] ,
'should have emitted notification'
)
assert . deepEqual (
notifications [ ORIGINS . c ] , [ ] ,
'should not have emitted notification'
'should not have emitted notification'
)
)
} )
} )
it ( 'emits notification if selected account not first in array' , async function ( ) {
it ( 'should emit notification for multiple connected domains' , async function ( ) {
assert ( preferences . subscribe . calledOnce )
assert ( preferences . subscribe . firstCall . args . length === 1 )
const onPreferencesUpdate = preferences . subscribe . firstCall . args [ 0 ]
await permController . handleNewAccountSelected (
await onPreferencesUpdate ( { selectedAddress : ACCOUNT _ARRAYS . a [ 1 ] } )
ORIGINS . a , ACCOUNT _ARRAYS . a [ 1 ]
)
const accountsWithoutFirst = ACCOUNT _ARRAYS . a
. filter ( ( account ) => account !== ACCOUNT _ARRAYS . a [ 1 ] )
const expectedAccounts = [ ACCOUNT _ARRAYS . a [ 1 ] , ... accountsWithoutFirst ]
assert . deepEqual (
assert . deepEqual (
notifications [ ORIGINS . a ] ,
notifications [ ORIGINS . b ] ,
[ NOTIFICATIONS . newAccounts ( [ ... ACCOUNT _ARRAYS . a ] . reverse ( ) ) ] ,
[ NOTIFICATIONS . newAccounts ( [ ... expectedAccounts , EXTRA _ACCOUNT ] ) ] ,
'should have emitted notification'
)
assert . deepEqual (
notifications [ ORIGINS . c ] ,
[ NOTIFICATIONS . newAccounts ( expectedAccounts ) ] ,
'should have emitted notification'
'should have emitted notification'
)
)
} )
} )