From 24c51e5fd77bbfdb9b1ae2a1c119d0d44461d4e0 Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Thu, 20 Aug 2020 17:43:25 -0700 Subject: [PATCH] redux: In REALM_ADD and LOGIN_SUCCESS, make `realm` be a URL object. As in the preceding handful of commits, stringify the realm exactly where we need it as a string. These should be the last sites in which it's not totally clear that the realm is well-formed data. --- src/__tests__/lib/exampleData.js | 2 +- src/account/__tests__/accountsReducer-test.js | 12 ++++++------ src/account/accountActions.js | 4 ++-- src/account/accountsReducer.js | 17 ++++++----------- src/actionTypes.js | 4 ++-- src/start/AuthScreen.js | 2 +- src/start/DevAuthScreen.js | 2 +- src/start/PasswordAuthScreen.js | 2 +- src/start/RealmScreen.js | 2 +- 9 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/__tests__/lib/exampleData.js b/src/__tests__/lib/exampleData.js index a400c9c706c..5f2885600fb 100644 --- a/src/__tests__/lib/exampleData.js +++ b/src/__tests__/lib/exampleData.js @@ -423,7 +423,7 @@ export const action = deepFreeze({ }, login_success: { type: LOGIN_SUCCESS, - realm: selfAccount.realm.toString(), + realm: selfAccount.realm, email: selfAccount.email, apiKey: selfAccount.apiKey, }, diff --git a/src/account/__tests__/accountsReducer-test.js b/src/account/__tests__/accountsReducer-test.js index 14782bedc0f..ef8596e27dc 100644 --- a/src/account/__tests__/accountsReducer-test.js +++ b/src/account/__tests__/accountsReducer-test.js @@ -28,7 +28,7 @@ describe('accountsReducer', () => { test('if no account with this realm exists, prepend new one, with empty email/apiKey', () => { const newRealm = new URL('https://new.realm.org'); - const action = deepFreeze({ ...baseAction, realm: newRealm.toString() }); + const action = deepFreeze({ ...baseAction, realm: newRealm }); expect(accountsReducer(prevState, action)).toEqual([ eg.makeAccount({ realm: newRealm, email: '', apiKey: '' }), account1, @@ -37,7 +37,7 @@ describe('accountsReducer', () => { }); test('if account with this realm exists, move to front of list', () => { - const action = deepFreeze({ ...baseAction, realm: account2.realm.toString() }); + const action = deepFreeze({ ...baseAction, realm: account2.realm }); expect(accountsReducer(prevState, action)).toEqual([account2, account1]); }); }); @@ -46,7 +46,7 @@ describe('accountsReducer', () => { const existingAccountBase = eg.makeAccount({}); const baseAction = deepFreeze({ type: REALM_ADD, - realm: existingAccountBase.realm.toString(), + realm: existingAccountBase.realm, zulipFeatureLevel: eg.zulipFeatureLevel, zulipVersion: eg.zulipVersion, }); @@ -168,7 +168,7 @@ describe('accountsReducer', () => { type: LOGIN_SUCCESS, apiKey: newAccount.apiKey, email: newAccount.email, - realm: newAccount.realm.toString(), + realm: newAccount.realm, }); const expectedState = [{ ...newAccount, zulipVersion: account1.zulipVersion }, account2]; @@ -190,7 +190,7 @@ describe('accountsReducer', () => { type: LOGIN_SUCCESS, apiKey: newAccount.apiKey, email: newAccount.email, - realm: newAccount.realm.toString(), + realm: newAccount.realm, }); const expectedState = [newAccount, account1, account2]; @@ -209,7 +209,7 @@ describe('accountsReducer', () => { const action = deepFreeze({ type: LOGIN_SUCCESS, apiKey: newAccount.apiKey, - realm: newAccount.realm.toString(), + realm: newAccount.realm, email: newAccount.email, }); diff --git a/src/account/accountActions.js b/src/account/accountActions.js index ab948cdb4aa..37e40e99b91 100644 --- a/src/account/accountActions.js +++ b/src/account/accountActions.js @@ -15,7 +15,7 @@ export const switchAccount = (index: number): Action => ({ }); export const realmAdd = ( - realm: string, + realm: URL, zulipFeatureLevel: number, zulipVersion: ZulipVersion, ): Action => ({ @@ -30,7 +30,7 @@ export const removeAccount = (index: number): Action => ({ index, }); -export const loginSuccess = (realm: string, email: string, apiKey: string): Action => ({ +export const loginSuccess = (realm: URL, email: string, apiKey: string): Action => ({ type: LOGIN_SUCCESS, realm, email, diff --git a/src/account/accountsReducer.js b/src/account/accountsReducer.js index 102cb769ccd..c9c06e6705e 100644 --- a/src/account/accountsReducer.js +++ b/src/account/accountsReducer.js @@ -16,7 +16,9 @@ import { NULL_ARRAY } from '../nullObjects'; const initialState = NULL_ARRAY; const realmAdd = (state, action) => { - const accountIndex = state.findIndex(account => account.realm.toString() === action.realm); + const accountIndex = state.findIndex( + account => account.realm.toString() === action.realm.toString(), + ); if (accountIndex !== -1) { const newAccount = { @@ -29,7 +31,7 @@ const realmAdd = (state, action) => { return [ { - realm: new URL(action.realm), + realm: action.realm, apiKey: '', email: '', ackedPushToken: null, @@ -67,17 +69,10 @@ const findAccount = (state: AccountsState, identity: Identity): number => { const loginSuccess = (state, action) => { const { realm, email, apiKey } = action; - const accountIndex = findAccount(state, { realm: new URL(realm), email }); + const accountIndex = findAccount(state, { realm, email }); if (accountIndex === -1) { return [ - { - realm: new URL(realm), - email, - apiKey, - ackedPushToken: null, - zulipVersion: null, - zulipFeatureLevel: null, - }, + { realm, email, apiKey, ackedPushToken: null, zulipVersion: null, zulipFeatureLevel: null }, ...state, ]; } diff --git a/src/actionTypes.js b/src/actionTypes.js index 2a03fbb0b3e..db6394f565a 100644 --- a/src/actionTypes.js +++ b/src/actionTypes.js @@ -142,7 +142,7 @@ type AccountSwitchAction = {| type RealmAddAction = {| type: typeof REALM_ADD, - realm: string, + realm: URL, zulipFeatureLevel: number, zulipVersion: ZulipVersion, |}; @@ -154,7 +154,7 @@ type AccountRemoveAction = {| type LoginSuccessAction = {| type: typeof LOGIN_SUCCESS, - realm: string, + realm: URL, email: string, apiKey: string, |}; diff --git a/src/start/AuthScreen.js b/src/start/AuthScreen.js index 985f182ba50..a8a4a718d7a 100644 --- a/src/start/AuthScreen.js +++ b/src/start/AuthScreen.js @@ -226,7 +226,7 @@ class AuthScreen extends PureComponent { const { dispatch, realm } = this.props; const auth = webAuth.authFromCallbackUrl(event.url, otp, realm); if (auth) { - dispatch(loginSuccess(auth.realm.toString(), auth.email, auth.apiKey)); + dispatch(loginSuccess(auth.realm, auth.email, auth.apiKey)); } }; diff --git a/src/start/DevAuthScreen.js b/src/start/DevAuthScreen.js index 3b2cf1fd50d..10259c570a8 100644 --- a/src/start/DevAuthScreen.js +++ b/src/start/DevAuthScreen.js @@ -73,7 +73,7 @@ class DevAuthScreen extends PureComponent { try { const { api_key } = await api.devFetchApiKey(partialAuth, email); - this.props.dispatch(loginSuccess(partialAuth.realm.toString(), email, api_key)); + this.props.dispatch(loginSuccess(partialAuth.realm, email, api_key)); this.setState({ progress: false }); } catch (err) { this.setState({ progress: false, error: err.data && err.data.msg }); diff --git a/src/start/PasswordAuthScreen.js b/src/start/PasswordAuthScreen.js index 6d90687f27d..21a4c7d4b52 100644 --- a/src/start/PasswordAuthScreen.js +++ b/src/start/PasswordAuthScreen.js @@ -57,7 +57,7 @@ class PasswordAuthScreen extends PureComponent { try { const fetchedKey = await api.fetchApiKey(partialAuth, email, password); this.setState({ progress: false }); - dispatch(loginSuccess(partialAuth.realm.toString(), fetchedKey.email, fetchedKey.api_key)); + dispatch(loginSuccess(partialAuth.realm, fetchedKey.email, fetchedKey.api_key)); } catch (err) { this.setState({ progress: false, diff --git a/src/start/RealmScreen.js b/src/start/RealmScreen.js index aecf8478253..73194af3606 100644 --- a/src/start/RealmScreen.js +++ b/src/start/RealmScreen.js @@ -64,7 +64,7 @@ class RealmScreen extends PureComponent { const serverSettings: ApiResponseServerSettings = await api.getServerSettings(parsedRealm); dispatch( realmAdd( - realmInputValue, + parsedRealm, serverSettings.zulip_feature_level ?? 0, new ZulipVersion(serverSettings.zulip_version), ),