From e1df1142ce2b091beffa9c2194aca4b493c75726 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Wed, 26 Feb 2020 14:22:32 +0000 Subject: [PATCH] Invalid scenario scores are now explicitly blocked --- .../controller/ObservationsController.kt | 6 ++++++ .../controller/ObservationsControllerTest.kt | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsController.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsController.kt index c8ca89f..fd1f539 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsController.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsController.kt @@ -132,6 +132,12 @@ class ObservationsController { logger.info("Attempted to add Observation without a 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( site = site.get(), date = LocalDate.now(), diff --git a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt index d589b1e..4ee1fcf 100644 --- a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt @@ -125,6 +125,16 @@ class ObservationsControllerTest { 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 fun testGetObservations_SiteAndTutorNull() { val request = ObservationsRequest(null, null, "", "", LocalDate.now(), LocalDate.now())