diff --git a/api/lib/domain/usecases/correct-answer-then-update-assessment.js b/api/lib/domain/usecases/correct-answer-then-update-assessment.js index 6ab4bd92395..534ee00ffcb 100644 --- a/api/lib/domain/usecases/correct-answer-then-update-assessment.js +++ b/api/lib/domain/usecases/correct-answer-then-update-assessment.js @@ -165,13 +165,13 @@ const correctAnswerThenUpdateAssessment = async function ({ let certificationCandidate; if (assessment.isCertification()) { - const onGoingCertificationChallengeLiveAlert = - await certificationChallengeLiveAlertRepository.getOngoingByChallengeIdAndAssessmentId({ + const ongoingOrValidatedCertificationChallengeLiveAlert = + await certificationChallengeLiveAlertRepository.getOngoingOrValidatedByChallengeIdAndAssessmentId({ challengeId: challenge.id, assessmentId: assessment.id, }); - if (onGoingCertificationChallengeLiveAlert) { + if (ongoingOrValidatedCertificationChallengeLiveAlert) { throw new ForbiddenAccess('An alert has been set.'); } diff --git a/api/tests/unit/domain/usecases/correct-answer-then-update-assessment_test.js b/api/tests/unit/domain/usecases/correct-answer-then-update-assessment_test.js index c94a6a4842f..2c143f419a3 100644 --- a/api/tests/unit/domain/usecases/correct-answer-then-update-assessment_test.js +++ b/api/tests/unit/domain/usecases/correct-answer-then-update-assessment_test.js @@ -1,4 +1,5 @@ import { correctAnswerThenUpdateAssessment } from '../../../../lib/domain/usecases/correct-answer-then-update-assessment.js'; +import { CertificationChallengeLiveAlertStatus } from '../../../../src/certification/shared/domain/models/CertificationChallengeLiveAlert.js'; import { EmptyAnswerError } from '../../../../src/evaluation/domain/errors.js'; import { AnswerJob } from '../../../../src/quest/domain/models/AnwserJob.js'; import { @@ -58,7 +59,7 @@ describe('Unit | Domain | Use Cases | correct-answer-then-update-assessment', fu flashAssessmentResultRepository = { save: sinon.stub() }; scorecardService = { computeScorecard: sinon.stub() }; knowledgeElementRepository = { findUniqByUserIdAndAssessmentId: sinon.stub() }; - certificationChallengeLiveAlertRepository = { getOngoingByChallengeIdAndAssessmentId: sinon.stub() }; + certificationChallengeLiveAlertRepository = { getOngoingOrValidatedByChallengeIdAndAssessmentId: sinon.stub() }; certificationEvaluationCandidateRepository = { findByAssessmentId: sinon.stub() }; flashAlgorithmService = { getCapacityAndErrorRate: sinon.stub() }; algorithmDataFetcherService = { fetchForFlashLevelEstimation: sinon.stub() }; @@ -1128,37 +1129,75 @@ describe('Unit | Domain | Use Cases | correct-answer-then-update-assessment', fu }); }); - context('when a live alert has been set in V3 certification', function () { - it('should throw an error', async function () { - // given - const challenge = domainBuilder.buildChallenge({ id: '123' }); - const assessment = domainBuilder.buildAssessment({ - userId, - lastQuestionDate: nowDate, - state: Assessment.states.STARTED, - }); - const answer = domainBuilder.buildAnswer({ challengeId: challenge.id }); - const certificationChallengeLiveAlert = domainBuilder.buildCertificationChallengeLiveAlert({ - assessmentId: assessment.id, - challengeId: challenge.id, - }); - assessmentRepository.get.resolves(assessment); - challengeRepository.get.withArgs(challenge.id).resolves(challenge); + context('when a live alert has been set for the current challenge in V3 certification', function () { + context('when the live alert is ongoing', function () { + it('should throw an error', async function () { + // given + const challenge = domainBuilder.buildChallenge({ id: '123' }); + const assessment = domainBuilder.buildAssessment({ + userId, + lastQuestionDate: nowDate, + state: Assessment.states.STARTED, + }); + const answer = domainBuilder.buildAnswer({ challengeId: challenge.id }); + const certificationChallengeLiveAlert = domainBuilder.buildCertificationChallengeLiveAlert({ + assessmentId: assessment.id, + challengeId: challenge.id, + status: CertificationChallengeLiveAlertStatus.ONGOING, + }); + assessmentRepository.get.resolves(assessment); + challengeRepository.get.withArgs(challenge.id).resolves(challenge); - certificationChallengeLiveAlertRepository.getOngoingByChallengeIdAndAssessmentId - .withArgs({ challengeId: challenge.id, assessmentId: assessment.id }) - .resolves(certificationChallengeLiveAlert); + certificationChallengeLiveAlertRepository.getOngoingOrValidatedByChallengeIdAndAssessmentId + .withArgs({ challengeId: challenge.id, assessmentId: assessment.id }) + .resolves(certificationChallengeLiveAlert); - // when - const error = await catchErr(correctAnswerThenUpdateAssessment)({ - answer, - userId, - ...dependencies, + // when + const error = await catchErr(correctAnswerThenUpdateAssessment)({ + answer, + userId, + ...dependencies, + }); + + // then + expect(error).to.be.an.instanceOf(ForbiddenAccess); + expect(error.message).to.equal('An alert has been set.'); }); + }); - // then - expect(error).to.be.an.instanceOf(ForbiddenAccess); - expect(error.message).to.equal('An alert has been set.'); + context('when the live alert is validated', function () { + it('should throw an error', async function () { + // given + const challenge = domainBuilder.buildChallenge({ id: '123' }); + const assessment = domainBuilder.buildAssessment({ + userId, + lastQuestionDate: nowDate, + state: Assessment.states.STARTED, + }); + const answer = domainBuilder.buildAnswer({ challengeId: challenge.id }); + const certificationChallengeLiveAlert = domainBuilder.buildCertificationChallengeLiveAlert({ + assessmentId: assessment.id, + challengeId: challenge.id, + status: CertificationChallengeLiveAlertStatus.VALIDATED, + }); + assessmentRepository.get.resolves(assessment); + challengeRepository.get.withArgs(challenge.id).resolves(challenge); + + certificationChallengeLiveAlertRepository.getOngoingOrValidatedByChallengeIdAndAssessmentId + .withArgs({ challengeId: challenge.id, assessmentId: assessment.id }) + .resolves(certificationChallengeLiveAlert); + + // when + const error = await catchErr(correctAnswerThenUpdateAssessment)({ + answer, + userId, + ...dependencies, + }); + + // then + expect(error).to.be.an.instanceOf(ForbiddenAccess); + expect(error.message).to.equal('An alert has been set.'); + }); }); });