Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduis objet métier Utilisateur #20

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/modeles/sessionFCPlus.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class SessionFCPlus {
jwtInfosUtilisateur,
this.urlClefsPubliques,
))
.then((infosDechiffrees) => Object.assign(
infosDechiffrees,
{ jwtSessionFCPlus: this.jwt, nonce: this.nonce },
))
.then((infosDechiffrees) => ({
prenom: infosDechiffrees.given_name,
nomUsage: infosDechiffrees.family_name,
jwtSessionFCPlus: this.jwt,
nonce: this.nonce,
}))
.catch((e) => Promise.reject(new ErreurEchecAuthentification(e.message)));
}

Expand Down
13 changes: 13 additions & 0 deletions src/modeles/utilisateur.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Utilisateur {
constructor(donnees) {
this.prenom = donnees.prenom;
this.nomUsage = donnees.nomUsage;
this.jwtSessionFCPlus = donnees.jwtSessionFCPlus;
}

afficheToi() {
return `${this.prenom} ${this.nomUsage}`;
}
}

module.exports = Utilisateur;
6 changes: 5 additions & 1 deletion src/routes/middleware.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Utilisateur = require('../modeles/utilisateur');

class Middleware {
constructor(config) {
this.adaptateurChiffrement = config.adaptateurChiffrement;
Expand All @@ -6,7 +8,9 @@ class Middleware {

renseigneUtilisateurCourant(requete, _reponse, suite) {
return this.adaptateurChiffrement.verifieJeton(requete.session.jeton, this.secret)
.then((infosUtilisateur) => { requete.utilisateurCourant = infosUtilisateur; })
.then((infosUtilisateur) => {
requete.utilisateurCourant = new Utilisateur(infosUtilisateur);
})
.catch(() => { requete.utilisateurCourant = undefined; })
.then(() => suite());
}
Expand Down
2 changes: 1 addition & 1 deletion src/vues/accueil.pug
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ block styles
block page
if avecConnexionFCPlus
if infosUtilisateur
p= `Utilisateur courant : ${infosUtilisateur.given_name} ${infosUtilisateur.family_name}`
p Utilisateur courant : #{infosUtilisateur.afficheToi()}
a(href = '/auth/fcplus/destructionSession') Déconnexion
else
p Pas d'utilisateur courant
Expand Down
5 changes: 3 additions & 2 deletions test/modeles/sessionFCPlus.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('Une session FranceConnect+', () => {
try {
expect(jwt).toBe('aaa');
expect(url).toBe('http://example.com');
return Promise.resolve({ uneClef: 'uneValeur' });
return Promise.resolve({ given_name: 'Anne', family_name: 'Durand' });
} catch (e) {
return Promise.reject(e);
}
Expand All @@ -96,7 +96,8 @@ describe('Une session FranceConnect+', () => {

return session.enJSON()
.then((json) => {
expect(json).toHaveProperty('uneClef', 'uneValeur');
expect(json).toHaveProperty('prenom', 'Anne');
expect(json).toHaveProperty('nomUsage', 'Durand');
expect(json).toHaveProperty('jwtSessionFCPlus', '999');
});
});
Expand Down
8 changes: 8 additions & 0 deletions test/modeles/utilisateur.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Utilisateur = require('../../src/modeles/utilisateur');

describe("L'utilisateur courant", () => {
it("sait s'afficher", () => {
const utilisateur = new Utilisateur({ prenom: 'Juliette', nomUsage: 'Haucourt' });
expect(utilisateur.afficheToi()).toBe('Juliette Haucourt');
});
});
4 changes: 2 additions & 2 deletions test/routes/middleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ describe('Le middleware OOTS-France', () => {
});

it("renseigne les infos de l'utilisateur courant dans la requête", (suite) => {
adaptateurChiffrement.verifieJeton = () => Promise.resolve({ infos: 'des infos' });
adaptateurChiffrement.verifieJeton = () => Promise.resolve({ prenom: 'Pierre', nomUsage: 'Jax' });

const middleware = new Middleware(config);
expect(requete.utilisateurCourant).toBeUndefined();

middleware.renseigneUtilisateurCourant(requete, null, () => {
try {
expect(requete.utilisateurCourant).toEqual({ infos: 'des infos' });
expect(requete.utilisateurCourant).toEqual({ prenom: 'Pierre', nomUsage: 'Jax' });
suite();
} catch (e) { suite(e); }
})
Expand Down
3 changes: 2 additions & 1 deletion test/routes/routesAuth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const axios = require('axios');

const { leveErreur } = require('./utils');
const serveurTest = require('./serveurTest');
const Utilisateur = require('../../src/modeles/utilisateur');

describe('Le serveur des routes `/auth`', () => {
const serveur = serveurTest();
Expand Down Expand Up @@ -143,7 +144,7 @@ describe('Le serveur des routes `/auth`', () => {
describe('sur GET /auth/fcplus/destructionSession', () => {
it("appelle le middleware pour renseigner les infos de l'utilisateur courant", () => {
serveur.middleware().reinitialise({
utilisateurCourant: { given_name: '', family_name: '', jwtSessionFCPlus: 'abcdef' },
utilisateurCourant: new Utilisateur({ prenom: 'Jean', nomUsage: 'Max', jwtSessionFCPlus: 'abcdef' }),
});

serveur.adaptateurFranceConnectPlus().urlDestructionSession = () => Promise.resolve(`http://localhost:${port}`);
Expand Down
9 changes: 5 additions & 4 deletions test/routes/routesBase.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const axios = require('axios');

const serveurTest = require('./serveurTest');
const { leveErreur } = require('./utils');
const Utilisateur = require('../../src/modeles/utilisateur');

describe('Le serveur des routes `/`', () => {
const serveur = serveurTest();
Expand All @@ -25,10 +26,10 @@ describe('Le serveur des routes `/`', () => {

it("affiche prénom et nom de l'utilisateur courant s'il existe", () => {
serveur.middleware().reinitialise({
utilisateurCourant: {
given_name: 'Sandra',
family_name: 'Nicouette',
},
utilisateurCourant: new Utilisateur({
prenom: 'Sandra',
nomUsage: 'Nicouette',
}),
});

return axios.get(`http://localhost:${port}/`)
Expand Down
Loading