Skip to content

Commit

Permalink
feat: add end_exam message for proctorio (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
alangsto authored May 31, 2023
1 parent e53cb1c commit 39a6340
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/data/messages/proctorio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
* vendor-specific integrations long term. As of now these events
* will trigger on ANY lti integration, not just Proctorio.
*/
function notifyStartExam() {
export function notifyStartExam() {
window.top.postMessage(
['exam_state_change', 'exam_take'],
'*', // this isn't emitting secure data so any origin is fine
);
}

export default notifyStartExam;
export function notifyEndExam() {
window.top.postMessage(
['exam_state_change', 'exam_end'],
'*', // this isn't emitting secure data so any origin is fine
);
}
15 changes: 15 additions & 0 deletions src/data/redux.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,21 @@ describe('Data layer integration tests', () => {
await executeThunk(thunks.submitExam(), store.dispatch, store.getState);
expect(axiosMock.history.put[0].url).toEqual(updateAttemptStatusLegacyUrl);
});

it('Should notify top window on LTI exam end', async () => {
const mockPostMessage = jest.fn();
windowSpy.mockImplementation(() => ({
top: {
postMessage: mockPostMessage,
},
}));

await initWithExamAttempt();
axiosMock.onGet(fetchExamAttemptsDataUrl).reply(200, { exam: submittedExam });
axiosMock.onPut(`${createUpdateAttemptURL}/${attempt.attempt_id}`).reply(200, { exam_attempt_id: submittedAttempt.attempt_id });
await executeThunk(thunks.submitExam(), store.dispatch, store.getState);
expect(mockPostMessage).toHaveBeenCalledWith(['exam_state_change', 'exam_end'], '*');
});
});

describe('Test expireExam', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/data/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import { ExamStatus } from '../constants';
import { workerPromiseForEventNames, pingApplication } from './messages/handlers';
import actionToMessageTypesMap from './messages/constants';
import notifyStartExam from './messages/proctorio';
import { notifyEndExam, notifyStartExam } from './messages/proctorio';

function handleAPIError(error, dispatch) {
const { message, detail } = error;
Expand Down Expand Up @@ -364,6 +364,7 @@ export function submitExam() {
const { exam, activeAttempt } = getState().examState;
const { desktop_application_js_url: workerUrl, external_id: attemptExternalId } = activeAttempt || {};
const useWorker = window.Worker && activeAttempt && workerUrl;
const examHasLtiProvider = !exam.useLegacyAttemptApi;

const handleBackendProviderSubmission = () => {
// if a backend provider is being used during the exam
Expand All @@ -375,6 +376,9 @@ export function submitExam() {
dispatch,
));
}
if (examHasLtiProvider) {
notifyEndExam();
}
};

if (!activeAttempt) {
Expand Down

0 comments on commit 39a6340

Please sign in to comment.