feat: allow survey cookie to be used for login (#277)

Refactored register survey cookie to be used for login survey as well.

VAN-462
This commit is contained in:
Waheed Ahmed
2021-05-10 21:10:43 +05:00
committed by GitHub
parent 37c5344066
commit b76998a36c
10 changed files with 55 additions and 20 deletions

2
.env
View File

@@ -17,5 +17,5 @@ USER_INFO_COOKIE_NAME=null
AUTHN_MINIMAL_HEADER=true
LOGIN_ISSUE_SUPPORT_LINK=null
REGISTRATION_OPTIONAL_FIELDS=null
USER_SIGNUP_SURVEY_COOKIE_NAME=null
USER_SURVEY_COOKIE_NAME=null
COOKIE_DOMAIN=null

View File

@@ -24,5 +24,5 @@ LOGIN_ISSUE_SUPPORT_LINK=''
TOS_AND_HONOR_CODE='http://localhost:18000/honor'
PRIVACY_POLICY='http://localhost:18000/privacy'
REGISTRATION_OPTIONAL_FIELDS='gender,goals,level_of_education,year_of_birth'
USER_SIGNUP_SURVEY_COOKIE_NAME='openedx-user-signup-timestamp'
USER_SURVEY_COOKIE_NAME='openedx-user-survey-type'
COOKIE_DOMAIN='localhost'

View File

@@ -18,4 +18,4 @@ SEGMENT_KEY=null
SITE_NAME='edX'
USER_INFO_COOKIE_NAME='edx-user-info'
LOGIN_ISSUE_SUPPORT_LINK='https://login-issue-support-url.com'
USER_SIGNUP_SURVEY_COOKIE_NAME='openedx-user-signup-timestamp'
USER_SURVEY_COOKIE_NAME='openedx-user-survey-type'

14
src/data/utils/cookies.js Normal file
View File

@@ -0,0 +1,14 @@
import Cookies from 'universal-cookie';
import { getConfig } from '@edx/frontend-platform';
export default function setSurveyCookie(surveyType) {
const cookieName = getConfig().USER_SURVEY_COOKIE_NAME;
if (cookieName) {
const cookies = new Cookies();
const signupTimestamp = (new Date()).getTime();
// set expiry to exactly 24 hours from now
const cookieExpiry = new Date(signupTimestamp + 1 * 864e5);
const options = { domain: getConfig().COOKIE_DOMAIN, expires: cookieExpiry, path: '/' };
cookies.set(cookieName, surveyType, options);
}
}

View File

@@ -8,3 +8,4 @@ export {
windowScrollTo,
} from './dataUtils';
export { default as AsyncActionType } from './reduxUtils';
export { default as setSurveyCookie } from './cookies';

View File

@@ -65,7 +65,7 @@ initialize({
TOS_AND_HONOR_CODE: process.env.TOS_AND_HONOR_CODE || null,
PRIVACY_POLICY: process.env.PRIVACY_POLICY || null,
REGISTRATION_OPTIONAL_FIELDS: process.env.REGISTRATION_OPTIONAL_FIELDS || '',
USER_SIGNUP_SURVEY_COOKIE_NAME: process.env.USER_SIGNUP_SURVEY_COOKIE_NAME || null,
USER_SURVEY_COOKIE_NAME: process.env.USER_SURVEY_COOKIE_NAME || null,
COOKIE_DOMAIN: process.env.COOKIE_DOMAIN,
});
},

View File

