Skip to content

Commit

Permalink
feat : Envoyer 404 pour élèves pas inscrits
Browse files Browse the repository at this point in the history
  • Loading branch information
aureliadelzottoOCTO committed Nov 28, 2024
1 parent 554d396 commit a1254bb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package fr.gouv.monprojetsup.authentification.application.controller
import fr.gouv.monprojetsup.authentification.domain.entity.ProfilEleve
import fr.gouv.monprojetsup.authentification.domain.entity.ProfilUtilisateur
import fr.gouv.monprojetsup.authentification.filter.IdentificationFilter.Companion.GRANTED_AUTHORITY_UTILISATEUR
import fr.gouv.monprojetsup.commun.erreur.domain.EleveSansCompteException
import fr.gouv.monprojetsup.commun.erreur.domain.MonProjetSupForbiddenException
import fr.gouv.monprojetsup.commun.erreur.domain.eleveSansCompteException
import org.springframework.security.core.context.SecurityContextHolder

abstract class AuthentifieController {
Expand All @@ -22,7 +22,7 @@ abstract class AuthentifieController {
val authentification = SecurityContextHolder.getContext().authentication
return when (val utilisateur = authentification.principal) {
is ProfilEleve.AvecProfilExistant -> utilisateur
is ProfilEleve.SansCompte -> throw EleveSansCompteException()
is ProfilEleve.SansCompte -> throw eleveSansCompteException()
else -> throw MonProjetSupForbiddenException("UTILISATEUR_PAS_ELEVE", "L'utilisateur connecté n'est pas un élève identifié")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ data class MonProjetSupNotFoundException(
val origine: Throwable? = null,
) : MonProjetSup4xxExceptions(code, msg, origine)

class EleveSansCompteException : MonProjetSupForbiddenException("ELEVE_SANS_COMPTE", "L'élève connecté n'a pas encore crée son compte")
fun eleveSansCompteException() =
MonProjetSupNotFoundException(
code = "ELEVE_SANS_COMPTE",
msg = "L'élève connecté n'a pas encore crée son compte",
)

open class MonProjetSupForbiddenException(
override val code: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fr.gouv.monprojetsup.eleve.application.controller

import fr.gouv.monprojetsup.authentification.application.controller.AuthentifieController
import fr.gouv.monprojetsup.authentification.domain.entity.ProfilEleve
import fr.gouv.monprojetsup.commun.erreur.domain.eleveSansCompteException
import fr.gouv.monprojetsup.eleve.application.dto.AjoutCompteParcoursupDTO
import fr.gouv.monprojetsup.eleve.application.dto.ModificationProfilDTO
import fr.gouv.monprojetsup.eleve.application.dto.ProfilDTO
Expand Down Expand Up @@ -47,9 +49,13 @@ class ProfilEleveController(
description = "Récupère le profil de l'utilisateur connecté tout en récupérant ses favoris Parcoursup",
)
fun getProfilEleve(): ProfilDTO {
val profil = recupererEleveAvecProfilExistant()
val profilMisAJour = miseAJourFavorisParcoursupService.mettreAJourFavorisParcoursup(profil)
return ProfilDTO(profilMisAJour)
when (val profil = recupererEleve()) {
is ProfilEleve.AvecProfilExistant -> {
val profilMisAJour = miseAJourFavorisParcoursupService.mettreAJourFavorisParcoursup(profil)
return ProfilDTO(profilMisAJour)
}
is ProfilEleve.SansCompte -> throw eleveSansCompteException()
}
}

@PostMapping("/parcoursup")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,23 +489,23 @@ class ProfilEleveControllerTest(

@ConnecteAvecUnEleve(idEleve = "123f627c-36dd-4df5-897b-159443a6d49c")
@Test
fun `si l'élève n'existe pas, doit renvoyer 403`() {
fun `si l'élève n'existe pas, doit renvoyer 404`() {
// Given
val idProfilInconnu = "123f627c-36dd-4df5-897b-159443a6d49c"
val profilSansCompte = ProfilEleve.SansCompte(id = idProfilInconnu)
given(recupererEleveService.recupererEleve(id = idProfilInconnu)).willReturn(profilSansCompte)

// When & Then
mvc.perform(get("/api/v1/profil")).andDo(print())
.andExpect(status().isForbidden)
.andExpect(status().isNotFound)
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON))
.andExpect(
content().json(
"""
{
"type": "about:blank",
"title": "ELEVE_SANS_COMPTE",
"status": 403,
"status": 404,
"detail": "L'élève connecté n'a pas encore crée son compte",
"instance": "/api/v1/profil"
}
Expand Down Expand Up @@ -749,7 +749,7 @@ class ProfilEleveControllerTest(

@ConnecteAvecUnEleve(idEleve = "d26da5c2-c38d-4c07-9ef3-9da2443846df")
@Test
fun `si l'élève n'a pas encore crée de profil, doit retourner 403`() {
fun `si l'élève n'a pas encore crée de profil, doit retourner 404`() {
// Given
val id = "d26da5c2-c38d-4c07-9ef3-9da2443846df"
given(recupererEleveService.recupererEleve(id)).willReturn(ProfilEleve.SansCompte(id))
Expand All @@ -764,15 +764,15 @@ class ProfilEleveControllerTest(
}
""".trimIndent()
mvc.perform(post("/api/v1/profil/parcoursup").contentType(MediaType.APPLICATION_JSON).content(bodyEntree))
.andExpect(status().isForbidden)
.andExpect(status().isNotFound)
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON))
.andExpect(
content().json(
"""
{
"type": "about:blank",
"title": "ELEVE_SANS_COMPTE",
"status": 403,
"status": 404,
"detail": "L'élève connecté n'a pas encore crée son compte",
"instance": "/api/v1/profil/parcoursup"
}
Expand Down

0 comments on commit a1254bb

Please sign in to comment.