Skip to content

Commit

Permalink
fix(api): prevent calling reward user usecase if user id is not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume committed Dec 2, 2024
1 parent 7ace134 commit c07273a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
6 changes: 5 additions & 1 deletion api/src/evaluation/application/answers/answer-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ const save = async function (request, h, dependencies = { answerSerializer, requ
const userId = dependencies.requestResponseUtils.extractUserIdFromRequest(request);
const locale = dependencies.requestResponseUtils.extractLocaleFromRequest(request);
const createdAnswer = await usecases.correctAnswerThenUpdateAssessment({ answer, userId, locale });
if (!config.featureToggles.isAsyncQuestRewardingCalculationEnabled && config.featureToggles.isQuestEnabled) {
if (
userId &&
!config.featureToggles.isAsyncQuestRewardingCalculationEnabled &&
config.featureToggles.isQuestEnabled
) {
await questUsecases.rewardUser({ userId });
}

Expand Down
4 changes: 4 additions & 0 deletions api/src/quest/domain/usecases/reward-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export const rewardUser = async ({
successRepository,
rewardRepository,
}) => {
if (!userId) {
return;
}

const quests = await questRepository.findAll();

if (quests.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ describe('Unit | Controller | answer-controller', function () {
locale,
});
});

it('should serialize the answer', function () {
// then
expect(answerSerializerStub.serialize).to.have.been.calledWithExactly(createdAnswer);
Expand Down Expand Up @@ -170,9 +169,13 @@ describe('Unit | Controller | answer-controller', function () {
});

context('when quest feature enabled', function () {
beforeEach(function () {
config.featureToggles.isQuestEnabled = true;
config.featureToggles.isAsyncQuestRewardingCalculationEnabled = false;
});

it('should not call rewardUser if async is enabled', async function () {
// given
config.featureToggles.isQuestEnabled = true;
config.featureToggles.isAsyncQuestRewardingCalculationEnabled = true;

// when
Expand All @@ -187,7 +190,6 @@ describe('Unit | Controller | answer-controller', function () {

it('should call rewardUser if async is not enabled', async function () {
// given
config.featureToggles.isQuestEnabled = true;
config.featureToggles.isAsyncQuestRewardingCalculationEnabled = false;

// when
Expand All @@ -199,6 +201,22 @@ describe('Unit | Controller | answer-controller', function () {
// then
expect(questUsecases.rewardUser).to.have.been.calledWith({ userId });
});

context('when userId is not provided', function () {
it('should not call the reward user usecase', async function () {
// given
requestResponseUtilsStub.extractUserIdFromRequest.returns(null);

// when
await answerController.save(request, hFake, {
answerSerializer: answerSerializerStub,
requestResponseUtils: requestResponseUtilsStub,
});

// then
expect(questUsecases.rewardUser).to.not.have.been.called;
});
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { _getCampaignParticipationOverviewsWithoutPagination } from '../../../../../../src/prescription/organization-learner/domain/usecases/find-organization-learners-with-participations.js';
import {
_getCampaignParticipationOverviewsWithoutPagination,
findOrganizationLearnersWithParticipations,
} from '../../../../../../src/prescription/organization-learner/domain/usecases/find-organization-learners-with-participations.js';
import { expect, sinon } from '../../../../../test-helper.js';

describe('Unit | UseCase | find-organization-learners-with-participations', function () {
context('#findOrganizationLearnersWithParticipations', function () {
it('should not call libOrganizationLearnerRepository if userIds is null', async function () {
// given
const libOrganizationLearnerRepository = {
findByUserId: sinon.stub(),
};

// when
await findOrganizationLearnersWithParticipations({ userIds: null, libOrganizationLearnerRepository });

// then
expect(libOrganizationLearnerRepository.findByUserId).to.not.have.been.called;
});
});

context('#_getCampaignParticipationOverviewsWithoutPagination', function () {
it('should return all campaign participations', async function () {
// given
Expand Down
40 changes: 27 additions & 13 deletions api/tests/quest/unit/domain/usecases/reward-user_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ describe('Quest | Unit | Domain | Usecases | RewardUser', function () {
let successRepository;
let rewardRepository;

let dependencies;

beforeEach(function () {
userId = 1;

Expand All @@ -22,6 +24,13 @@ describe('Quest | Unit | Domain | Usecases | RewardUser', function () {
successRepository = { find: sinon.stub() };

rewardRepository = { save: sinon.stub(), getByUserId: sinon.stub() };

dependencies = {
questRepository,
eligibilityRepository,
successRepository,
rewardRepository,
};
});

context('when there are no quests available', function () {
Expand All @@ -30,7 +39,21 @@ describe('Quest | Unit | Domain | Usecases | RewardUser', function () {
questRepository.findAll.resolves([]);

// when
await rewardUser({ userId, questRepository, eligibilityRepository });
await rewardUser({ userId, ...dependencies });
expect(eligibilityRepository.find).to.not.have.been.called;
});
});

context('when user id is not provided', function () {
it('should not call eligibility repository', async function () {
// given
const quest = { isEligible: () => false };
questRepository.findAll.resolves([quest]);
rewardRepository.getByUserId.resolves([]);
eligibilityRepository.find.resolves([]);

// when
await rewardUser({ userId: null, ...dependencies });
expect(eligibilityRepository.find).to.not.have.been.called;
});
});
Expand All @@ -46,10 +69,7 @@ describe('Quest | Unit | Domain | Usecases | RewardUser', function () {
// when
await rewardUser({
userId,
questRepository,
eligibilityRepository,
successRepository,
rewardRepository,
...dependencies,
});
expect(successRepository.find).to.not.have.been.called;
});
Expand All @@ -72,10 +92,7 @@ describe('Quest | Unit | Domain | Usecases | RewardUser', function () {
// when
await rewardUser({
userId,
questRepository,
eligibilityRepository,
successRepository,
rewardRepository,
...dependencies,
});

// then
Expand All @@ -99,10 +116,7 @@ describe('Quest | Unit | Domain | Usecases | RewardUser', function () {
// when
await rewardUser({
userId,
questRepository,
eligibilityRepository,
successRepository,
rewardRepository,
...dependencies,
});
expect(successRepository.find).to.not.have.been.called;
});
Expand Down

0 comments on commit c07273a

Please sign in to comment.