diff options
| -rw-r--r-- | app/lib/backend.js | 17 | ||||
| -rw-r--r-- | app/lib/ipc-facade.js | 21 | ||||
| -rw-r--r-- | test/connect.spec.js | 16 | ||||
| -rw-r--r-- | test/mocks/ipc.js | 5 |
4 files changed, 41 insertions, 18 deletions
diff --git a/app/lib/backend.js b/app/lib/backend.js index 9513a3f08b..48ede8ff62 100644 --- a/app/lib/backend.js +++ b/app/lib/backend.js @@ -308,7 +308,7 @@ export class Backend { this._ipc.registerStateListener(newState => { log.info('Got new state from backend', newState); - const newStatus = this._backendStateToConnectionState(newState); + const newStatus = this._securityStateToConnectionState(newState); this._store.dispatch(connectionActions.connectionChange({ status: newStatus, })); @@ -317,15 +317,14 @@ export class Backend { }); } - _backendStateToConnectionState(backendState: BackendState): ConnectionState { - switch(backendState) { - case 'unsecured': - return 'disconnected'; - case 'secured': + _securityStateToConnectionState(backendState: BackendState): ConnectionState { + if (backendState.state === 'unsecured' && backendState.target_state === 'secured') { + return 'connecting'; + } else if (backendState.state === 'secured' && backendState.target_state === 'secured') { return 'connected'; - - default: - throw new Error('Unknown backend state: ' + backendState); + } else if (backendState.target_state === 'unsecured') { + return 'disconnected'; } + throw new Error('Unsupported state/target state combination: ' + JSON.stringify(backendState)); } } diff --git a/app/lib/ipc-facade.js b/app/lib/ipc-facade.js index b5541e4297..3dac5d188a 100644 --- a/app/lib/ipc-facade.js +++ b/app/lib/ipc-facade.js @@ -20,7 +20,11 @@ const LocationSchema = object({ city: string, }); -export type BackendState = 'secured' | 'unsecured'; +export type SecurityState = 'secured' | 'unsecured'; +export type BackendState = { + state: SecurityState, + target_state: SecurityState, +}; export interface IpcFacade { getAccountData(AccountNumber): Promise<AccountData>, @@ -120,8 +124,19 @@ export class RealIpc implements IpcFacade { } _parseBackendState(raw: mixed): BackendState { - if (raw === 'secured' || raw === 'unsecured') { - return raw; + if (raw && raw.state && raw.target_state) { + + const uncheckedRaw: any = raw; + + const states: Array<SecurityState> = ['secured', 'unsecured']; + const correctState = states.includes(uncheckedRaw.state); + const correctTargetState = states.includes(uncheckedRaw.target_state); + + if (!correctState || !correctTargetState) { + throw new InvalidReply(raw); + } + + return (uncheckedRaw: BackendState); } else { throw new InvalidReply(raw); } diff --git a/test/connect.spec.js b/test/connect.spec.js index 56fdb76afc..13f4bef78a 100644 --- a/test/connect.spec.js +++ b/test/connect.spec.js @@ -43,25 +43,31 @@ describe('connect', () => { }, done); }); - it('should update the store on \'secured\' state from the backend', () => { + it('should correctly deduce \'connected\' from backend states', () => { const { store, mockIpc } = setupBackendAndStore(); expect(store.getState().connection.status).not.to.equal('connected'); - mockIpc.sendNewState('secured'); + mockIpc.sendNewState({ state: 'secured', target_state: 'secured' }); expect(store.getState().connection.status).to.equal('connected'); + }); + + it('should correctly deduce \'connecting\' from backend states', () => { + const { store, mockIpc } = setupBackendAndStore(); + expect(store.getState().connection.status).not.to.equal('connecting'); + mockIpc.sendNewState({ state: 'unsecured', target_state: 'secured' }); + expect(store.getState().connection.status).to.equal('connecting'); }); - it('should update the store on \'unsecured\' state from the backend', () => { + it('should correctly deduce \'disconnected\' from backend states', () => { const { store, mockIpc } = setupBackendAndStore(); store.dispatch(connectionActions.connectionChange({ status: 'connected', })); expect(store.getState().connection.status).not.to.equal('disconnected'); - mockIpc.sendNewState('unsecured'); + mockIpc.sendNewState({ state: 'unsecured', target_state: 'unsecured' }); expect(store.getState().connection.status).to.equal('disconnected'); - }); }); diff --git a/test/mocks/ipc.js b/test/mocks/ipc.js index cc6146be73..25c8cdf7fa 100644 --- a/test/mocks/ipc.js +++ b/test/mocks/ipc.js @@ -45,7 +45,10 @@ export function newMockIpc() { })); }, getState: () => { - return new Promise(r => r('unsecured')); + return new Promise(r => r({ + state: 'unsecured', + target_state:'unsecured', + })); }, registerStateListener: (listener: (BackendState) => void) => { stateListeners.push(listener); |
