diff options
| author | Erik Larkö <erik@mullvad.net> | 2017-07-25 14:02:33 +0200 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-07-28 11:25:16 +0200 |
| commit | e2bf350e0742724f86a131c7421fea50c839f378 (patch) | |
| tree | 983f2f903afb67298f0c5e6a1ec1fe69264879eb | |
| parent | a196629083e1fb634a1d65e816a1b4bdfbdeb072 (diff) | |
| download | mullvadvpn-e2bf350e0742724f86a131c7421fea50c839f378.tar.xz mullvadvpn-e2bf350e0742724f86a131c7421fea50c839f378.zip | |
Added START_LOGIN action
| -rw-r--r-- | app/lib/backend.js | 11 | ||||
| -rw-r--r-- | app/redux/account/actions.js | 18 | ||||
| -rw-r--r-- | app/redux/account/reducers.js | 9 | ||||
| -rw-r--r-- | app/redux/store.js | 4 | ||||
| -rw-r--r-- | test/login.spec.js | 29 |
5 files changed, 41 insertions, 30 deletions
diff --git a/app/lib/backend.js b/app/lib/backend.js index 3c322afa6d..53e724fcfc 100644 --- a/app/lib/backend.js +++ b/app/lib/backend.js @@ -148,16 +148,12 @@ export class Backend { } - login(accountNumber: string) { + login(accountNumber: string): Promise<void> { log.info('Attempting to login with account number', accountNumber); - this._store.dispatch(accountActions.loginChange({ - accountNumber: accountNumber, - status: 'logging in', - error: null, - })); + this._store.dispatch(accountActions.startLogin(accountNumber)); - this._ipc.getAccountData(accountNumber) + return this._ipc.getAccountData(accountNumber) .then( response => { log.info('Account exists', response); @@ -169,6 +165,7 @@ export class Backend { this._store.dispatch(accountActions.loginChange({ status: 'ok', + accountNumber: accountNumber, paidUntil: accountData.paid_until, error: null, })); diff --git a/app/redux/account/actions.js b/app/redux/account/actions.js index 0b828c678a..5e423f3aa8 100644 --- a/app/redux/account/actions.js +++ b/app/redux/account/actions.js @@ -3,11 +3,25 @@ import type { Backend } from '../../lib/backend'; import type { AccountReduxState } from './reducers.js'; -export type LoginChangeAction = { +type StartLoginAction = { + type: 'START_LOGIN', + accountNumber: string, +}; + +type LoginChangeAction = { type:'LOGIN_CHANGE', newData: $Shape<AccountReduxState>, }; +export type AccountAction = StartLoginAction | LoginChangeAction; + +function startLogin(accountNumber: string): StartLoginAction { + return { + type: 'START_LOGIN', + accountNumber: accountNumber, + }; +} + function loginChange(data: $Shape<AccountReduxState>): LoginChangeAction { return { type: 'LOGIN_CHANGE', @@ -18,4 +32,4 @@ function loginChange(data: $Shape<AccountReduxState>): LoginChangeAction { const login = (backend: Backend, account: string) => () => backend.login(account); const logout = (backend: Backend) => () => backend.logout(); -export default { login, logout, loginChange }; +export default { login, logout, loginChange, startLogin }; diff --git a/app/redux/account/reducers.js b/app/redux/account/reducers.js index d1fd284ee5..2ac8595d90 100644 --- a/app/redux/account/reducers.js +++ b/app/redux/account/reducers.js @@ -20,8 +20,15 @@ const initialState: AccountReduxState = { export default function(state: AccountReduxState = initialState, action: ReduxAction): AccountReduxState { - if (action.type === 'LOGIN_CHANGE') { + switch (action.type) { + case 'LOGIN_CHANGE': return { ...state, ...action.newData }; + case 'START_LOGIN': + return { ...state, ...{ + status: 'logging in', + accountNumber: action.accountNumber, + error: null, + }}; } return state; diff --git a/app/redux/store.js b/app/redux/store.js index 0a72a1869a..b4aa0375e1 100644 --- a/app/redux/store.js +++ b/app/redux/store.js @@ -17,7 +17,7 @@ import type { ConnectionReduxState } from './connection/reducers.js'; import type { SettingsReduxState } from './settings/reducers.js'; import type { ConnectionAction } from './connection/actions.js'; -import type { LoginChangeAction } from './account/actions.js'; +import type { AccountAction } from './account/actions.js'; import type { UpdateSettingsAction } from './settings/actions.js'; export type ReduxState = { @@ -26,7 +26,7 @@ export type ReduxState = { settings: SettingsReduxState }; -export type ReduxAction = LoginChangeAction +export type ReduxAction = AccountAction | UpdateSettingsAction | ConnectionAction; diff --git a/test/login.spec.js b/test/login.spec.js index 950205804c..5c7e0fe540 100644 --- a/test/login.spec.js +++ b/test/login.spec.js @@ -25,25 +25,22 @@ describe('Logging in', () => { chain.onSuccessOrFailure(done); - const action: any = accountActions.login(backend, '123'); - store.dispatch(action); + store.dispatch(accountActions.login(backend, '123')); }); - it('should put the account data in the state', (done) => { + it('should put the account data in the state', () => { const { store, backend, mockIpc } = setupBackendAndStore(); mockIpc.getAccountData = () => new Promise(r => r({ paid_until: '2001-01-01T00:00:00', })); - const action: any = accountActions.login(backend, '123'); - store.dispatch(action); - - checkNextTick( () => { - const state = store.getState().account; - expect(state.status).to.equal('ok'); - expect(state.accountNumber).to.equal('123'); - expect(state.paidUntil).to.equal('2001-01-01T00:00:00'); - }, done); + return backend.login('123') + .then( () => { + const state = store.getState().account; + expect(state.status).to.equal('ok'); + expect(state.accountNumber).to.equal('123'); + expect(state.paidUntil).to.equal('2001-01-01T00:00:00'); + }); }); it('should indicate failure for non-existing accounts', (done) => { @@ -54,9 +51,7 @@ describe('Logging in', () => { }); - const action: any = accountActions.login(backend, '123'); - store.dispatch(action); - + store.dispatch(accountActions.login(backend, '123')); checkNextTick(() => { const state = store.getState().account; @@ -68,9 +63,7 @@ describe('Logging in', () => { it('should redirect to /connect after 1s after successful login', (done) => { const { store, backend } = setupBackendAndMockStore(); - const action: any = accountActions.login(backend, '123'); - store.dispatch(action); - + store.dispatch(accountActions.login(backend, '123')); setTimeout(() => { |
