From 605968c9034167a804e9f8e1bf526c3fe07cbc2e Mon Sep 17 00:00:00 2001 From: Waheed Ahmed Date: Fri, 1 Jan 2021 12:35:52 +0500 Subject: [PATCH] Fix password reset confirmation redirection. (#61) Fixed password reset redirection to dashboard page if user is already logged in. VAN-234 --- src/common-components/LoggedInRedirect.jsx | 26 -------- src/common-components/UnAuthOnlyRoute.jsx | 22 +++++++ src/common-components/index.jsx | 2 +- .../tests/LoggedInRedirect.test.jsx | 46 -------------- .../tests/UnAuthOnlyRoute.test.jsx | 63 +++++++++++++++++++ src/data/constants.js | 2 + src/index.jsx | 36 ++++++----- 7 files changed, 107 insertions(+), 90 deletions(-) delete mode 100644 src/common-components/LoggedInRedirect.jsx create mode 100644 src/common-components/UnAuthOnlyRoute.jsx delete mode 100644 src/common-components/tests/LoggedInRedirect.test.jsx create mode 100644 src/common-components/tests/UnAuthOnlyRoute.test.jsx diff --git a/src/common-components/LoggedInRedirect.jsx b/src/common-components/LoggedInRedirect.jsx deleted file mode 100644 index f3f66f94..00000000 --- a/src/common-components/LoggedInRedirect.jsx +++ /dev/null @@ -1,26 +0,0 @@ -import { useContext } from 'react'; -import PropTypes from 'prop-types'; -import { AppContext } from '@edx/frontend-platform/react'; - -import { DEFAULT_REDIRECT_URL } from '../data/constants'; - -/** - * This wrapper component redirects the requester to our default redirect url if they are - * already authenticated. - * - * @param {node} children The child nodes to render if there is an unauthenticated user. - */ -export default function LoggedInRedirect({ children }) { - const { authenticatedUser, config } = useContext(AppContext); - - if (authenticatedUser) { - global.location.href = config.LMS_BASE_URL.concat(DEFAULT_REDIRECT_URL); - return null; - } - - return children; -} - -LoggedInRedirect.propTypes = { - children: PropTypes.node.isRequired, -}; diff --git a/src/common-components/UnAuthOnlyRoute.jsx b/src/common-components/UnAuthOnlyRoute.jsx new file mode 100644 index 00000000..f136f4f3 --- /dev/null +++ b/src/common-components/UnAuthOnlyRoute.jsx @@ -0,0 +1,22 @@ +import React from 'react'; +import { Route } from 'react-router-dom'; +import { AppContext } from '@edx/frontend-platform/react'; + +import { DEFAULT_REDIRECT_URL } from '../data/constants'; + +/** + * This wrapper redirects the requester to our default redirect url if they are + * already authenticated. + */ +const UnAuthOnlyRoute = (props) => { + const { authenticatedUser, config } = React.useContext(AppContext); + + if (authenticatedUser) { + global.location.href = config.LMS_BASE_URL.concat(DEFAULT_REDIRECT_URL); + return null; + } + + return ; +}; + +export default UnAuthOnlyRoute; diff --git a/src/common-components/index.jsx b/src/common-components/index.jsx index bb310481..b9260c8d 100644 --- a/src/common-components/index.jsx +++ b/src/common-components/index.jsx @@ -1,4 +1,4 @@ export { default as HeaderLayout } from './HeaderLayout'; -export { default as LoggedInRedirect } from './LoggedInRedirect'; export { default as RedirectLogistration } from './RedirectLogistration'; export { default as registerIcons } from './RegisterFaIcons'; +export { default as UnAuthOnlyRoute } from './UnAuthOnlyRoute'; diff --git a/src/common-components/tests/LoggedInRedirect.test.jsx b/src/common-components/tests/LoggedInRedirect.test.jsx deleted file mode 100644 index 9e0f4542..00000000 --- a/src/common-components/tests/LoggedInRedirect.test.jsx +++ /dev/null @@ -1,46 +0,0 @@ -import React from 'react'; -import { mount } from 'enzyme'; - -import { getConfig } from '@edx/frontend-platform'; - -import LoggedInRedirect from '../LoggedInRedirect'; -import { DEFAULT_REDIRECT_URL } from '../../data/constants'; - -describe('LoggedInRedirect', () => { - const loggedInRedirect = ( - -
test
-
- ); - const dashboardURL = getConfig().LMS_BASE_URL.concat(DEFAULT_REDIRECT_URL); - - it('should redirect to dashboard if already logged in', () => { - delete window.location; - window.location = { href: '' }; - const user = { - username: 'gonzo', - other: 'data', - }; - const mockUseContext = jest.fn().mockImplementation(() => ({ - authenticatedUser: user, - config: getConfig(), - })); - - React.useContext = mockUseContext; - mount(loggedInRedirect); - - expect(window.location.href).toBe(dashboardURL); - }); - - it('should render child components', () => { - const mockUseContext = jest.fn().mockImplementation(() => ({ - authenticatedUser: null, - config: null, - })); - - React.useContext = mockUseContext; - const wrapper = mount(loggedInRedirect); - - expect(wrapper.find('div').length).toBe(1); - }); -}); diff --git a/src/common-components/tests/UnAuthOnlyRoute.test.jsx b/src/common-components/tests/UnAuthOnlyRoute.test.jsx new file mode 100644 index 00000000..3b0ad0cb --- /dev/null +++ b/src/common-components/tests/UnAuthOnlyRoute.test.jsx @@ -0,0 +1,63 @@ +import React from 'react'; +import { mount } from 'enzyme'; +import { BrowserRouter as Router, MemoryRouter, Switch } from 'react-router-dom'; + +import { getConfig } from '@edx/frontend-platform'; + +import { UnAuthOnlyRoute } from '..'; +import { DEFAULT_REDIRECT_URL, LOGIN_PAGE } from '../../data/constants'; + +const RRD = require('react-router-dom'); +// Just render plain div with its children +// eslint-disable-next-line react/prop-types +RRD.BrowserRouter = ({ children }) =>
{ children }
; +module.exports = RRD; + +const TestApp = () => ( + +
+ + (Login Page)} /> + +
+
+); + +describe('UnAuthOnlyRoute', () => { + const routerWrapper = () => ( + + + + ); + + it('should redirect to dashboard if already logged in', () => { + const dashboardURL = getConfig().LMS_BASE_URL.concat(DEFAULT_REDIRECT_URL); + delete window.location; + window.location = { href: '' }; + const user = { + username: 'gonzo', + other: 'data', + }; + const mockUseContext = jest.fn().mockImplementation(() => ({ + authenticatedUser: user, + config: getConfig(), + })); + + React.useContext = mockUseContext; + mount(routerWrapper()); + + expect(window.location.href).toBe(dashboardURL); + }); + + it('should render test login components', () => { + const mockUseContext = jest.fn().mockImplementation(() => ({ + authenticatedUser: null, + config: {}, + })); + + React.useContext = mockUseContext; + const wrapper = mount(routerWrapper()); + + expect(wrapper.find('span').text()).toBe('Login Page'); + }); +}); diff --git a/src/data/constants.js b/src/data/constants.js index c3d9d4a7..969d2edd 100644 --- a/src/data/constants.js +++ b/src/data/constants.js @@ -3,6 +3,8 @@ export const LOGIN_PAGE = '/login'; export const REGISTER_PAGE = '/register'; export const RESET_PAGE = '/reset'; export const DEFAULT_REDIRECT_URL = '/dashboard'; +export const PASSWORD_RESET_CONFIRM = '/password_reset_confirm/:token/'; +export const PAGE_NOT_FOUND = '/notfound'; // Stateful Submit Button States export const DEFAULT_STATE = 'default'; diff --git a/src/index.jsx b/src/index.jsx index c24de740..9eb021be 100755 --- a/src/index.jsx +++ b/src/index.jsx @@ -12,9 +12,11 @@ import { messages as headerMessages } from '@edx/frontend-component-header'; import configureStore from './data/configureStore'; import { LoginPage, RegistrationPage, NotFoundPage } from './logistration'; -import { LOGIN_PAGE, REGISTER_PAGE, RESET_PAGE } from './data/constants'; +import { + LOGIN_PAGE, PAGE_NOT_FOUND, REGISTER_PAGE, RESET_PAGE, PASSWORD_RESET_CONFIRM, +} from './data/constants'; import ForgotPasswordPage from './forgot-password'; -import { HeaderLayout, LoggedInRedirect, registerIcons } from './common-components'; +import { HeaderLayout, UnAuthOnlyRoute, registerIcons } from './common-components'; import ResetPasswordPage from './reset-password'; import appMessages from './i18n'; @@ -25,21 +27,21 @@ registerIcons(); subscribe(APP_READY, () => { ReactDOM.render( - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + , document.getElementById('root'), );