Skip to content

Commit

Permalink
Déplace sentinelles vérification présence query params code et state
Browse files Browse the repository at this point in the history
Co-authored-by: Fabien Lamarque <[email protected]>
  • Loading branch information
egaillot and Fabinout committed Jun 6, 2024
1 parent 0a9a09e commit 3f4134f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
31 changes: 16 additions & 15 deletions src/routes/routesAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,28 @@ const routesAuth = (config) => {
});

routes.get('/fcplus/connexion', (requete, reponse) => {
const paramsRequete = new URLSearchParams(requete.query).toString();
redirigeDepuisNavigateur(`/auth/fcplus/connexion_apres_redirection?${paramsRequete}`, reponse);
const { code, state } = requete.query;
if (typeof state === 'undefined' || state === '') {
reponse.status(400).json({ erreur: "Paramètre 'state' absent de la requête" });
} else if (typeof code === 'undefined' || code === '') {
reponse.status(400).json({ erreur: "Paramètre 'code' absent de la requête" });
} else {
const paramsRequete = new URLSearchParams(requete.query).toString();
redirigeDepuisNavigateur(`/auth/fcplus/connexion_apres_redirection?${paramsRequete}`, reponse);
}
});

routes.get(
'/fcplus/connexion_apres_redirection',
(...args) => middleware.verifieTamponUnique(...args),
(requete, reponse) => {
const { code, state } = requete.query;
if (typeof state === 'undefined' || state === '') {
reponse.status(400).json({ erreur: "Paramètre 'state' absent de la requête" });
} else if (typeof code === 'undefined' || code === '') {
reponse.status(400).json({ erreur: "Paramètre 'code' absent de la requête" });
} else {
connexionFCPlus(
{ adaptateurChiffrement, fabriqueSessionFCPlus },
code,
requete,
reponse,
);
}
const { code } = requete.query;
connexionFCPlus(
{ adaptateurChiffrement, fabriqueSessionFCPlus },
code,
requete,
reponse,
);
},
);

Expand Down
60 changes: 31 additions & 29 deletions test/routes/routesAuth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,37 @@ describe('Le serveur des routes `/auth`', () => {
});

describe('sur GET /auth/fcplus/connexion', () => {
it('redirige vers `/auth/fcplus/connexion_apres_redirection', () => axios
.get(`http://localhost:${port}/auth/fcplus/connexion`)
.then((reponse) => expect(reponse.data).toContain('<meta http-equiv="refresh" content="0; url=\'/auth/fcplus/connexion_apres_redirection'))
.catch(leveErreur));

it('transmets les paramètres reçus dans la requête', () => axios
.get(`http://localhost:${port}/auth/fcplus/connexion?state=unState&code=unCode`)
.then((reponse) => expect(reponse.data).toContain('?state=unState&code=unCode'))
.catch(leveErreur));
it("sert une erreur HTTP 400 (Bad Request) si le paramètre 'code' est manquant", () => {
expect.assertions(2);

return axios.get(`http://localhost:${port}/auth/fcplus/connexion?state=unState`)
.catch(({ response }) => {
expect(response.status).toBe(400);
expect(response.data).toEqual({ erreur: "Paramètre 'code' absent de la requête" });
});
});

it("sert une erreur HTTP 400 (Bad Request) si le paramètre 'state' est manquant", () => {
expect.assertions(2);

return axios.get(`http://localhost:${port}/auth/fcplus/connexion?code=unCode`)
.catch(({ response }) => {
expect(response.status).toBe(400);
expect(response.data).toEqual({ erreur: "Paramètre 'state' absent de la requête" });
});
});

describe('lorsque les paramètres `code` et `state` sont présents', () => {
it('redirige vers `/auth/fcplus/connexion_apres_redirection', () => axios
.get(`http://localhost:${port}/auth/fcplus/connexion?state=unState&code=unCode`)
.then((reponse) => expect(reponse.data).toContain('<meta http-equiv="refresh" content="0; url=\'/auth/fcplus/connexion_apres_redirection'))
.catch(leveErreur));

it('transmets les paramètres reçus dans la requête', () => axios
.get(`http://localhost:${port}/auth/fcplus/connexion?state=unState&code=unCode`)
.then((reponse) => expect(reponse.data).toContain('?state=unState&code=unCode'))
.catch(leveErreur));
});
});

describe('sur GET /auth/fcplus/connexion_apres_redirection', () => {
Expand Down Expand Up @@ -99,26 +121,6 @@ describe('Le serveur des routes `/auth`', () => {
});
});
});

it("sert une erreur HTTP 400 (Bad Request) si le paramètre 'code' est manquant", () => {
expect.assertions(2);

return axios.get(`http://localhost:${port}/auth/fcplus/connexion_apres_redirection?state=unState`)
.catch(({ response }) => {
expect(response.status).toBe(400);
expect(response.data).toEqual({ erreur: "Paramètre 'code' absent de la requête" });
});
});

it("sert une erreur HTTP 400 (Bad Request) si le paramètre 'state' est manquant", () => {
expect.assertions(2);

return axios.get(`http://localhost:${port}/auth/fcplus/connexion_apres_redirection?code=unCode`)
.catch(({ response }) => {
expect(response.status).toBe(400);
expect(response.data).toEqual({ erreur: "Paramètre 'state' absent de la requête" });
});
});
});

describe('sur GET /auth/fcplus/deconnexion', () => {
Expand Down

0 comments on commit 3f4134f

Please sign in to comment.