-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added support to check if active enterprise is same as Enterpri…
…seCourseEnrollment object (#967)
- Loading branch information
1 parent
a7b584c
commit f9806d0
Showing
12 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/alerts/active-enteprise-alert/ActiveEnterpriseAlert.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from 'react'; | ||
import { FormattedMessage, injectIntl, intlShape } from '@edx/frontend-platform/i18n'; | ||
import PropTypes from 'prop-types'; | ||
import { Alert, Hyperlink } from '@edx/paragon'; | ||
import { WarningFilled } from '@edx/paragon/icons'; | ||
|
||
import { getConfig } from '@edx/frontend-platform'; | ||
import genericMessages from './messages'; | ||
|
||
function ActiveEnterpriseAlert({ intl, payload }) { | ||
const { text } = payload; | ||
const changeActiveEnterprise = ( | ||
<Hyperlink | ||
style={{ textDecoration: 'underline' }} | ||
destination={ | ||
`${getConfig().LMS_BASE_URL}/enterprise/select/active/?success_url=${encodeURIComponent(global.location.href)}` | ||
} | ||
> | ||
{intl.formatMessage(genericMessages.changeActiveEnterpriseLowercase)} | ||
</Hyperlink> | ||
); | ||
|
||
return ( | ||
<Alert variant="warning" icon={WarningFilled}> | ||
{text} | ||
<FormattedMessage | ||
id="learning.activeEnterprise.alert" | ||
description="Prompts the user to log-in with the correct enterprise to access the course content." | ||
defaultMessage=" {changeActiveEnterprise}." | ||
values={{ | ||
changeActiveEnterprise, | ||
}} | ||
/> | ||
</Alert> | ||
); | ||
} | ||
|
||
ActiveEnterpriseAlert.propTypes = { | ||
intl: intlShape.isRequired, | ||
payload: PropTypes.shape({ | ||
text: PropTypes.string, | ||
}).isRequired, | ||
}; | ||
|
||
export default injectIntl(ActiveEnterpriseAlert); |
26 changes: 26 additions & 0 deletions
26
src/alerts/active-enteprise-alert/ActiveEnterpriseAlert.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import { getConfig } from '@edx/frontend-platform'; | ||
import { | ||
initializeTestStore, render, screen, | ||
} from '../../setupTest'; | ||
import ActiveEnterpriseAlert from './ActiveEnterpriseAlert'; | ||
|
||
describe('ActiveEnterpriseAlert', () => { | ||
const mockData = { | ||
payload: { | ||
text: 'test message', | ||
}, | ||
}; | ||
beforeAll(async () => { | ||
await initializeTestStore({ excludeFetchCourse: true, excludeFetchSequence: true }); | ||
}); | ||
|
||
it('Shows alert message and links', () => { | ||
render(<ActiveEnterpriseAlert {...mockData} />); | ||
expect(screen.getByRole('alert')).toBeInTheDocument(); | ||
expect(screen.getByText('test message')).toBeInTheDocument(); | ||
expect(screen.getByRole('link', { name: 'change enterprise now' })).toHaveAttribute( | ||
'href', `${getConfig().LMS_BASE_URL}/enterprise/select/active/?success_url=http%3A%2F%2Flocalhost%2F`, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React, { useMemo } from 'react'; | ||
import { ALERT_TYPES, useAlert } from '../../generic/user-messages'; | ||
import { useModel } from '../../generic/model-store'; | ||
|
||
const ActiveEnterpriseAlert = React.lazy(() => import('./ActiveEnterpriseAlert')); | ||
|
||
export default function useActiveEnterpriseAlert(courseId) { | ||
const { courseAccess } = useModel('courseHomeMeta', courseId); | ||
/** | ||
* This alert should render if | ||
* 1. course access code is incorrect_active_enterprise | ||
*/ | ||
const isVisible = courseAccess && !courseAccess.hasAccess && courseAccess.errorCode === 'incorrect_active_enterprise'; | ||
|
||
const payload = { | ||
text: courseAccess && courseAccess.userMessage, | ||
courseId, | ||
}; | ||
useAlert(isVisible, { | ||
code: 'clientActiveEnterpriseAlert', | ||
topic: 'outline', | ||
dismissible: false, | ||
type: ALERT_TYPES.ERROR, | ||
payload: useMemo(() => payload, Object.values(payload).sort()), | ||
}); | ||
|
||
return { clientActiveEnterpriseAlert: ActiveEnterpriseAlert }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import useActiveEnterpriseAlert from './hooks'; | ||
|
||
export default useActiveEnterpriseAlert; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { defineMessages } from '@edx/frontend-platform/i18n'; | ||
|
||
const messages = defineMessages({ | ||
changeActiveEnterpriseLowercase: { | ||
id: 'learning.activeEnterprise.change.alert', | ||
defaultMessage: 'change enterprise now', | ||
description: 'Text in a link, prompting the user to change active enterprise. Used in learning.activeEnterprise.change.alert"', | ||
}, | ||
}); | ||
|
||
export default messages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useEffect } from 'react'; | ||
import { LearningHeader as Header } from '@edx/frontend-component-header'; | ||
import Footer from '@edx/frontend-component-footer'; | ||
import { useParams } from 'react-router-dom'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { Redirect } from 'react-router'; | ||
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; | ||
import useActiveEnterpriseAlert from '../alerts/active-enteprise-alert'; | ||
import { AlertList } from './user-messages'; | ||
import { fetchDiscussionTab } from '../course-home/data/thunks'; | ||
import { LOADED, LOADING } from '../course-home/data/slice'; | ||
import PageLoading from './PageLoading'; | ||
import messages from '../tab-page/messages'; | ||
|
||
function CourseAccessErrorPage({ intl }) { | ||
const { courseId } = useParams(); | ||
|
||
const dispatch = useDispatch(); | ||
const activeEnterpriseAlert = useActiveEnterpriseAlert(courseId); | ||
useEffect(() => { | ||
dispatch(fetchDiscussionTab(courseId)); | ||
}, [courseId]); | ||
|
||
const { | ||
courseStatus, | ||
} = useSelector(state => state.courseHome); | ||
|
||
if (courseStatus === LOADING) { | ||
return ( | ||
<> | ||
<Header /> | ||
<PageLoading | ||
srMessage={intl.formatMessage(messages.loading)} | ||
/> | ||
<Footer /> | ||
</> | ||
); | ||
} | ||
if (courseStatus === LOADED) { | ||
return (<Redirect to={`/redirect/home/${courseId}`} />); | ||
} | ||
return ( | ||
<> | ||
<Header /> | ||
<main id="main-content" className="container my-5 text-center" data-testid="access-denied-main"> | ||
<AlertList | ||
topic="outline" | ||
className="mx-5 mt-3" | ||
customAlerts={{ | ||
...activeEnterpriseAlert, | ||
}} | ||
/> | ||
</main> | ||
<Footer /> | ||
</> | ||
); | ||
} | ||
|
||
CourseAccessErrorPage.propTypes = { | ||
intl: intlShape.isRequired, | ||
}; | ||
|
||
export default injectIntl(CourseAccessErrorPage); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react'; | ||
import { history } from '@edx/frontend-platform'; | ||
import { Route } from 'react-router'; | ||
import { initializeTestStore, render, screen } from '../setupTest'; | ||
import CourseAccessErrorPage from './CourseAccessErrorPage'; | ||
|
||
const mockDispatch = jest.fn(); | ||
let mockCourseStatus; | ||
jest.mock('react-redux', () => ({ | ||
...jest.requireActual('react-redux'), | ||
useDispatch: () => mockDispatch, | ||
useSelector: () => ({ courseStatus: mockCourseStatus }), | ||
})); | ||
jest.mock('./PageLoading', () => () => <div data-testid="page-loading" />); | ||
|
||
describe('CourseAccessErrorPage', () => { | ||
let courseId; | ||
let accessDeniedUrl; | ||
beforeEach(async () => { | ||
const store = await initializeTestStore({ excludeFetchSequence: true }); | ||
courseId = store.getState().courseware.courseId; | ||
accessDeniedUrl = `/course/${courseId}/access-denied`; | ||
history.push(accessDeniedUrl); | ||
}); | ||
|
||
it('Displays loading in start on page rendering', () => { | ||
mockCourseStatus = 'loading'; | ||
render( | ||
<Route path="/course/:courseId/access-denied"> | ||
<CourseAccessErrorPage /> | ||
</Route>, | ||
); | ||
expect(screen.getByTestId('page-loading')).toBeInTheDocument(); | ||
expect(history.location.pathname).toBe(accessDeniedUrl); | ||
}); | ||
|
||
it('Redirect user to homepage if user has access', () => { | ||
mockCourseStatus = 'loaded'; | ||
render( | ||
<Route path="/course/:courseId/access-denied"> | ||
<CourseAccessErrorPage /> | ||
</Route>, | ||
); | ||
expect(history.location.pathname).toBe('/redirect/home/course-v1:edX+DemoX+Demo_Course'); | ||
}); | ||
|
||
it('For access denied it should render access denied page', () => { | ||
mockCourseStatus = 'denied'; | ||
|
||
render( | ||
<Route path="/course/:courseId/access-denied"> | ||
<CourseAccessErrorPage /> | ||
</Route>, | ||
); | ||
expect(screen.getByTestId('access-denied-main')).toBeInTheDocument(); | ||
expect(history.location.pathname).toBe(accessDeniedUrl); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters