Skip to content

Commit

Permalink
correct test
Browse files Browse the repository at this point in the history
  • Loading branch information
fufeck committed Dec 10, 2024
1 parent 045a918 commit 530bdf4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 58 deletions.
37 changes: 7 additions & 30 deletions src/modules/api_annuaire/api_annuaire.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('API ANNUAIRE MODULE', () => {
},
{
nom: 'mairie deleguee',
adresse_courriel: 'no@test.fr',
adresse_courriel: '[email protected];ok3@test.fr',
},
],
};
Expand All @@ -52,31 +52,8 @@ describe('API ANNUAIRE MODULE', () => {
)
.reply(200, data);

const email = await apiAnnuaireService.getEmailCommune(codeCommune);
expect(email).toBe('[email protected]');
});

it('getEmailCommune multi email', async () => {
const codeCommune = '91400';

// MOCK AXIOS
const data: any = {
results: [
{
nom: 'mairie principal',
adresse_courriel: '[email protected];[email protected]',
},
],
};
axiosMock
.onGet(
`/catalog/datasets/api-lannuaire-administration/records?where=pivot%20LIKE%20"mairie"%20AND%20code_insee_commune="${codeCommune}"&limit=100`,
)
.reply(200, data);

const email = await apiAnnuaireService.getEmailCommune(codeCommune);

expect(email).toBe('[email protected]');
const emails = await apiAnnuaireService.getEmailsCommune(codeCommune);
expect(emails).toEqual(['[email protected]', '[email protected]', '[email protected]']);
});

it('getEmailCommune only mairie deleguee', async () => {
Expand All @@ -97,9 +74,9 @@ describe('API ANNUAIRE MODULE', () => {
)
.reply(200, data);

const email = await apiAnnuaireService.getEmailCommune(codeCommune);
const email = await apiAnnuaireService.getEmailsCommune(codeCommune);

expect(email).toBe('[email protected]');
expect(email).toEqual(['[email protected]']);
});

it('getEmailCommune multi email', async () => {
Expand All @@ -120,13 +97,13 @@ describe('API ANNUAIRE MODULE', () => {
)
.reply(200, data);

const email = await apiAnnuaireService.getEmailCommune(codeCommune);
const email = await apiAnnuaireService.getEmailsCommune(codeCommune);
expect(email).toBeUndefined();
});

it('getEmailCommune multi email', async () => {
const codeCommune = '91400';
const email = await apiAnnuaireService.getEmailCommune(codeCommune);
const email = await apiAnnuaireService.getEmailsCommune(codeCommune);
expect(email).toBeUndefined();
});
});
Expand Down
58 changes: 31 additions & 27 deletions src/modules/habilitation/habilitation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,29 +148,32 @@ export class HabilitationService {
return expireAt;
}

public async sendCodePin(body: Habilitation): Promise<Habilitation> {
if (body.status === StatusHabilitationEnum.ACCEPTED) {
public async sendCodePin(habilitation: Habilitation): Promise<Habilitation> {
if (habilitation.status === StatusHabilitationEnum.ACCEPTED) {
throw new HttpException(
'Cette habilitation est déjà validée',
HttpStatus.PRECONDITION_FAILED,
);
}

if (body.status === StatusHabilitationEnum.REJECTED) {
if (habilitation.status === StatusHabilitationEnum.REJECTED) {
throw new HttpException(
'Cette habilitation est rejetée',
HttpStatus.PRECONDITION_FAILED,
);
}

if (body.emailsCommune.length <= 0) {
if (!habilitation.emailsCommune || habilitation.emailsCommune.length <= 0) {
throw new HttpException(
'Impossible d’envoyer le code, aucun courriel n’est connu pour cette commune',
HttpStatus.PRECONDITION_FAILED,
);
}

if (body.strategy && this.hasBeenSentRecently(body.strategy.createdAt)) {
if (
habilitation.strategy &&
this.hasBeenSentRecently(habilitation.strategy.createdAt)
) {
throw new HttpException(
'Un courriel a déjà été envoyé, merci de patienter',
HttpStatus.CONFLICT,
Expand All @@ -180,33 +183,34 @@ export class HabilitationService {
const now = new Date();
const pinCode = await this.generatePinCode();

const habilitation: Habilitation = await this.updateOne(body.id, {
strategy: {
type: TypeStrategyEnum.EMAIL,
pinCode,
pinCodeExpiration: this.getExpirationDate(now),
remainingAttempts: 10,
createdAt: now,
const habilitationUpdated: Habilitation = await this.updateOne(
habilitation.id,
{
strategy: {
type: TypeStrategyEnum.EMAIL,
pinCode,
pinCodeExpiration: this.getExpirationDate(now),
remainingAttempts: 10,
createdAt: now,
},
},
});
);

const { nom }: CommuneCOG = getCommune(habilitation.codeCommune);

if (habilitation.emailsCommune.length > 0) {
await this.mailerService.sendMail({
to: habilitation.emailsCommune,
subject: 'Demande de code d’identification',
template: 'code-pin',
bcc: this.configService.get('SMTP_BCC'),
context: {
apiUrl: this.configService.get('API_DEPOT_URL'),
pinCode,
nomCommune: nom,
},
});
}
await this.mailerService.sendMail({
to: habilitation.emailsCommune,
subject: 'Demande de code d’identification',
template: 'code-pin',
bcc: this.configService.get('SMTP_BCC'),
context: {
apiUrl: this.configService.get('API_DEPOT_URL'),
pinCode,
nomCommune: nom,
},
});

return habilitation;
return habilitationUpdated;
}

public async validateCodePin(
Expand Down
2 changes: 1 addition & 1 deletion src/modules/habilitation/habilitation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ describe('HABILITATION MODULE', () => {
it('SEND CODE PIN ALREADY SEND', async () => {
const client: Client2 = await createClient();
const habilitation: Partial<Habilitation> = {
codeCommune: '94000',
codeCommune: '91534',
emailsCommune: ['[email protected]'],
status: StatusHabilitationEnum.PENDING,
};
Expand Down

0 comments on commit 530bdf4

Please sign in to comment.