Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #1917 Purge unused code from login page in developer portal #2004

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -35,15 +35,15 @@ describe('register-confirm', () => {
fn()
/* hack to flush promise https://github.com/facebook/jest/issues/2157 */
await new Promise(resolve => setImmediate(resolve))
expect(replaceSpy).toHaveBeenCalledWith(`${Routes.DEVELOPER_LOGIN}?isSuccess=1`)
expect(replaceSpy).toHaveBeenCalledWith(`${Routes.LOGIN}?isSuccess=1`)
})

it('should call replace on fail with correct params', async () => {
;(confirmRegistration as jest.Mocked<any>).mockRejectedValue('error')
const fn = handleUseEffect(mockParams)
fn()
await new Promise(resolve => setImmediate(resolve))
expect(replaceSpy).toHaveBeenCalledWith(`${Routes.DEVELOPER_LOGIN}?confirmError=1`)
expect(replaceSpy).toHaveBeenCalledWith(`${Routes.LOGIN}?confirmError=1`)
})
})
})
Original file line number Diff line number Diff line change
@@ -17,8 +17,8 @@ exports[`Login should match a snapshot 1`] = `
initialEntries={
Array [
Object {
"key": "clientLoginRoute",
"pathname": "/client/login",
"key": "loginRoute",
"pathname": "/developer/login",
},
]
}
@@ -33,8 +33,8 @@ exports[`Login should match a snapshot 1`] = `
"entries": Array [
Object {
"hash": "",
"key": "clientLoginRoute",
"pathname": "/client/login",
"key": "loginRoute",
"pathname": "/developer/login",
"search": "",
},
],
@@ -46,8 +46,8 @@ exports[`Login should match a snapshot 1`] = `
"listen": [Function],
"location": Object {
"hash": "",
"key": "clientLoginRoute",
"pathname": "/client/login",
"key": "loginRoute",
"pathname": "/developer/login",
"search": "",
},
"push": [Function],
@@ -75,8 +75,7 @@ exports[`Login should match a snapshot 1`] = `
data-test=""
>
<p>
Welcome to Reapit
Foundations
Welcome to Reapit Foundations
</p>
</section>
</Component>
@@ -87,12 +86,8 @@ exports[`Login should match a snapshot 1`] = `
>
<Component
dataTest="login-button"
disabled={false}
fullWidth={true}
loading={false}
onClick={[Function]}
type="button"
variant="primary"
>
<button
className="button is-primary is-fullwidth "
@@ -104,6 +99,24 @@ exports[`Login should match a snapshot 1`] = `
Login
</button>
</Component>
<div>
Don't have an account yet? 
<Link
to="/register"
>
<LinkAnchor
href="/register"
navigate={[Function]}
>
<a
href="/register"
onClick={[Function]}
>
Register
</a>
</LinkAnchor>
</Link>
</div>
</div>
</Component>
<Component
Original file line number Diff line number Diff line change
@@ -3,11 +3,9 @@ import { MemoryRouter } from 'react-router'
import * as ReactRedux from 'react-redux'
import { mount } from 'enzyme'
import configureStore from 'redux-mock-store'
import { Login, handleShowNotificationAfterPasswordChanged, handleChangeLoginType, onLoginButtonClick } from '../login'
import { Login, handleShowNotificationAfterPasswordChanged, onLoginButtonClick } from '../login'
import appState from '@/reducers/__stubs__/app-state'
import { showNotificationMessage } from '@/actions/notification-message'
import { authChangeLoginType } from '@/actions/auth'
import { LoginType } from '@reapit/cognito-auth'
import * as cognito from '@reapit/cognito-auth'
import messages from '@/constants/messages'
import Routes from '@/constants/routes'
@@ -38,7 +36,7 @@ describe('Login', () => {
expect(
mount(
<ReactRedux.Provider store={store}>
<MemoryRouter initialEntries={[{ pathname: Routes.CLIENT_LOGIN, key: 'clientLoginRoute' }]}>
<MemoryRouter initialEntries={[{ pathname: Routes.LOGIN, key: 'loginRoute' }]}>
<Login />
</MemoryRouter>
</ReactRedux.Provider>,
@@ -56,25 +54,11 @@ describe('Login', () => {
expect(spyLocalStorageRemoveItem).toBeCalledWith('isPasswordChanged')
})
})
describe('handleChangeLoginType', () => {
it('should run correctly', () => {
const mockLoginType: LoginType = 'CLIENT'
const fn = handleChangeLoginType(spyDispatch, mockLoginType)
fn()
expect(spyDispatch).toBeCalledWith(authChangeLoginType(mockLoginType))
})
})
describe('onLoginButtonClick', () => {
it('should run correctly', () => {
const spyRedirectToLogin = jest.spyOn(cognito, 'redirectToLogin')
const mockLoginType: LoginType = 'CLIENT'
const mockIsDeveloperFirstTimeLoginComplete = true
const mockIsClientFirstTimeLoginComplete = true
const fn = onLoginButtonClick(
mockLoginType,
mockIsDeveloperFirstTimeLoginComplete,
mockIsClientFirstTimeLoginComplete,
)
const mockIsFirstTimeLogin = true
const fn = onLoginButtonClick(mockIsFirstTimeLogin)
fn()
expect(spyRedirectToLogin).toBeCalled()
})
2 changes: 2 additions & 0 deletions packages/developer-portal/src/components/pages/login/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Login from './login'
export default Login
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import * as React from 'react'
import { useLocation, useRouteMatch } from 'react-router'
import { Link, Redirect } from 'react-router-dom'
import { Dispatch } from 'redux'
import { useSelector, useDispatch } from 'react-redux'
import { authChangeLoginType } from '@/actions/auth'
import { showNotificationMessage } from '@/actions/notification-message'
import { selectLoginSession, selectRefreshSession, selectLoginType, selectIsDesktopMode } from '@/selector/auth'
import { LoginType, redirectToLogin } from '@reapit/cognito-auth'
import { selectLoginSession, selectRefreshSession, selectIsDesktopMode } from '@/selector/auth'
import { redirectToLogin } from '@reapit/cognito-auth'
import { Button, Level, FlexContainerBasic, Section } from '@reapit/elements'
import { getLoginTypeByPath, getDefaultPathByLoginType, getDefaultRouteByLoginType } from '@/utils/auth-route'
import { getDefaultPath, getDefaultRoute } from '@/utils/auth-route'
import Routes from '@/constants/routes'
import messages from '@/constants/messages'
import {
getCookieString,
COOKIE_DEVELOPER_FIRST_TIME_LOGIN_COMPLETE,
COOKIE_CLIENT_FIRST_TIME_LOGIN_COMPLETE,
} from '@/utils/cookie'
import { getCookieString, COOKIE_DEVELOPER_FIRST_TIME_LOGIN_COMPLETE } from '@/utils/cookie'
import loginStyles from '@/styles/pages/login.scss?mod'
import logoImage from '@/assets/images/reapit-graphic.jpg'
import connectImage from '@/assets/images/reapit-connect.png'
@@ -37,45 +31,22 @@ export const handleShowNotificationAfterPasswordChanged = (
}
}

export const handleChangeLoginType = (dispatch: Dispatch, loginType: LoginType) => {
export const onLoginButtonClick = (isFirtTimeLogin: boolean) => {
return () => {
dispatch(authChangeLoginType(loginType))
}
}

export const onLoginButtonClick = (
loginType: LoginType,
isDeveloperFirstTimeLoginComplete: boolean,
isClientFirstTimeLoginComplete: boolean,
) => {
return () => {
const redirectRoute = getDefaultRouteByLoginType({
loginType,
isDeveloperFirstTimeLoginComplete,
isClientFirstTimeLoginComplete,
})

redirectToLogin(window.reapit.config.cognitoClientId, redirectRoute, loginType)
const redirectRoute = getDefaultRoute(isFirtTimeLogin)
redirectToLogin(window.reapit.config.cognitoClientId, redirectRoute, 'DEVELOPER')
}
}

export const Login: React.FunctionComponent<LoginProps> = () => {
const location = useLocation()
let developerLoginRouteMatch = useRouteMatch(Routes.DEVELOPER_LOGIN)
let clientLoginRouteMatch = useRouteMatch(Routes.CLIENT_LOGIN)
const dispatch = useDispatch()
const loginType = useSelector(selectLoginType)
const loginSession = useSelector(selectLoginSession)
const refreshSession = useSelector(selectRefreshSession)
const isDesktopMode = useSelector(selectIsDesktopMode)

const isPasswordChanged = localStorage.getItem('isPasswordChanged') === 'true'
const hasSession = !!loginSession || !!refreshSession
const currentLoginType = getLoginTypeByPath(location.pathname)
const isDeveloperFirstTimeLoginComplete = Boolean(getCookieString(COOKIE_DEVELOPER_FIRST_TIME_LOGIN_COMPLETE))
const isClientFirstTimeLoginComplete = Boolean(getCookieString(COOKIE_CLIENT_FIRST_TIME_LOGIN_COMPLETE))

React.useEffect(handleChangeLoginType(dispatch, currentLoginType), [dispatch, currentLoginType])
const isFirtTimeLogin = Boolean(getCookieString(COOKIE_DEVELOPER_FIRST_TIME_LOGIN_COMPLETE))

React.useEffect(handleShowNotificationAfterPasswordChanged(isPasswordChanged, localStorage, dispatch), [
isPasswordChanged,
@@ -84,14 +55,7 @@ export const Login: React.FunctionComponent<LoginProps> = () => {
])

if (hasSession) {
const redirectRoute = getDefaultPathByLoginType({
loginType: currentLoginType,
developerLoginRouteMatch,
clientLoginRouteMatch,
isDeveloperFirstTimeLoginComplete,
isClientFirstTimeLoginComplete,
isDesktopMode,
})
const redirectRoute = getDefaultPath(isDesktopMode, isFirtTimeLogin)
return <Redirect to={redirectRoute} />
}

@@ -102,27 +66,21 @@ export const Login: React.FunctionComponent<LoginProps> = () => {
<img src={connectImage} alt="Reapit Connect Graphic" />
</Level>
<Section>
<p>Welcome to Reapit {`${loginType === 'CLIENT' ? 'Marketplace' : 'Foundations'}`}</p>
<p>Welcome to Reapit Foundations</p>
</Section>
<Level className={registerLevel}>
<Button
className={loginButton}
type="button"
onClick={onLoginButtonClick(loginType, isDeveloperFirstTimeLoginComplete, isClientFirstTimeLoginComplete)}
loading={false}
variant="primary"
disabled={false}
onClick={onLoginButtonClick(isFirtTimeLogin)}
fullWidth
dataTest="login-button"
>
Login
</Button>
{loginType === 'DEVELOPER' && (
<div className={register}>
Don&apos;t have an account yet?&nbsp;
<Link to={Routes.REGISTER}>Register</Link>
</div>
)}
<div className={register}>
Don&apos;t have an account yet?&nbsp;
<Link to={Routes.REGISTER}>Register</Link>
</div>
</Level>
<FlexContainerBasic className="pt-8" centerContent>
{process.env.APP_VERSION}
Original file line number Diff line number Diff line change
@@ -10,11 +10,11 @@ export const handleUseEffect = ({ userName, verificationCode, replace }) => () =
cognitoClientId: window.reapit.config.cognitoClientId,
})
.then(() => {
replace(`${Routes.DEVELOPER_LOGIN}?isSuccess=1`)
replace(`${Routes.LOGIN}?isSuccess=1`)
})
.catch(error => {
console.log(error)
replace(`${Routes.DEVELOPER_LOGIN}?confirmError=1`)
replace(`${Routes.LOGIN}?confirmError=1`)
})
}

Original file line number Diff line number Diff line change
@@ -89,13 +89,13 @@ describe('Register', () => {
it('should redirect to developer login page', () => {
const fn = onLoginButtonClick(history, spyDispatch, '')
fn()
expect(history.replace).toBeCalledWith(`${Routes.DEVELOPER_LOGIN}`)
expect(history.replace).toBeCalledWith(`${Routes.LOGIN}`)
})
it('should run dispatch logout first and then redirect to developer login page', () => {
const fn = onLoginButtonClick(history, spyDispatch, 'testClientId')
fn()
expect(spyDispatch).toBeCalledWith(authLogout())
expect(history.replace).toBeCalledWith(`${Routes.DEVELOPER_LOGIN}`)
expect(history.replace).toBeCalledWith(`${Routes.LOGIN}`)
})
})
})
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ export const onLoginButtonClick = (history: History, dispatch: Dispatch, clientI
if (clientId) {
dispatch(authLogout())
}
history.replace(Routes.DEVELOPER_LOGIN)
history.replace(Routes.LOGIN)
}
}

@@ -189,7 +189,7 @@ export const Register: React.FunctionComponent<RegisterProps> = () => {
</Link>
) : (
<Level>
Already have an account?<Link to={Routes.DEVELOPER_LOGIN}>Login</Link>
Already have an account?<Link to={Routes.LOGIN}>Login</Link>
</Level>
)}
{formState === 'ERROR' && (
2 changes: 1 addition & 1 deletion packages/developer-portal/src/constants/routes.ts
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ const Routes = {
ADMIN_STATS: '/admin/stats',
ADMIN_BILLING: '/admin/billing',
CLIENT_LOGIN: '/client/login',
DEVELOPER_LOGIN: '/developer/login',
LOGIN: '/developer/login',
FOUR_O_FOUR: '/404',
FORGOT_PASSWORD: '/developer/forgot-password',
}
Original file line number Diff line number Diff line change
@@ -7,12 +7,7 @@ import appState from '@/reducers/__stubs__/app-state'
import { PrivateRouteWrapper, handleSetTermsAcceptFromCookie } from '../private-route-wrapper'
import { selectLoginSession, selectRefreshSession, selectLoginType } from '@/selector/auth'
import { getTokenFromQueryString, redirectToOAuth, RefreshParams } from '@reapit/cognito-auth'
import { getAuthRouteByLoginType } from '@/utils/auth-route'
import {
getCookieString,
COOKIE_DEVELOPER_FIRST_TIME_LOGIN_COMPLETE,
COOKIE_CLIENT_FIRST_TIME_LOGIN_COMPLETE,
} from '@/utils/cookie'
import { getCookieString, COOKIE_DEVELOPER_FIRST_TIME_LOGIN_COMPLETE } from '@/utils/cookie'
import {
authSetRefreshSession,
setInitDeveloperTermsAcceptedStateFromCookie,
@@ -41,8 +36,7 @@ jest.mock('@reapit/cognito-auth', () => ({
}))

jest.mock('@/utils/auth-route', () => ({
getDefaultRouteByLoginType: jest.fn(() => 'login-type-route'),
getAuthRouteByLoginType: jest.fn(() => 'auth-route'),
getDefaultRoute: jest.fn(() => 'login-type-route'),
}))

jest.mock('@/utils/cookie', () => ({
@@ -85,7 +79,6 @@ describe('PrivateRouteWrapper', () => {
expect(useSelector).toHaveBeenCalledWith(selectLoginType)
expect(useLocation).toHaveBeenCalled()
expect(getCookieString).toHaveBeenCalledWith(COOKIE_DEVELOPER_FIRST_TIME_LOGIN_COMPLETE)
expect(getCookieString).toHaveBeenCalledWith(COOKIE_CLIENT_FIRST_TIME_LOGIN_COMPLETE)
expect(getTokenFromQueryString).toHaveBeenCalledWith(
locationMock.search,
window.reapit.config.cognitoClientId,
@@ -108,21 +101,6 @@ describe('PrivateRouteWrapper', () => {
expect(dispatch).toHaveBeenCalledWith(authSetRefreshSession(refreshParams))
})

it('should call correct functions with type && location.pathname === / case', () => {
// mock useSelector to return loginSession & refreshSession
;(useLocation as jest.Mocked<any>).mockImplementationOnce(() => ({ ...locationMock, pathname: '/' }))
;(useSelector as jest.Mocked<any>).mockImplementationOnce(() => appState.auth.loginSession)
;(useSelector as jest.Mocked<any>).mockImplementationOnce(() => appState.auth.refreshSession)
mount(
<Provider store={store}>
<MemoryRouter>
<PrivateRouteWrapper path="/" />
</MemoryRouter>
</Provider>,
)
expect(getAuthRouteByLoginType).toHaveBeenCalledWith('CLIENT')
})

it('should call correct functions with !hasSession case', () => {
// mock useSelector to return loginSession & refreshSession as null
;(useLocation as jest.Mocked<any>).mockImplementationOnce(() => ({ ...locationMock, pathname: '/test' }))
Loading