Skip to content

Commit

Permalink
refactor(api): move route
Browse files Browse the repository at this point in the history
  • Loading branch information
igorissen authored Jun 21, 2024
1 parent b82f45c commit 695c1fb
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 156 deletions.
44 changes: 0 additions & 44 deletions api/lib/application/account-recovery/index.js

This file was deleted.

2 changes: 0 additions & 2 deletions api/lib/routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as accountRecovery from './application/account-recovery/index.js';
import * as adminMembers from './application/admin-members/index.js';
import * as authentication from './application/authentication/index.js';
import * as cache from './application/cache/index.js';
Expand Down Expand Up @@ -36,7 +35,6 @@ import * as userOrgaSettings from './application/user-orga-settings/index.js';
import * as users from './application/users/index.js';

const routes = [
accountRecovery,
adminMembers,
authentication,
cache,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import Joi from 'joi';
import JoiDate from '@joi/date';
import BaseJoi from 'joi';
import XRegExp from 'xregexp';

import { config } from '../../../shared/config.js';
import { accountRecoveryController } from './account-recovery.controller.js';

const Joi = BaseJoi.extend(JoiDate);
const { passwordValidationPattern } = config.account;
const inePattern = new RegExp('^[0-9]{9}[a-zA-Z]{2}$');
const inaPattern = new RegExp('^[0-9]{10}[a-zA-Z]{1}$');

export const accountRecoveryRoutes = [
{
Expand Down Expand Up @@ -48,4 +52,33 @@ export const accountRecoveryRoutes = [
tags: ['identity-access-management', 'api', 'account-recovery'],
},
},
{
method: 'POST',
path: '/api/account-recovery',
config: {
auth: false,
handler: accountRecoveryController.sendEmailForAccountRecovery,
validate: {
payload: Joi.object({
data: {
attributes: {
'first-name': Joi.string().empty(Joi.string().regex(/^\s*$/)).required(),
'last-name': Joi.string().empty(Joi.string().regex(/^\s*$/)).required(),
'ine-ina': Joi.alternatives().try(
Joi.string().regex(inePattern).required(),
Joi.string().regex(inaPattern).required(),
),
birthdate: Joi.date().format('YYYY-MM-DD').required(),
email: Joi.string().email().required(),
},
},
}),
options: {
allowUnknown: true,
},
},
notes: ["- Permet d'envoyer un mail de demande d'ajout de mot de passe pour récupérer son compte Pix."],
tags: ['api', 'account-recovery'],
},
},
];

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,103 @@ describe('Acceptance | Identity Access Management | Application | Route | accoun
});
});
});

describe('POST /api/account-recovery', function () {
const studentInformation = {
ineIna: '123456789aa',
firstName: 'Jude',
lastName: 'Law',
birthdate: '2016-06-01',
};

const createUserWithSeveralOrganizationLearners = async ({ email = '[email protected]' } = {}) => {
const user = databaseBuilder.factory.buildUser.withRawPassword({
id: 8,
firstName: 'Judy',
lastName: 'Howl',
email,
username: 'jude.law0601',
});
const organization = databaseBuilder.factory.buildOrganization({
id: 7,
name: 'Collège Hollywoodien',
});
const latestOrganization = databaseBuilder.factory.buildOrganization({
id: 2,
name: 'Super Collège Hollywoodien',
});
databaseBuilder.factory.buildOrganizationLearner({
userId: user.id,
...studentInformation,
nationalStudentId: studentInformation.ineIna.toUpperCase(),
organizationId: organization.id,
updatedAt: new Date('2005-01-01T15:00:00Z'),
});
databaseBuilder.factory.buildOrganizationLearner({
userId: user.id,
...studentInformation,
nationalStudentId: studentInformation.ineIna.toUpperCase(),
organizationId: latestOrganization.id,
updatedAt: new Date('2010-01-01T15:00:00Z'),
});
await databaseBuilder.commit();
};

it('returns 204 HTTP status code', async function () {
// given
await createUserWithSeveralOrganizationLearners();
const newEmail = '[email protected]';

const options = {
method: 'POST',
url: '/api/account-recovery',
payload: {
data: {
attributes: {
'ine-ina': studentInformation.ineIna,
'first-name': studentInformation.firstName,
'last-name': studentInformation.lastName,
birthdate: studentInformation.birthdate,
email: newEmail,
},
},
},
};

// when
const response = await server.inject(options);

// then
expect(response.statusCode).to.equal(204);
});

it('returns 400 if email already exists', async function () {
// given
const newEmail = '[email protected]';
await createUserWithSeveralOrganizationLearners({ email: newEmail });

const options = {
method: 'POST',
url: '/api/account-recovery',
payload: {
data: {
attributes: {
'ine-ina': studentInformation.ineIna,
'first-name': studentInformation.firstName,
'last-name': studentInformation.lastName,
birthdate: studentInformation.birthdate,
email: newEmail,
},
},
},
};

// when
const response = await server.inject(options);

// then
expect(response.statusCode).to.equal(400);
expect(response.result.errors[0].detail).to.equal('Cette adresse e-mail est déjà utilisée.');
});
});
});

0 comments on commit 695c1fb

Please sign in to comment.