Skip to content

Commit

Permalink
feat: call edx-exams if exams base url is defined (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
alangsto authored Mar 9, 2023
1 parent 36c11c6 commit feb32dd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
LANGUAGE_PREFERENCE_COOKIE_NAME='openedx-language-preference'
LMS_BASE_URL='http://localhost:18000'
EXAMS_BASE_URL='http://localhost:18740'
16 changes: 14 additions & 2 deletions src/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ export async function pollExamAttempt(url) {
}

export async function createExamAttempt(examId, startClock = true, attemptProctored = false) {
const url = new URL(`${getConfig().LMS_BASE_URL}${BASE_API_URL}`);
let urlString;
if (!getConfig().EXAMS_BASE_URL) {
urlString = `${getConfig().LMS_BASE_URL}${BASE_API_URL}`;
} else {
urlString = `${getConfig().EXAMS_BASE_URL}/exams/attempt`;
}
const url = new URL(urlString);
const payload = {
exam_id: examId,
start_clock: startClock.toString(),
Expand All @@ -33,7 +39,13 @@ export async function createExamAttempt(examId, startClock = true, attemptProcto
}

export async function updateAttemptStatus(attemptId, action, detail = null) {
const url = new URL(`${getConfig().LMS_BASE_URL}${BASE_API_URL}/${attemptId}`);
let urlString;
if (!getConfig().EXAMS_BASE_URL) {
urlString = `${getConfig().LMS_BASE_URL}${BASE_API_URL}/${attemptId}`;
} else {
urlString = `${getConfig().EXAMS_BASE_URL}/attempt/${attemptId}`;
}
const url = new URL(urlString);
const payload = { action };
if (detail) {
payload.detail = detail;
Expand Down
33 changes: 32 additions & 1 deletion src/data/redux.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Factory } from 'rosie';
import MockAdapter from 'axios-mock-adapter';

import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { getConfig } from '@edx/frontend-platform';
import { getConfig, mergeConfig } from '@edx/frontend-platform';

import * as thunks from './thunks';

Expand Down Expand Up @@ -643,4 +643,35 @@ describe('Data layer integration tests', () => {
}));
});
});

describe('Test exams IDA url', () => {
beforeAll(async () => {
mergeConfig({
EXAMS_BASE_URL: process.env.EXAMS_BASE_URL || null,
});
});

it('Should call the exams service for create attempt', async () => {
const createExamAttemptURL = `${getConfig().EXAMS_BASE_URL}/exams/attempt`;

axiosMock.onGet(fetchExamAttemptsDataUrl).replyOnce(200, { exam, active_attempt: {} });
axiosMock.onPost(createExamAttemptURL).reply(200, { exam_attempt_id: 1111111 });

await executeThunk(thunks.getExamAttemptsData(courseId, contentId), store.dispatch);
await executeThunk(thunks.startTimedExam(), store.dispatch, store.getState);

expect(axiosMock.history.post[0].url).toEqual(createExamAttemptURL);
});

it('Should call the exams service for update attempt', async () => {
const updateExamAttemptURL = `${getConfig().EXAMS_BASE_URL}/attempt/${attempt.attempt_id}`;

axiosMock.onGet(fetchExamAttemptsDataUrl).replyOnce(200, { exam: {}, active_attempt: attempt });
axiosMock.onPut(updateExamAttemptURL).reply(200, { exam_attempt_id: attempt.attempt_id });

await executeThunk(thunks.getExamAttemptsData(courseId, contentId), store.dispatch);
await executeThunk(thunks.stopExam(), store.dispatch, store.getState);
expect(axiosMock.history.put[0].url).toEqual(updateExamAttemptURL);
});
});
});

0 comments on commit feb32dd

Please sign in to comment.