From feb32ddb84f95bd78ba3ef3479406d5f993d4144 Mon Sep 17 00:00:00 2001 From: alangsto <46360176+alangsto@users.noreply.github.com> Date: Thu, 9 Mar 2023 10:00:49 -0500 Subject: [PATCH] feat: call edx-exams if exams base url is defined (#87) --- .env.test | 1 + src/data/api.js | 16 ++++++++++++++-- src/data/redux.test.jsx | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/.env.test b/.env.test index 8c6acedf..b9a422f7 100644 --- a/.env.test +++ b/.env.test @@ -1,2 +1,3 @@ LANGUAGE_PREFERENCE_COOKIE_NAME='openedx-language-preference' LMS_BASE_URL='http://localhost:18000' +EXAMS_BASE_URL='http://localhost:18740' diff --git a/src/data/api.js b/src/data/api.js index e75f7cef..5a7d1b61 100644 --- a/src/data/api.js +++ b/src/data/api.js @@ -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(), @@ -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; diff --git a/src/data/redux.test.jsx b/src/data/redux.test.jsx index 8d0ecd31..b19b78ac 100644 --- a/src/data/redux.test.jsx +++ b/src/data/redux.test.jsx @@ -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'; @@ -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); + }); + }); });