Skip to content

Commit 1e86d15

Browse files
feat(orga): added unit test back
1 parent 841cc02 commit 1e86d15

File tree

1 file changed

+290
-0
lines changed

1 file changed

+290
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
import { PoleEmploiSending } from '../../../../../../src/prescription/campaign-participation/domain/models/PoleEmploiSending';
2+
import { sendSharedParticipationResultsToPoleEmploi } from '../../../../../../src/prescription/campaign-participation/domain/usecases/send-shared-participation-results-to-pole-emploi';
3+
import { PoleEmploiPayload } from '../../../../../../src/prescription/campaign-participation/infrastructure/externals/pole-emploi/PoleEmploiPayload';
4+
import { config } from '../../../../../../src/shared/config';
5+
import { domainBuilder, expect, sinon } from '../../../../../test-helper';
6+
7+
describe('Unit | Domain | UseCase | send-shared-participation-results-to-pole-emploi', function () {
8+
let dependencies, expectedResults;
9+
let campaignRepository,
10+
badgeRepository,
11+
badgeAcquisitionRepository,
12+
campaignParticipationRepository,
13+
campaignParticipationResultRepository,
14+
organizationRepository,
15+
targetProfileRepository,
16+
userRepository,
17+
poleEmploiNotifier,
18+
poleEmploiSendingRepository;
19+
let httpAgent, httpErrorsHelper, logger;
20+
let campaignId, campaignParticipationId, userId, organizationId, badges, badgeAcquiredIds;
21+
let authenticationMethodRepository;
22+
23+
beforeEach(function () {
24+
httpAgent = Symbol('httpAgent');
25+
logger = Symbol('logger');
26+
httpErrorsHelper = Symbol('httpErrorsHelper');
27+
badgeRepository = { findByCampaignId: sinon.stub() };
28+
badgeAcquisitionRepository = { getAcquiredBadgeIds: sinon.stub() };
29+
campaignRepository = { get: sinon.stub() };
30+
campaignParticipationRepository = { get: sinon.stub() };
31+
campaignParticipationResultRepository = { getByParticipationId: sinon.stub() };
32+
organizationRepository = { get: sinon.stub() };
33+
targetProfileRepository = { get: sinon.stub() };
34+
userRepository = { get: sinon.stub() };
35+
poleEmploiNotifier = { notify: sinon.stub() };
36+
poleEmploiSendingRepository = { create: sinon.stub() };
37+
authenticationMethodRepository = {
38+
findOneByUserIdAndIdentityProvider: sinon.stub(),
39+
updateAuthenticationComplementByUserIdAndIdentityProvider: sinon.stub(),
40+
};
41+
42+
dependencies = {
43+
authenticationMethodRepository,
44+
badgeRepository,
45+
badgeAcquisitionRepository,
46+
campaignRepository,
47+
campaignParticipationRepository,
48+
campaignParticipationResultRepository,
49+
organizationRepository,
50+
poleEmploiSendingRepository,
51+
targetProfileRepository,
52+
userRepository,
53+
poleEmploiNotifier,
54+
notifierDependencies: {
55+
httpAgent,
56+
logger,
57+
httpErrorsHelper,
58+
},
59+
};
60+
61+
expectedResults = new PoleEmploiPayload({
62+
campagne: {
63+
nom: 'Campagne Pôle Emploi',
64+
dateDebut: new Date('2020-01-01'),
65+
dateFin: new Date('2020-02-01'),
66+
type: 'EVALUATION',
67+
codeCampagne: 'CODEPE123',
68+
urlCampagne: `${config.domain.pixApp + config.domain.tldFr}/campagnes/CODEPE123`,
69+
nomOrganisme: 'Pix',
70+
typeOrganisme: 'externe',
71+
},
72+
individu: {
73+
nom: 'Bonneau',
74+
prenom: 'Jean',
75+
},
76+
test: {
77+
etat: 4,
78+
progression: 100,
79+
typeTest: 'DI',
80+
referenceExterne: 55667788,
81+
dateDebut: new Date('2020-01-02'),
82+
dateProgression: new Date('2020-01-03'),
83+
dateValidation: new Date('2020-01-03'),
84+
evaluation: 70,
85+
uniteEvaluation: 'A',
86+
elementsEvalues: [
87+
{
88+
libelle: 'Gérer des données',
89+
categorie: 'competence',
90+
type: 'competence Pix',
91+
domaineRattachement: 'Information et données',
92+
nbSousElements: 4,
93+
evaluation: {
94+
scoreObtenu: 50,
95+
uniteScore: 'A',
96+
nbSousElementValide: 2,
97+
},
98+
},
99+
{
100+
libelle: 'Gérer des données 2',
101+
categorie: 'competence',
102+
type: 'competence Pix',
103+
domaineRattachement: 'Information et données',
104+
nbSousElements: 3,
105+
evaluation: {
106+
scoreObtenu: 100,
107+
uniteScore: 'A',
108+
nbSousElementValide: 3,
109+
},
110+
},
111+
],
112+
},
113+
badges: [
114+
{
115+
cle: 1,
116+
titre: 'titre',
117+
message: 'message',
118+
imageUrl: 'imageUrl',
119+
messageAlternatif: 'messageAlternatif',
120+
certifiable: true,
121+
obtenu: true,
122+
},
123+
],
124+
});
125+
badges = [
126+
{
127+
id: 1,
128+
key: 1,
129+
title: 'titre',
130+
message: 'message',
131+
imageUrl: 'imageUrl',
132+
altMessage: 'messageAlternatif',
133+
isCertifiable: true,
134+
},
135+
];
136+
badgeAcquiredIds = [1];
137+
campaignId = Symbol('campaignId');
138+
campaignParticipationId = 55667788;
139+
userId = Symbol('userId');
140+
organizationId = Symbol('organizationId');
141+
});
142+
143+
context('when campaign is of type ASSESSMENT and organization is Pole Emploi', function () {
144+
beforeEach(function () {
145+
organizationRepository.get.withArgs(organizationId).resolves({ isPoleEmploi: true });
146+
userRepository.get
147+
.withArgs(userId)
148+
.resolves(domainBuilder.buildUser({ id: userId, firstName: 'Jean', lastName: 'Bonneau' }));
149+
targetProfileRepository.get.withArgs('targetProfileId1').resolves({ name: 'Diagnostic initial' });
150+
const campaign = domainBuilder.buildCampaign({
151+
id: campaignId,
152+
name: 'Campagne Pôle Emploi',
153+
code: 'CODEPE123',
154+
createdAt: new Date('2020-01-01'),
155+
archivedAt: new Date('2020-02-01'),
156+
type: 'ASSESSMENT',
157+
targetProfile: { id: 'targetProfileId1' },
158+
organization: { id: organizationId },
159+
});
160+
campaignParticipationRepository.get.withArgs(campaignParticipationId).resolves(
161+
domainBuilder.buildCampaignParticipation({
162+
id: campaignParticipationId,
163+
campaign,
164+
userId,
165+
sharedAt: new Date('2020-01-03'),
166+
createdAt: new Date('2020-01-02'),
167+
}),
168+
);
169+
campaignRepository.get.withArgs(campaignId).resolves(campaign);
170+
campaignParticipationResultRepository.getByParticipationId.withArgs(campaignParticipationId).resolves(
171+
domainBuilder.buildCampaignParticipationResult({
172+
totalSkillsCount: 10,
173+
validatedSkillsCount: 7,
174+
competenceResults: [
175+
domainBuilder.buildCompetenceResult({
176+
name: 'Gérer des données',
177+
areaName: 'Information et données',
178+
totalSkillsCount: 4,
179+
testedSkillsCount: 2,
180+
validatedSkillsCount: 2,
181+
}),
182+
domainBuilder.buildCompetenceResult({
183+
name: 'Gérer des données 2',
184+
areaName: 'Information et données',
185+
totalSkillsCount: 3,
186+
testedSkillsCount: 3,
187+
validatedSkillsCount: 3,
188+
}),
189+
],
190+
}),
191+
);
192+
badgeRepository.findByCampaignId.withArgs(campaignId).resolves(badges);
193+
badgeAcquisitionRepository.getAcquiredBadgeIds.withArgs({ badgeIds: [1], userId }).resolves(badgeAcquiredIds);
194+
});
195+
196+
it('should notify pole emploi and create pole emploi sending accordingly', async function () {
197+
// given
198+
const expectedResponse = { isSuccessful: 'someValue', code: 'someCode' };
199+
poleEmploiNotifier.notify
200+
.withArgs(userId, expectedResults, {
201+
authenticationMethodRepository,
202+
httpAgent,
203+
logger,
204+
httpErrorsHelper,
205+
})
206+
.resolves(expectedResponse);
207+
const poleEmploiSending = Symbol('Pole emploi sending');
208+
sinon
209+
.stub(PoleEmploiSending, 'buildForParticipationShared')
210+
.withArgs({
211+
campaignParticipationId,
212+
payload: expectedResults.toString(),
213+
isSuccessful: expectedResponse.isSuccessful,
214+
responseCode: expectedResponse.code,
215+
})
216+
.returns(poleEmploiSending);
217+
218+
// when
219+
await sendSharedParticipationResultsToPoleEmploi({
220+
...dependencies,
221+
campaignParticipationId,
222+
});
223+
224+
// then
225+
expect(poleEmploiSendingRepository.create).to.have.been.calledWithExactly({ poleEmploiSending });
226+
});
227+
});
228+
229+
context('when campaign is of type ASSESSMENT but organization is not Pole Emploi', function () {
230+
beforeEach(function () {
231+
const campaign = domainBuilder.buildCampaign({
232+
id: campaignId,
233+
type: 'ASSESSMENT',
234+
organization: { id: organizationId },
235+
});
236+
campaignParticipationRepository.get.withArgs(campaignParticipationId).resolves(
237+
domainBuilder.buildCampaignParticipation({
238+
id: campaignParticipationId,
239+
campaign,
240+
userId,
241+
sharedAt: new Date('2020-01-03'),
242+
createdAt: new Date('2020-01-02'),
243+
}),
244+
);
245+
campaignRepository.get.withArgs(campaignId).resolves(campaign);
246+
organizationRepository.get.withArgs(organizationId).resolves({ isPoleEmploi: false });
247+
});
248+
249+
it('should not notify to Pole Emploi', async function () {
250+
// when
251+
await sendSharedParticipationResultsToPoleEmploi({
252+
...dependencies,
253+
campaignParticipationId,
254+
});
255+
256+
// then
257+
sinon.assert.notCalled(poleEmploiNotifier.notify);
258+
});
259+
});
260+
261+
context('when organization is Pole Emploi but campaign is of type PROFILES_COLLECTION', function () {
262+
beforeEach(function () {
263+
const campaign = domainBuilder.buildCampaign({ id: campaignId, campaignId, type: 'PROFILES_COLLECTION' });
264+
campaignParticipationRepository.get.withArgs(campaignParticipationId).resolves(
265+
domainBuilder.buildCampaignParticipation({
266+
id: campaignParticipationId,
267+
campaign,
268+
userId,
269+
sharedAt: new Date('2020-01-03'),
270+
createdAt: new Date('2020-01-02'),
271+
}),
272+
);
273+
campaignRepository.get.withArgs(campaignId).resolves(campaign);
274+
organizationRepository.get
275+
.withArgs(organizationId)
276+
.resolves({ isPoleEmploi: true, organization: { id: organizationId } });
277+
});
278+
279+
it('should not notify to Pole Emploi', async function () {
280+
// when
281+
await sendSharedParticipationResultsToPoleEmploi({
282+
...dependencies,
283+
campaignParticipationId,
284+
});
285+
286+
// then
287+
sinon.assert.notCalled(poleEmploiNotifier.notify);
288+
});
289+
});
290+
});

0 commit comments

Comments
 (0)