From cdde2e7c7b3ee3299fe7ed07fad82a6fd77e1299 Mon Sep 17 00:00:00 2001 From: Guillaume Lagorce Date: Tue, 26 Nov 2024 16:36:31 +0100 Subject: [PATCH] feat(simulator): handle locale from payload and not header anymore Co-authored-by: Alexandre Coin --- .../scenario-simulator-controller.js | 5 +- .../application/scenario-simulator-route.js | 5 ++ .../scenario-simulator-controller_test.js | 1 + .../scenario-simulator-controller_test.js | 85 ++++++++++++++++++- 4 files changed, 89 insertions(+), 7 deletions(-) diff --git a/api/src/certification/flash-certification/application/scenario-simulator-controller.js b/api/src/certification/flash-certification/application/scenario-simulator-controller.js index a95ec5383dc..d7264b478f4 100644 --- a/api/src/certification/flash-certification/application/scenario-simulator-controller.js +++ b/api/src/certification/flash-certification/application/scenario-simulator-controller.js @@ -4,7 +4,6 @@ import _ from 'lodash'; import { pickChallengeService } from '../../../evaluation/domain/services/pick-challenge-service.js'; import { random } from '../../../shared/infrastructure/utils/random.js'; -import { extractLocaleFromRequest } from '../../../shared/infrastructure/utils/request-response-utils.js'; import { pickAnswerStatusService } from '../../shared/domain/services/pick-answer-status-service.js'; import { usecases } from '../domain/usecases/index.js'; import { scenarioSimulatorBatchSerializer } from '../infrastructure/serializers/scenario-simulator-batch-serializer.js'; @@ -17,7 +16,6 @@ async function simulateFlashAssessmentScenario( random, pickAnswerStatusService, pickChallengeService, - extractLocaleFromRequest, }, ) { const { @@ -27,12 +25,11 @@ async function simulateFlashAssessmentScenario( variationPercent, capacity, accessibilityAdjustmentNeeded, + locale, } = request.payload; const pickAnswerStatus = dependencies.pickAnswerStatusService.pickAnswerStatusForCapacity(capacity); - const locale = dependencies.extractLocaleFromRequest(request); - async function* generate() { const iterations = _.range(0, numberOfIterations); diff --git a/api/src/certification/flash-certification/application/scenario-simulator-route.js b/api/src/certification/flash-certification/application/scenario-simulator-route.js index 11a7b9caabb..6c2328512ef 100644 --- a/api/src/certification/flash-certification/application/scenario-simulator-route.js +++ b/api/src/certification/flash-certification/application/scenario-simulator-route.js @@ -1,6 +1,7 @@ import Joi from 'joi'; import { securityPreHandlers } from '../../../shared/application/security-pre-handlers.js'; +import { LOCALE } from '../../../shared/domain/constants.js'; import { scenarioSimulatorController } from './scenario-simulator-controller.js'; const register = async (server) => { @@ -27,6 +28,10 @@ const register = async (server) => { variationPercent: Joi.number().min(0).max(1), capacity: Joi.number().min(-8).max(8).required(), accessibilityAdjustmentNeeded: Joi.boolean(), + locale: Joi.string() + .valid(...Object.values(LOCALE)) + .lowercase() + .required(), }) .required(), }, diff --git a/api/tests/acceptance/application/scenario-simulator/scenario-simulator-controller_test.js b/api/tests/acceptance/application/scenario-simulator/scenario-simulator-controller_test.js index a462f7073c9..fdaf2b8ce46 100644 --- a/api/tests/acceptance/application/scenario-simulator/scenario-simulator-controller_test.js +++ b/api/tests/acceptance/application/scenario-simulator/scenario-simulator-controller_test.js @@ -32,6 +32,7 @@ describe('Acceptance | Controller | scenario-simulator-controller', function () validPayload = { capacity: 4.5, + locale: 'fr-fr', }; const learningContent = { diff --git a/api/tests/certification/flash-certification/integration/application/scenario-simulator-controller_test.js b/api/tests/certification/flash-certification/integration/application/scenario-simulator-controller_test.js index 8c759e91d2e..2c000a8f112 100644 --- a/api/tests/certification/flash-certification/integration/application/scenario-simulator-controller_test.js +++ b/api/tests/certification/flash-certification/integration/application/scenario-simulator-controller_test.js @@ -76,9 +76,9 @@ describe('Integration | Application | scenario-simulator-controller', function ( initialCapacity, capacity: 6, challengePickProbability, + locale: 'en', }, null, - { 'accept-language': 'en' }, ); // then @@ -133,9 +133,9 @@ describe('Integration | Application | scenario-simulator-controller', function ( '/api/scenario-simulator', { capacity, + locale: 'en', }, null, - { 'accept-language': 'en' }, ); // then @@ -191,9 +191,9 @@ describe('Integration | Application | scenario-simulator-controller', function ( { capacity, initialCapacity, + locale: 'en', }, null, - { 'accept-language': 'en' }, ); // then @@ -219,6 +219,85 @@ describe('Integration | Application | scenario-simulator-controller', function ( ]); }); }); + context('When the route is called with uppercased supported locale', function () { + it('should call simulateFlashAssessmentScenario usecase with correct arguments', async function () { + // given + const capacity = -3.1; + + const pickChallengeImplementation = sinon.stub(); + pickChallengeService.chooseNextChallenge.returns(pickChallengeImplementation); + const pickAnswerStatusFromCapacityImplementation = sinon.stub(); + pickAnswerStatusService.pickAnswerStatusForCapacity + .withArgs(capacity) + .returns(pickAnswerStatusFromCapacityImplementation); + + usecases.simulateFlashAssessmentScenario + .withArgs({ + pickChallenge: pickChallengeImplementation, + locale: 'en', + pickAnswerStatus: pickAnswerStatusFromCapacityImplementation, + initialCapacity, + }) + .resolves(simulationResults); + securityPreHandlers.checkAdminMemberHasRoleSuperAdmin.returns(() => true); + + // when + const response = await httpTestServer.request( + 'POST', + '/api/scenario-simulator', + { + capacity, + initialCapacity, + locale: 'EN', + }, + null, + ); + + // then + expect(response.statusCode).to.equal(200); + const parsedResult = parseJsonStream(response); + expect(parsedResult).to.deep.equal([ + { + index: 0, + simulationReport: [ + { + challengeId: challenge1.id, + errorRate: errorRate1, + capacity: capacity1, + minimumCapability: 0.6190392084062237, + answerStatus: 'ok', + reward: reward1, + difficulty: challenge1.difficulty, + discriminant: challenge1.discriminant, + numberOfAvailableChallenges: 1, + }, + ], + }, + ]); + }); + }); + context('When the route is called with non supported locale', function () { + it('should returns 400', async function () { + // given + const capacity = -3.1; + + // when + const response = await httpTestServer.request( + 'POST', + '/api/scenario-simulator', + { + capacity, + initialCapacity, + locale: 'jp', + }, + null, + ); + + // then + expect(response.statusCode).to.equal(400); + expect(response.result.errors[0].detail).to.have.string('"locale" must be one of'); + }); + }); }); }); });