diff --git a/src/exam/ExamWrapper.jsx b/src/exam/ExamWrapper.jsx index d74a675f..1bd7c391 100644 --- a/src/exam/ExamWrapper.jsx +++ b/src/exam/ExamWrapper.jsx @@ -1,4 +1,5 @@ import React, { useContext, useEffect } from 'react'; +import { AppContext } from '@edx/frontend-platform/react'; import PropTypes from 'prop-types'; import Exam from './Exam'; import ExamStateContext from '../context'; @@ -8,6 +9,7 @@ import ExamStateContext from '../context'; */ const ExamWrapper = ({ children, ...props }) => { const state = useContext(ExamStateContext); + const { authenticatedUser } = useContext(AppContext); const { sequence, courseId } = props; const { getExamAttemptsData, getAllowProctoringOptOut } = state; const loadInitialData = async () => { @@ -15,6 +17,12 @@ const ExamWrapper = ({ children, ...props }) => { await getAllowProctoringOptOut(sequence.allowProctoringOptOut); }; + // if the user is browsing public content (not logged in) they cannot be in an exam + // any requests for exam state will 403 so just short circuit this component here + if (!authenticatedUser) { + return children; + } + useEffect(() => { loadInitialData(); }, []); diff --git a/src/exam/ExamWrapper.test.jsx b/src/exam/ExamWrapper.test.jsx index 71838439..e99861e0 100644 --- a/src/exam/ExamWrapper.test.jsx +++ b/src/exam/ExamWrapper.test.jsx @@ -106,4 +106,19 @@ describe('SequenceExamWrapper', () => { ); expect(getByTestId('sequence-content')).toHaveTextContent('children'); }); + + it('does not take any actions if the sequence item is not an exam and the user is anonymous', () => { + const appContext = { + authenticatedUser: null, + }; + const { getByTestId } = render( + + +
children
+
+
, + { store, appContext }, + ); + expect(getByTestId('sequence-content')).toHaveTextContent('children'); + }); }); diff --git a/src/setupTest.js b/src/setupTest.js index b9dd531b..8a1b6d32 100644 --- a/src/setupTest.js +++ b/src/setupTest.js @@ -1,9 +1,10 @@ import 'babel-polyfill'; import '@testing-library/jest-dom'; import './data/__factories__'; -import { getConfig, mergeConfig } from '@edx/frontend-platform'; +import { getConfig } from '@edx/frontend-platform'; import { configure as configureLogging } from '@edx/frontend-platform/logging'; import { configure as configureAuth, MockAuthService } from '@edx/frontend-platform/auth'; +import { AppContext } from '@edx/frontend-platform/react'; import React from 'react'; import PropTypes from 'prop-types'; import { Provider } from 'react-redux'; @@ -24,15 +25,6 @@ class MockLoggingService { } export function initializeMockApp() { - mergeConfig({ - authenticatedUser: { - userId: 'abc123', - username: 'Mock User', - roles: [], - administrator: false, - }, - }); - const loggingService = configureLogging(MockLoggingService, { config: getConfig(), }); @@ -70,17 +62,28 @@ function render( ui, { store = null, + appContext = null, ...renderOptions } = {}, ) { function Wrapper({ children }) { + const defaultAppContext = { + authenticatedUser: { + userId: 'abc123', + username: 'Mock User', + roles: [], + administrator: false, + }, + }; return ( // eslint-disable-next-line react/jsx-filename-extension - - - {children} - - + + + + {children} + + + ); }