diff --git a/api/lib/application/users/user-controller.js b/api/lib/application/users/user-controller.js index 8290a57da6b..14d2f5ce10b 100644 --- a/api/lib/application/users/user-controller.js +++ b/api/lib/application/users/user-controller.js @@ -69,19 +69,6 @@ const resetScorecard = function (request, h, dependencies = { scorecardSerialize .then(dependencies.scorecardSerializer.serialize); }; -const getUserCampaignParticipationToCampaign = function ( - request, - h, - dependencies = { campaignParticipationSerializer }, -) { - const authenticatedUserId = request.auth.credentials.userId; - const campaignId = request.params.campaignId; - - return usecases - .getUserCampaignParticipationToCampaign({ userId: authenticatedUserId, campaignId }) - .then((campaignParticipation) => dependencies.campaignParticipationSerializer.serialize(campaignParticipation)); -}; - const anonymizeUser = async function (request, h, dependencies = { userAnonymizedDetailsForAdminSerializer }) { const userToAnonymizeId = request.params.id; const adminMemberId = request.auth.credentials.userId; @@ -167,7 +154,6 @@ const userController = { findPaginatedUserRecommendedTrainings, findUserOrganizationsForAdmin, getCampaignParticipations, - getUserCampaignParticipationToCampaign, reassignAuthenticationMethods, rememberUserHasSeenAssessmentInstructions, rememberUserHasSeenChallengeTooltip, diff --git a/api/src/prescription/campaign-participation/application/campaign-participation-controller.js b/api/src/prescription/campaign-participation/application/campaign-participation-controller.js index 870cc0496f2..891caf1b1ca 100644 --- a/api/src/prescription/campaign-participation/application/campaign-participation-controller.js +++ b/api/src/prescription/campaign-participation/application/campaign-participation-controller.js @@ -6,10 +6,24 @@ import * as campaignAnalysisSerializer from '../infrastructure/serializers/jsona import * as campaignAssessmentParticipationResultSerializer from '../infrastructure/serializers/jsonapi/campaign-assessment-participation-result-serializer.js'; import * as campaignAssessmentParticipationSerializer from '../infrastructure/serializers/jsonapi/campaign-assessment-participation-serializer.js'; import * as campaignParticipationOverviewSerializer from '../infrastructure/serializers/jsonapi/campaign-participation-overview-serializer.js'; +import * as campaignParticipationSerializer from '../infrastructure/serializers/jsonapi/campaign-participation-serializer.js'; import * as campaignProfileSerializer from '../infrastructure/serializers/jsonapi/campaign-profile-serializer.js'; import * as participantResultSerializer from '../infrastructure/serializers/jsonapi/participant-result-serializer.js'; import * as participationForCampaignManagementSerializer from '../infrastructure/serializers/jsonapi/participation-for-campaign-management-serializer.js'; +const getUserCampaignParticipationToCampaign = function ( + request, + h, + dependencies = { campaignParticipationSerializer }, +) { + const authenticatedUserId = request.auth.credentials.userId; + const campaignId = request.params.campaignId; + + return usecases + .getUserCampaignParticipationToCampaign({ userId: authenticatedUserId, campaignId }) + .then((campaignParticipation) => dependencies.campaignParticipationSerializer.serialize(campaignParticipation)); +}; + const findPaginatedParticipationsForCampaignManagement = async function (request) { const { campaignId } = request.params; const { page } = request.query; @@ -161,17 +175,18 @@ const getUserCampaignAssessmentResult = async function ( }; const campaignParticipationController = { + deleteCampaignParticipationForAdmin, + deleteParticipation, findPaginatedParticipationsForCampaignManagement, - getUserCampaignAssessmentResult, getAnalysis, - getCampaignProfile, getCampaignAssessmentParticipation, - getCampaignParticipationsForOrganizationLearner, - deleteParticipation, getCampaignAssessmentParticipationResult, - updateParticipantExternalId, - deleteCampaignParticipationForAdmin, getCampaignParticipationOverviews, + getCampaignParticipationsForOrganizationLearner, + getCampaignProfile, + getUserCampaignAssessmentResult, + getUserCampaignParticipationToCampaign, + updateParticipantExternalId, }; export { campaignParticipationController }; diff --git a/api/tests/integration/application/users/user-controller_test.js b/api/tests/integration/application/users/user-controller_test.js index 5d601173fbc..08de8ff3585 100644 --- a/api/tests/integration/application/users/user-controller_test.js +++ b/api/tests/integration/application/users/user-controller_test.js @@ -2,7 +2,7 @@ import * as moduleUnderTest from '../../../../lib/application/users/index.js'; import { usecases } from '../../../../lib/domain/usecases/index.js'; import { securityPreHandlers } from '../../../../src/shared/application/security-pre-handlers.js'; import { UserNotAuthorizedToRemoveAuthenticationMethod } from '../../../../src/shared/domain/errors.js'; -import { domainBuilder, expect, HttpTestServer, sinon } from '../../../test-helper.js'; +import { expect, HttpTestServer, sinon } from '../../../test-helper.js'; describe('Integration | Application | Users | user-controller', function () { let sandbox; @@ -10,10 +10,8 @@ describe('Integration | Application | Users | user-controller', function () { beforeEach(async function () { sandbox = sinon.createSandbox(); - sandbox.stub(securityPreHandlers, 'checkRequestedUserIsAuthenticatedUser'); sandbox.stub(securityPreHandlers, 'hasAtLeastOneAccessOf'); - sandbox.stub(usecases, 'getUserCampaignParticipationToCampaign'); sandbox.stub(usecases, 'removeAuthenticationMethod'); httpTestServer = new HttpTestServer(); @@ -24,52 +22,6 @@ describe('Integration | Application | Users | user-controller', function () { sandbox.restore(); }); - describe('#getUserCampaignParticipationToCampaign', function () { - const auth = { credentials: {}, strategy: {} }; - - context('Success cases', function () { - let campaignParticipation; - - beforeEach(function () { - campaignParticipation = domainBuilder.buildCampaignParticipation(); - securityPreHandlers.checkRequestedUserIsAuthenticatedUser.returns(true); - auth.credentials.userId = '1234'; - }); - - it('should return an HTTP response with status code 200', async function () { - // given - usecases.getUserCampaignParticipationToCampaign.resolves(campaignParticipation); - - // when - const response = await httpTestServer.request( - 'GET', - '/api/users/1234/campaigns/5678/campaign-participations', - null, - auth, - ); - - // then - expect(response.statusCode).to.equal(200); - }); - }); - - context('Error cases', function () { - beforeEach(function () { - securityPreHandlers.checkRequestedUserIsAuthenticatedUser.callsFake((request, h) => { - return Promise.resolve(h.response().code(403).takeover()); - }); - }); - - it('should return a 403 HTTP response', async function () { - // when - const response = await httpTestServer.request('GET', '/api/users/1234/campaigns/5678/campaign-participations'); - - // then - expect(response.statusCode).to.equal(403); - }); - }); - }); - describe('#removeAuthenticationMethod', function () { const method = 'POST'; const url = '/api/admin/users/1/remove-authentication'; diff --git a/api/tests/prescription/campaign-participation/integration/application/campaign-participation-route_test.js b/api/tests/prescription/campaign-participation/integration/application/campaign-participation-route_test.js index 9c561b709cc..0b9df53fad7 100644 --- a/api/tests/prescription/campaign-participation/integration/application/campaign-participation-route_test.js +++ b/api/tests/prescription/campaign-participation/integration/application/campaign-participation-route_test.js @@ -29,6 +29,24 @@ describe('Integration | Application | Route | campaignParticipationRouter', func sandbox.restore(); }); + describe('#getUserCampaignParticipationToCampaign', function () { + context('When Authenticated user mismatch requested user', function () { + beforeEach(function () { + securityPreHandlers.checkRequestedUserIsAuthenticatedUser.callsFake((request, h) => { + return Promise.resolve(h.response().code(403).takeover()); + }); + }); + + it('should return a 403 HTTP response', async function () { + // when + const response = await httpTestServer.request('GET', '/api/users/1234/campaigns/5678/campaign-participations'); + + // then + expect(response.statusCode).to.equal(403); + }); + }); + }); + describe('#getUserCampaignAssessmentResult', function () { context('Error cases', function () { it('should not called controller when user not authenticated', async function () { diff --git a/api/tests/prescription/campaign-participation/unit/application/campaign-participation-controller_test.js b/api/tests/prescription/campaign-participation/unit/application/campaign-participation-controller_test.js index 94c60b08fb5..b7eaada6990 100644 --- a/api/tests/prescription/campaign-participation/unit/application/campaign-participation-controller_test.js +++ b/api/tests/prescription/campaign-participation/unit/application/campaign-participation-controller_test.js @@ -362,4 +362,38 @@ describe('Unit | Application | Controller | Campaign-Participation', function () }); }); }); + + describe('#getUserCampaignParticipationToCampaign', function () { + it('should return serialized campaign participation', async function () { + // given + const userId = 789; + const campaignId = 456; + const campaignParticipation = Symbol('campaign participation'); + const expectedCampaignParticipation = Symbol('expected campaign participation'); + + const request = { + auth: { + credentials: { + userId, + }, + }, + params: { + userId, + campaignId, + }, + }; + const campaignParticipationSerializer = { serialize: sinon.stub() }; + sinon.stub(usecases, 'getUserCampaignParticipationToCampaign'); + usecases.getUserCampaignParticipationToCampaign.withArgs({ userId, campaignId }).resolves(campaignParticipation); + campaignParticipationSerializer.serialize.withArgs(campaignParticipation).returns(expectedCampaignParticipation); + + // when + const response = await campaignParticipationController.getUserCampaignParticipationToCampaign(request, hFake, { + campaignParticipationSerializer, + }); + + // then + expect(response).to.equal(expectedCampaignParticipation); + }); + }); }); diff --git a/api/tests/unit/application/users/user-controller_test.js b/api/tests/unit/application/users/user-controller_test.js index 2bc36620157..c63beaef8d0 100644 --- a/api/tests/unit/application/users/user-controller_test.js +++ b/api/tests/unit/application/users/user-controller_test.js @@ -205,40 +205,6 @@ describe('Unit | Controller | user-controller', function () { }); }); - describe('#getUserCampaignParticipationToCampaign', function () { - it('should return serialized campaign participation', async function () { - // given - const userId = 789; - const campaignId = 456; - const campaignParticipation = Symbol('campaign participation'); - const expectedCampaignParticipation = Symbol('expected campaign participation'); - - const request = { - auth: { - credentials: { - userId, - }, - }, - params: { - userId, - campaignId, - }, - }; - const campaignParticipationSerializer = { serialize: sinon.stub() }; - sinon.stub(usecases, 'getUserCampaignParticipationToCampaign'); - usecases.getUserCampaignParticipationToCampaign.withArgs({ userId, campaignId }).resolves(campaignParticipation); - campaignParticipationSerializer.serialize.withArgs(campaignParticipation).returns(expectedCampaignParticipation); - - // when - const response = await userController.getUserCampaignParticipationToCampaign(request, hFake, { - campaignParticipationSerializer, - }); - - // then - expect(response).to.equal(expectedCampaignParticipation); - }); - }); - describe('#anonymizeUser', function () { let clock;