Skip to content

Commit

Permalink
Introduis un adaptateur de journalisation
Browse files Browse the repository at this point in the history
Co-authored-by: Emmanuel Gaillot <[email protected]>
  • Loading branch information
Fabinout and egaillot committed Jul 4, 2024
1 parent 9b60b45 commit 2cea15f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 31 deletions.
8 changes: 3 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const SiteVitrine = require('./src/siteVitrine');
const adaptateurChiffrement = require('./src/adaptateurs/adaptateurChiffrement');
const adaptateurEnvironnement = require('./src/adaptateurs/adaptateurEnvironnement');
const adaptateurFranceConnectPlus = require('./src/adaptateurs/adaptateurFranceConnectPlus');
const journal = require('./src/adaptateurs/journal');
const FabriqueSessionFCPlus = require('./src/modeles/fabriqueSessionFCPlus');
const Middleware = require('./src/routes/middleware');

Expand All @@ -16,15 +17,12 @@ const serveur = SiteVitrine.creeServeur({
adaptateurEnvironnement,
adaptateurFranceConnectPlus,
fabriqueSessionFCPlus,
journal,
middleware,
});

const port = process.env.PORT || 3000;

serveur.ecoute(port, () => {
/* eslint-disable no-console */

console.log(`Le site vitrine est démarré et écoute le port ${port} !…`);

/* eslint-enable no-console */
journal.consigne(`Le site vitrine est démarré et écoute le port ${port} !…`);
});
5 changes: 5 additions & 0 deletions src/adaptateurs/journal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable no-console */
const consigne = console.log;
/* eslint-enable no-console */

module.exports = { consigne };
10 changes: 8 additions & 2 deletions src/api/connexionFCPlus.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const { stockeDansCookieSession } = require('../routes/utils');

const connexionFCPlus = (config, code, requete, reponse) => {
const { adaptateurChiffrement, adaptateurEnvironnement, fabriqueSessionFCPlus } = config;
const {
adaptateurChiffrement,
adaptateurEnvironnement,
fabriqueSessionFCPlus,
journal,
} = config;

const secret = adaptateurEnvironnement.secretJetonSession();

Expand All @@ -13,8 +18,9 @@ const connexionFCPlus = (config, code, requete, reponse) => {
return stockeDansCookieSession(infos, adaptateurChiffrement, requete);
}))
.then(() => reponse.render('redirectionNavigateur', { destination: '/' }))
.catch(() => {
.catch((e) => {
requete.session.jeton = undefined;
journal.consigne(`Échec authentification (${e.message})`);
reponse.render('redirectionNavigateur', { destination: '/auth/fcplus/destructionSession' });
});
};
Expand Down
14 changes: 8 additions & 6 deletions src/routes/routesAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const routesAuth = (config) => {
adaptateurEnvironnement,
adaptateurFranceConnectPlus,
fabriqueSessionFCPlus,
journal,
middleware,
} = config;

Expand Down Expand Up @@ -56,12 +57,13 @@ const routesAuth = (config) => {
(...args) => middleware.verifieTamponUnique(...args),
(requete, reponse) => {
const { code } = requete.query;
connexionFCPlus(
{ adaptateurChiffrement, adaptateurEnvironnement, fabriqueSessionFCPlus },
code,
requete,
reponse,
);
const adaptateurs = {
adaptateurChiffrement,
adaptateurEnvironnement,
fabriqueSessionFCPlus,
journal,
};
connexionFCPlus(adaptateurs, code, requete, reponse);
},
);

Expand Down
2 changes: 2 additions & 0 deletions src/siteVitrine.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const creeServeur = (config) => {
adaptateurEnvironnement,
adaptateurFranceConnectPlus,
fabriqueSessionFCPlus,
journal,
middleware,
} = config;
let serveur;
Expand All @@ -35,6 +36,7 @@ const creeServeur = (config) => {
adaptateurEnvironnement,
adaptateurFranceConnectPlus,
fabriqueSessionFCPlus,
journal,
middleware,
}));

