From ccbd87d7c4006e2224d4b8ca06b7878a26f72f2a Mon Sep 17 00:00:00 2001 From: Moncef Abboud Date: Mon, 2 Oct 2023 11:54:44 +0200 Subject: [PATCH] feat: implement SHOW_REGISTRATION_LINKS setting --- src/common-components/EnterpriseSSO.jsx | 5 +- src/common-components/Logistration.jsx | 8 +- .../tests/Logistration.test.jsx | 153 +++++++++--------- 3 files changed, 86 insertions(+), 80 deletions(-) diff --git a/src/common-components/EnterpriseSSO.jsx b/src/common-components/EnterpriseSSO.jsx index 9005c12fe6..bec5caa718 100644 --- a/src/common-components/EnterpriseSSO.jsx +++ b/src/common-components/EnterpriseSSO.jsx @@ -18,7 +18,8 @@ import messages from './messages'; const EnterpriseSSO = (props) => { const { formatMessage } = useIntl(); const tpaProvider = props.provider; - const disablePublicAccountCreation = getConfig().ALLOW_PUBLIC_ACCOUNT_CREATION === false; + const hideRegistrationLink = getConfig().ALLOW_PUBLIC_ACCOUNT_CREATION === false + || getConfig().SHOW_REGISTRATION_LINKS === false; const handleSubmit = (e, url) => { e.preventDefault(); @@ -71,7 +72,7 @@ const EnterpriseSSO = (props) => { className="w-100" onClick={(e) => handleClick(e)} > - {disablePublicAccountCreation + {hideRegistrationLink ? formatMessage(messages['enterprisetpa.login.button.text.public.account.creation.disabled']) : formatMessage(messages['enterprisetpa.login.button.text'])} diff --git a/src/common-components/Logistration.jsx b/src/common-components/Logistration.jsx index cc8c11019b..ab423df361 100644 --- a/src/common-components/Logistration.jsx +++ b/src/common-components/Logistration.jsx @@ -36,6 +36,7 @@ const Logistration = (props) => { const [institutionLogin, setInstitutionLogin] = useState(false); const [key, setKey] = useState(''); const disablePublicAccountCreation = getConfig().ALLOW_PUBLIC_ACCOUNT_CREATION === false; + const hideRegistrationLink = getConfig().SHOW_REGISTRATION_LINKS === false; useEffect(() => { const authService = getAuthService(); @@ -108,7 +109,7 @@ const Logistration = (props) => { ) - : (!isValidTpaHint() && ( + : (!isValidTpaHint() && !hideRegistrationLink && ( @@ -118,6 +119,11 @@ const Logistration = (props) => { )}
+ {!institutionLogin && !isValidTpaHint() && hideRegistrationLink && ( +

+ {formatMessage(messages[selectedPage === LOGIN_PAGE ? 'logistration.sign.in' : 'logistration.register'])} +

+ )} {selectedPage === LOGIN_PAGE ? : ( diff --git a/src/common-components/tests/Logistration.test.jsx b/src/common-components/tests/Logistration.test.jsx index c7f367c0fe..fda3380b6b 100644 --- a/src/common-components/tests/Logistration.test.jsx +++ b/src/common-components/tests/Logistration.test.jsx @@ -2,22 +2,25 @@ import React from 'react'; import { Provider } from 'react-redux'; import { getConfig, mergeConfig } from '@edx/frontend-platform'; -import * as analytics from '@edx/frontend-platform/analytics'; -import * as auth from '@edx/frontend-platform/auth'; +import { sendPageEvent, sendTrackEvent } from '@edx/frontend-platform/analytics'; import { configure, injectIntl, IntlProvider } from '@edx/frontend-platform/i18n'; import { mount } from 'enzyme'; import { MemoryRouter } from 'react-router-dom'; import configureStore from 'redux-mock-store'; -import { COMPLETE_STATE, LOGIN_PAGE } from '../../data/constants'; +import { + COMPLETE_STATE, LOGIN_PAGE, REGISTER_PAGE, +} from '../../data/constants'; import { backupRegistrationForm } from '../../register/data/actions'; import { clearThirdPartyAuthContextErrorMessage } from '../data/actions'; import { RenderInstitutionButton } from '../InstitutionLogistration'; import Logistration from '../Logistration'; -jest.mock('@edx/frontend-platform/analytics'); +jest.mock('@edx/frontend-platform/analytics', () => ({ + sendPageEvent: jest.fn(), + sendTrackEvent: jest.fn(), +})); jest.mock('@edx/frontend-platform/auth'); -analytics.sendPageEvent = jest.fn(); const mockStore = configureStore(); const IntlLogistration = injectIntl(Logistration); @@ -40,8 +43,47 @@ describe('Logistration', () => { ); + const initialState = { + register: { + registrationFormData: { + configurableFormFields: { + marketingEmailsOptIn: true, + }, + formFields: { + name: '', email: '', username: '', password: '', + }, + emailSuggestion: { + suggestion: '', type: '', + }, + errors: { + name: '', email: '', username: '', password: '', + }, + }, + registrationResult: { success: false, redirectUrl: '' }, + registrationError: {}, + usernameSuggestions: [], + validationApiRateLimited: false, + }, + commonComponents: { + thirdPartyAuthContext: { + providers: [], + secondaryProviders: [], + }, + }, + login: { + loginResult: { success: false, redirectUrl: '' }, + }, + }; + beforeEach(() => { - auth.getAuthenticatedUser = jest.fn(() => ({ userId: 3, username: 'test-user' })); + store = mockStore(initialState); + jest.mock('@edx/frontend-platform/auth', () => ({ + getAuthenticatedUser: jest.fn(() => ({ + userId: 3, + username: 'test-user', + })), + })); + configure({ loggingService: { logError: jest.fn() }, config: { @@ -56,52 +98,48 @@ describe('Logistration', () => { mergeConfig({ ALLOW_PUBLIC_ACCOUNT_CREATION: true, }); - store = mockStore({ - register: { - registrationResult: { success: false, redirectUrl: '' }, - registrationError: {}, - }, - commonComponents: { - thirdPartyAuthContext: { - providers: [], - secondaryProviders: [], - }, - }, - }); + const logistration = mount(reduxWrapper()); expect(logistration.find('#main-content').find('RegistrationPage').exists()).toBeTruthy(); }); it('should render login page', () => { - store = mockStore({ - login: { - loginResult: { success: false, redirectUrl: '' }, - }, - commonComponents: { - thirdPartyAuthContext: { - providers: [], - secondaryProviders: [], - }, - }, - }); - const props = { selectedPage: LOGIN_PAGE }; const logistration = mount(reduxWrapper()); expect(logistration.find('#main-content').find('LoginPage').exists()).toBeTruthy(); }); + it('should render login/register headings when show registration links is disabled', () => { + mergeConfig({ + SHOW_REGISTRATION_LINKS: false, + }); + + let props = { selectedPage: LOGIN_PAGE }; + let logistration = mount(reduxWrapper()); + + // verifying sign in heading + expect(logistration.find('#main-content').find('h3').text()).toEqual('Sign in'); + + // register page is still accessible when SHOW_REGISTRATION_LINKS is false + // but it needs to be accessed directly + props = { selectedPage: REGISTER_PAGE }; + logistration = mount(reduxWrapper()); + + // verifying register heading + expect(logistration.find('#main-content').find('h3').text()).toEqual('Register'); + }); + it('should render only login page when public account creation is disabled', () => { mergeConfig({ ALLOW_PUBLIC_ACCOUNT_CREATION: false, DISABLE_ENTERPRISE_LOGIN: 'true', + SHOW_REGISTRATION_LINKS: 'true', }); store = mockStore({ - login: { - loginResult: { success: false, redirectUrl: '' }, - }, + ...initialState, commonComponents: { thirdPartyAuthContext: { currentProvider: null, @@ -131,9 +169,7 @@ describe('Logistration', () => { }); store = mockStore({ - login: { - loginResult: { success: false, redirectUrl: '' }, - }, + ...initialState, commonComponents: { thirdPartyAuthContext: { currentProvider: null, @@ -164,9 +200,7 @@ describe('Logistration', () => { }); store = mockStore({ - login: { - loginResult: { success: false, redirectUrl: '' }, - }, + ...initialState, commonComponents: { thirdPartyAuthContext: { currentProvider: null, @@ -182,8 +216,8 @@ describe('Logistration', () => { const logistration = mount(reduxWrapper()); logistration.find(RenderInstitutionButton).simulate('click', { institutionLogin: true }); - expect(analytics.sendTrackEvent).toHaveBeenCalledWith('edx.bi.institution_login_form.toggled', { category: 'user-engagement' }); - expect(analytics.sendPageEvent).toHaveBeenCalledWith('login_and_registration', 'institution_login'); + expect(sendTrackEvent).toHaveBeenCalledWith('edx.bi.institution_login_form.toggled', { category: 'user-engagement' }); + expect(sendPageEvent).toHaveBeenCalledWith('login_and_registration', 'institution_login'); mergeConfig({ DISABLE_ENTERPRISE_LOGIN: '', @@ -196,10 +230,7 @@ describe('Logistration', () => { }); store = mockStore({ - register: { - registrationResult: { success: false, redirectUrl: '' }, - registrationError: {}, - }, + ...initialState, commonComponents: { thirdPartyAuthContext: { currentProvider: null, @@ -224,22 +255,6 @@ describe('Logistration', () => { }); it('should fire action to backup registration form on tab click', () => { - store = mockStore({ - login: { - loginResult: { success: false, redirectUrl: '' }, - }, - register: { - registrationResult: { success: false, redirectUrl: '' }, - registrationError: {}, - }, - commonComponents: { - thirdPartyAuthContext: { - providers: [], - secondaryProviders: [], - }, - }, - }); - store.dispatch = jest.fn(store.dispatch); const logistration = mount(reduxWrapper()); logistration.find('a[data-rb-event-key="/login"]').simulate('click'); @@ -247,22 +262,6 @@ describe('Logistration', () => { }); it('should clear tpa context errorMessage tab click', () => { - store = mockStore({ - login: { - loginResult: { success: false, redirectUrl: '' }, - }, - register: { - registrationResult: { success: false, redirectUrl: '' }, - registrationError: {}, - }, - commonComponents: { - thirdPartyAuthContext: { - providers: [], - secondaryProviders: [], - }, - }, - }); - store.dispatch = jest.fn(store.dispatch); const logistration = mount(reduxWrapper()); logistration.find('a[data-rb-event-key="/login"]').simulate('click');