Skip to content

Commit

Permalink
tech(api): move controller to bounded context
Browse files Browse the repository at this point in the history
  • Loading branch information
xav-car committed Nov 13, 2024
1 parent 1b3434f commit f5aafd0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 103 deletions.
14 changes: 0 additions & 14 deletions api/lib/application/users/user-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,7 +154,6 @@ const userController = {
findPaginatedUserRecommendedTrainings,
findUserOrganizationsForAdmin,
getCampaignParticipations,
getUserCampaignParticipationToCampaign,
reassignAuthenticationMethods,
rememberUserHasSeenAssessmentInstructions,
rememberUserHasSeenChallengeTooltip,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 };
50 changes: 1 addition & 49 deletions api/tests/integration/application/users/user-controller_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ 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;
let httpTestServer;

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();
Expand All @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
34 changes: 0 additions & 34 deletions api/tests/unit/application/users/user-controller_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit f5aafd0

Please sign in to comment.