Expand Down
56 changes: 38 additions & 18 deletions test/api/connexionFCPlus.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ describe('Le requêteur de connexion FC+', () => {
const adaptateurChiffrement = {};
const adaptateurEnvironnement = {};
const fabriqueSessionFCPlus = {};
const config = { adaptateurChiffrement, adaptateurEnvironnement, fabriqueSessionFCPlus };
const journal = {};
const config = {
adaptateurChiffrement,
adaptateurEnvironnement,
fabriqueSessionFCPlus,
journal,
};
const requete = {};
const reponse = {};

Expand All @@ -15,6 +21,7 @@ describe('Le requêteur de connexion FC+', () => {
fabriqueSessionFCPlus.nouvelleSession = () => Promise.resolve({
enJSON: () => Promise.resolve({}),
});
journal.consigne = () => {};
requete.session = {};
reponse.render = () => Promise.resolve();
reponse.status = () => reponse;
Expand All @@ -39,25 +46,38 @@ describe('Le requêteur de connexion FC+', () => {
.then(() => expect(requete.session.jeton).toBeUndefined());
});

it('redirige vers la destruction de session FC+ si le nonce retourné est différent du nonce en session', () => {
expect.assertions(2);
adaptateurChiffrement.verifieJeton = () => Promise.resolve({ nonce: 'unNonce' });
describe('quand nonce retourné diffère du nonce en session', () => {
beforeEach(() => {
adaptateurChiffrement.verifieJeton = () => Promise.resolve({ nonce: 'unNonce' });

requete.session.jeton = { nonce: 'abcde' };
fabriqueSessionFCPlus.nouvelleSession = () => Promise.resolve({
enJSON: () => Promise.resolve({ nonce: 'oups' }),
requete.session.jeton = { nonce: 'abcde' };
fabriqueSessionFCPlus.nouvelleSession = () => Promise.resolve({
enJSON: () => Promise.resolve({ nonce: 'oups' }),
});
});

it("journalise l'erreur", () => {
expect.assertions(1);

journal.consigne = (entree) => { expect(entree).toBe('Échec authentification (nonce invalide)'); };

return connexionFCPlus(config, 'unCode', requete, reponse);
});

reponse.render = (nomModelePage, { destination }) => {
try {
expect(nomModelePage).toBe('redirectionNavigateur');
expect(destination).toBe('/auth/fcplus/destructionSession');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};

return connexionFCPlus(config, 'unCode', requete, reponse);
it('redirige vers la destruction de session FC+', () => {
expect.assertions(2);

reponse.render = (nomModelePage, { destination }) => {
try {
expect(nomModelePage).toBe('redirectionNavigateur');
expect(destination).toBe('/auth/fcplus/destructionSession');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
};

return connexionFCPlus(config, 'unCode', requete, reponse);
});
});
});
7 changes: 7 additions & 0 deletions test/routes/serveurTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const serveurTest = () => {
let adaptateurEnvironnement;
let adaptateurFranceConnectPlus;
let fabriqueSessionFCPlus;
let journal;
let middleware;

let serveur;
Expand Down Expand Up @@ -44,13 +45,18 @@ const serveurTest = () => {
nouvelleSession: () => Promise.resolve({ enJSON: () => Promise.resolve({}) }),
};

journal = {
consigne: () => {},
};

middleware = new MiddlewareFantaisie({});

serveur = OOTS_FRANCE.creeServeur({
adaptateurChiffrement,
adaptateurEnvironnement,
adaptateurFranceConnectPlus,
fabriqueSessionFCPlus,
journal,
middleware,
});

Expand All @@ -65,6 +71,7 @@ const serveurTest = () => {
adaptateurFranceConnectPlus: () => adaptateurFranceConnectPlus,
arrete,
fabriqueSessionFCPlus: () => fabriqueSessionFCPlus,
journal: () => journal,
initialise,
middleware: () => middleware,
port,
Expand Down

0 comments on commit 2cea15f

Please sign in to comment.