@ -38,6 +38,7 @@ const {
ORIGINS ,
PERM _NAMES ,
REQUEST _IDS ,
EXTRA _ACCOUNT ,
} = constants
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 ( ) {
preferences = {
getState : ( ) => {
return { selectedAddress : DUMMY _ACCOUNT }
} ,
subscribe : sinon . stub ( ) ,
}
notifications = initNotifications ( )
permController = initPermController ( notifications )
permController = new PermissionsController ( {
... getPermControllerOpts ( ) ,
notifyDomain : getNotifyDomain ( notifications ) ,
notifyAllDomains : getNotifyAllDomains ( notifications ) ,
preferences ,
} )
grantPermissions (
permController , ORIGINS . a ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNT _ARRAYS . a )
permController , ORIGINS . b ,
PERMS . finalizedRequests . eth _accounts ( [ ... ACCOUNT _ARRAYS . a , EXTRA _ACCOUNT ] )
)
grantPermissions (
permController , ORIGINS . b ,
PERMS . finalizedRequests . eth _accounts ( ACCOUNT _ARRAYS . b )
permController , ORIGINS . c ,
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 (
permController . handleNewAccountSelected ( { } , DUMMY _ACCOUNT ) ,
ERRORS . handleNew AccountSelected . invalidParams ( ) ,
'should throw if origin non- string'
( ) => onPreferencesUpdate ( { selectedAddress : { } } ) ,
ERRORS . _ handleAccountSelected. invalidParams ( ) ,
'should throw if account is not a string'
)
} )
await assert . rejects (
permController . handleNewAccountSelected ( '' , DUMMY _ACCOUNT ) ,
ERRORS . handleNewAccountSelected . invalidParams ( ) ,
'should throw if origin empty string'
)
it ( 'should do nothing if account not permitted for any origins' , async function ( ) {
assert ( preferences . subscribe . calledOnce )
assert ( preferences . subscribe . firstCall . args . length === 1 )
const onPreferencesUpdate = preferences . subscribe . firstCall . args [ 0 ]
await assert . rejects (
permController . handleNewAccountSelected ( ORIGINS . a , { } ) ,
ERRORS . handleNewAccountSelected . invalidParams ( ) ,
'should throw if account non-string'
)
await onPreferencesUpdate ( { selectedAddress : DUMMY _ACCOUNT } )
await assert . rejects (
permController . handleNewAccountSelected ( ORIGINS . a , '' ) ,
ERRORS . handleNewAccountSelected . invalidParams ( ) ,
'should throw if account empty string'
assert . deepEqual (
notifications [ ORIGINS . b ] , [ ] ,
'should not have emitted notification'
)
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 (
ORIGINS . b , ACCOUNT _ARRAYS . c [ 0 ]
)
await onPreferencesUpdate ( { selectedAddress : ACCOUNT _ARRAYS . a [ 0 ] } )
assert . deepEqual (
notifications [ ORIGINS . b ] , [ ] ,
'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 (
ORIGINS . a , ACCOUNT _ARRAYS . a [ 0 ]
)
await onPreferencesUpdate ( { selectedAddress : EXTRA _ACCOUNT } )
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'
)
} )
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 (
ORIGINS . a , ACCOUNT _ARRAYS . a [ 1 ]
)
await onPreferencesUpdate ( { selectedAddress : 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 (
notifications [ ORIGINS . a ] ,
[ NOTIFICATIONS . newAccounts ( [ ... ACCOUNT _ARRAYS . a ] . reverse ( ) ) ] ,
notifications [ ORIGINS . b ] ,
[ NOTIFICATIONS . newAccounts ( [ ... expectedAccounts , EXTRA _ACCOUNT ] ) ] ,
'should have emitted notification'
)
assert . deepEqual (
notifications [ ORIGINS . c ] ,
[ NOTIFICATIONS . newAccounts ( expectedAccounts ) ] ,
'should have emitted notification'
)
} )