Skip to content

Commit

Permalink
fix: log worker errors for startProctoredExam (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
bseverino authored Dec 8, 2021
1 parent 0a4d286 commit 3019a63
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
56 changes: 56 additions & 0 deletions src/data/redux.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ const BASE_API_URL = '/api/edx_proctoring/v1/proctored_exam/attempt';
const { loggingService } = initializeMockApp();
const axiosMock = new MockAdapter(getAuthenticatedHttpClient());

let windowSpy;

// Mock for worker failure
const mockPromise = jest.fn(() => (
new Promise((resolve, reject) => {
reject(Error('test error'));
})
));
jest.mock('./messages/handlers', () => ({
...jest.requireActual('./messages/handlers'),
createWorker: jest.fn(),
workerPromiseForEventNames: jest.fn(() => mockPromise),
}));

describe('Data layer integration tests', () => {
const exam = Factory.build('exam', { attempt: Factory.build('attempt') });
const { course_id: courseId, content_id: contentId, attempt } = exam;
Expand All @@ -25,10 +39,17 @@ describe('Data layer integration tests', () => {
let store;

beforeEach(async () => {
windowSpy = jest.spyOn(window, 'window', 'get');
axiosMock.reset();
loggingService.logError.mockReset();
loggingService.logInfo.mockReset();
store = await initializeTestStore();
});

afterEach(() => {
windowSpy.mockRestore();
});

describe('Test getVerificationData', () => {
const getVerificationDataUrl = `${getConfig().LMS_BASE_URL}/verify_student/status/`;

Expand Down Expand Up @@ -498,6 +519,41 @@ describe('Data layer integration tests', () => {

expect(loggingService.logError).toHaveBeenCalled();
});

it('Should log an error on worker failure', async () => {
windowSpy.mockImplementation(() => ({
Worker: jest.fn(),
URL: { createObjectURL: jest.fn() },
}));

const createdWorkerAttempt = Factory.build(
'attempt', { attempt_status: ExamStatus.CREATED, desktop_application_js_url: 'http://proctortest.com' },
);
const startedWorkerAttempt = Factory.build(
'attempt', { attempt_status: ExamStatus.STARTED, desktop_application_js_url: 'http://proctortest.com' },
);
const createdWorkerExam = Factory.build('exam', { attempt: createdWorkerAttempt });
const startedWorkerExam = Factory.build('exam', { attempt: startedWorkerAttempt });
const continueWorkerAttemptUrl = `${getConfig().LMS_BASE_URL}${BASE_API_URL}/${createdWorkerAttempt.attempt_id}`;

axiosMock.onGet(fetchExamAttemptsDataUrl).replyOnce(
200, { exam: createdWorkerExam, active_attempt: createdWorkerAttempt },
);
axiosMock.onGet(fetchExamAttemptsDataUrl).reply(
200, { exam: startedWorkerExam, active_attempt: startedWorkerAttempt },
);
axiosMock.onPost(continueWorkerAttemptUrl).reply(200, { exam_attempt_id: startedWorkerAttempt.attempt_id });

await executeThunk(thunks.getExamAttemptsData(courseId, contentId), store.dispatch);
await executeThunk(thunks.startProctoredExam(), store.dispatch, store.getState);
expect(loggingService.logInfo).toHaveBeenCalledWith(
Error('test error'), {
attemptId: createdWorkerAttempt.attempt_id,
courseId: createdWorkerAttempt.course_id,
examId: createdWorkerExam.id,
},
);
});
});

describe('Test skipProctoringExam', () => {
Expand Down
22 changes: 17 additions & 5 deletions src/data/thunks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logError } from '@edx/frontend-platform/logging';
import { logError, logInfo } from '@edx/frontend-platform/logging';
import {
fetchExamAttemptsData,
createExamAttempt,
Expand Down Expand Up @@ -150,10 +150,22 @@ export function startProctoredExam() {
.then(() => updateAttemptAfter(
exam.course_id, exam.content_id, continueAttempt(attempt.attempt_id),
)(dispatch))
.catch(() => handleAPIError(
{ message: 'Something has gone wrong starting your exam. Please double-check that the application is running.' },
dispatch,
));
.catch(error => {
if (error) {
logInfo(
error,
{
attemptId: attempt.attempt_id,
courseId: attempt.course_id,
examId: exam.id,
},
);
}
handleAPIError(
{ message: 'Something has gone wrong starting your exam. Please double-check that the application is running.' },
dispatch,
);
});
} else {
await updateAttemptAfter(
exam.course_id, exam.content_id, continueAttempt(attempt.attempt_id),
Expand Down

0 comments on commit 3019a63

Please sign in to comment.