Invalid scenario scores are now explicitly blocked

This commit is contained in:
neviyn 2020-02-26 14:22:32 +00:00
parent c4a7f4cc58
commit e1df1142ce
2 changed files with 16 additions and 0 deletions

View File

@ -132,6 +132,12 @@ class ObservationsController {
logger.info("Attempted to add Observation without a tutor") logger.info("Attempted to add Observation without a tutor")
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Observation requires at least one Tutor") throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Observation requires at least one Tutor")
} }
for(scenario in newObservation.scenarios){
if(!scenario.ratingsAllValid()){
logger.info("Attempted to submit scenario with invalid score, $scenario")
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Scenario scores must be between 1 and 5")
}
}
var observation = Observation( var observation = Observation(
site = site.get(), site = site.get(),
date = LocalDate.now(), date = LocalDate.now(),

View File

@ -125,6 +125,16 @@ class ObservationsControllerTest {
assertEquals(1L, result) assertEquals(1L, result)
} }
@Test(expected = ResponseStatusException::class)
fun testAddObservationInvalidScenario() {
val site = Site(1, "X")
val tutor = Tutor(1, "Foo Bar", site)
doReturn(Optional.of(site)).`when`(siteRepository).findById(1)
doReturn(listOf(tutor)).`when`(tutorRepository).findAllById(listOf(1))
val newData = NewObservation(1, TrainingType.INITIAL, "An Observation", listOf(Scenario(1, "Something", 5, "", "", 100, "", "", 0, "", "", -1, "", "", Byte.MAX_VALUE, "", "", Byte.MIN_VALUE, "", "", 1, "", "", 5, "", "")), listOf(1), "Mr X")
controller.addObservation(newData)
}
@Test @Test
fun testGetObservations_SiteAndTutorNull() { fun testGetObservations_SiteAndTutorNull() {
val request = ObservationsRequest(null, null, "", "", LocalDate.now(), LocalDate.now()) val request = ObservationsRequest(null, null, "", "", LocalDate.now(), LocalDate.now())