@@ -33,7 +33,13 @@ import {
} from '../data/constants';
import { forgotPasswordResultSelector } from '../forgot-password';
import {
getTpaProvider, getTpaHint, updatePathWithQueryParams, getAllPossibleQueryParam, getActivationStatus, windowScrollTo,
getTpaHint,
getTpaProvider,
windowScrollTo,
setSurveyCookie,
getActivationStatus,
getAllPossibleQueryParam,
updatePathWithQueryParams,
} from '../data/utils';
class LoginPage extends React.Component {
@@ -167,6 +173,10 @@ class LoginPage extends React.Component {
);
}
if (this.props.loginResult.success) {
setSurveyCookie('login');
}
return (
<>
<Helmet>

View File

@@ -5,7 +5,7 @@ import { mount } from 'enzyme';
import configureStore from 'redux-mock-store';
import CookiePolicyBanner from '@edx/frontend-component-cookie-policy-banner';
import { getConfig } from '@edx/frontend-platform';
import { getConfig, mergeConfig } from '@edx/frontend-platform';
import * as analytics from '@edx/frontend-platform/analytics';
import { IntlProvider, injectIntl } from '@edx/frontend-platform/i18n';
@@ -25,6 +25,10 @@ const IntlLoginPage = injectIntl(LoginPage);
const mockStore = configureStore();
describe('LoginPage', () => {
mergeConfig({
USER_SURVEY_COOKIE_NAME: process.env.USER_SURVEY_COOKIE_NAME,
});
const initialState = {
forgotPassword: { status: null },
login: {
@@ -501,4 +505,19 @@ describe('LoginPage', () => {
mount(reduxWrapper(<IntlLoginPage {...props} />));
expect(window.location.href).toEqual(getConfig().LMS_BASE_URL + secondaryProviders.loginUrl);
});
it('should set login survey cookie', () => {
store = mockStore({
...initialState,
login: {
...initialState.login,
loginResult: {
success: true,
},
},
});
renderer.create(reduxWrapper(<IntlLoginPage />));
expect(document.cookie).toMatch(`${getConfig().USER_SURVEY_COOKIE_NAME}=login`);
});
});

View File

@@ -5,7 +5,6 @@ import { connect } from 'react-redux';
import Skeleton from 'react-loading-skeleton';
import { Helmet } from 'react-helmet';
import PropTypes from 'prop-types';
import Cookies from 'universal-cookie';
import { getConfig } from '@edx/frontend-platform';
import { sendPageEvent, sendTrackEvent } from '@edx/frontend-platform/analytics';
@@ -33,7 +32,7 @@ import {
DEFAULT_STATE, LOGIN_PAGE, PENDING_STATE, REGISTER_PAGE, VALID_EMAIL_REGEX,
} from '../data/constants';
import {
getTpaProvider, getTpaHint, updatePathWithQueryParams, getAllPossibleQueryParam,
getTpaProvider, getTpaHint, updatePathWithQueryParams, getAllPossibleQueryParam, setSurveyCookie,
} from '../data/utils';
class RegistrationPage extends React.Component {
@@ -430,15 +429,7 @@ class RegistrationPage extends React.Component {
}
if (this.props.registrationResult.success) {
const cookieName = getConfig().USER_SIGNUP_SURVEY_COOKIE_NAME;
if (cookieName) {
const cookies = new Cookies();
const signupTimestamp = (new Date()).getTime();
// set expiry to exactly 24 hours from now
const cookieExpiry = new Date(signupTimestamp + 1 * 864e5);
const options = { domain: getConfig().COOKIE_DOMAIN, expires: cookieExpiry, path: '/' };
cookies.set(cookieName, signupTimestamp, options);
}
setSurveyCookie('register');
}
return (

View File

@@ -29,7 +29,7 @@ describe('RegistrationPageTests', () => {
PRIVACY_POLICY: 'http://privacy-policy.com',
REGISTRATION_OPTIONAL_FIELDS: 'gender,goals,level_of_education,year_of_birth',
TOS_AND_HONOR_CODE: 'http://tos-and-honot-code.com',
USER_SIGNUP_SURVEY_COOKIE_NAME: process.env.USER_SIGNUP_SURVEY_COOKIE_NAME,
USER_SURVEY_COOKIE_NAME: process.env.USER_SURVEY_COOKIE_NAME,
});
const initialState = {
@@ -541,7 +541,7 @@ describe('RegistrationPageTests', () => {
expect(window.location.href).toBe(dasboardUrl);
});
it('should set registration timestamp cookie', () => {
it('should set registration survey cookie', () => {
store = mockStore({
...initialState,
register: {
@@ -553,7 +553,7 @@ describe('RegistrationPageTests', () => {
});
renderer.create(reduxWrapper(<IntlRegistrationPage />));
expect(document.cookie).toMatch(getConfig().USER_SIGNUP_SURVEY_COOKIE_NAME);
expect(document.cookie).toMatch(`${getConfig().USER_SURVEY_COOKIE_NAME}=register`);
});
it('should display institution register button', () => {