From b86a7bedb9761815cbdf2aa009cd991b6644dfd6 Mon Sep 17 00:00:00 2001 From: Manfred Zingl Date: Fri, 2 Aug 2024 13:26:43 +0200 Subject: [PATCH] check for valid decimalDigits range --- .../com/campudus/tableaux/helper/JsonUtils.scala | 14 ++++++++++++-- .../api/structure/ChangeStructureTest.scala | 14 ++++++++++++-- .../tableaux/api/structure/CreateColumnTest.scala | 14 +++++++++++++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/main/scala/com/campudus/tableaux/helper/JsonUtils.scala b/src/main/scala/com/campudus/tableaux/helper/JsonUtils.scala index 17baa480..aac036ec 100644 --- a/src/main/scala/com/campudus/tableaux/helper/JsonUtils.scala +++ b/src/main/scala/com/campudus/tableaux/helper/JsonUtils.scala @@ -105,7 +105,7 @@ object JsonUtils extends LazyLogging { val maxLength = Try(json.getInteger("maxLength").intValue()).toOption val minLength = Try(json.getInteger("minLength").intValue()).toOption - val decimalDigits = Try(json.getInteger("decimalDigits").intValue()).toOption + val decimalDigits = parseDecimalDigits(json) // languageType or deprecated multilanguage // if languageType == 'country' countryCodes must be specified @@ -283,6 +283,16 @@ object JsonUtils extends LazyLogging { } } + private def parseDecimalDigits(json: JsonObject): Option[Int] = { + val decimalDigits = Try(json.getInteger("decimalDigits").intValue()).toOption + + decimalDigits.map({ + case value if value > 10 || value < 0 => + throw InvalidJsonException(s"Decimal digits must be between 0 and 10, but was $value.", "decimalDigits") + case value => value + }) + } + def toRowValueSeq(json: JsonObject): Seq[Seq[_]] = { (for { checkedRowList <- toJsonObjectSeq("rows", json) @@ -383,7 +393,7 @@ object JsonUtils extends LazyLogging { val maxLength = getNullableJsonIntegerValue("maxLength", json).toOption val minLength = getNullableJsonIntegerValue("minLength", json).toOption - val decimalDigits = getNullableJsonIntegerValue("decimalDigits", json).toOption + val decimalDigits = parseDecimalDigits(json) ( name, diff --git a/src/test/scala/com/campudus/tableaux/api/structure/ChangeStructureTest.scala b/src/test/scala/com/campudus/tableaux/api/structure/ChangeStructureTest.scala index b46eb2a3..a99ef9f4 100644 --- a/src/test/scala/com/campudus/tableaux/api/structure/ChangeStructureTest.scala +++ b/src/test/scala/com/campudus/tableaux/api/structure/ChangeStructureTest.scala @@ -94,18 +94,28 @@ class ChangeStructureTest extends TableauxTestBase { @Test def changeColumnDecimalDigits(implicit c: TestContext): Unit = okTest { - val postJson = Json.obj("decimalDigits" -> 4) + val postJson = Json.obj("decimalDigits" -> 10) for { _ <- createDefaultTable() resultPost <- sendRequest("POST", "/tables/1/columns/2", postJson) resultGet <- sendRequest("GET", "/tables/1/columns/2") } yield { - assertEquals(4, resultGet.getInteger("decimalDigits")) + assertEquals(10, resultGet.getInteger("decimalDigits")) assertEquals(resultPost, resultGet) } } + @Test + def changeColumnDecimalDigitsTooHigh(implicit c: TestContext): Unit = exceptionTest("error.json.decimalDigits") { + val postJson = Json.obj("decimalDigits" -> 11) + + for { + _ <- createDefaultTable() + resultPost <- sendRequest("POST", "/tables/1/columns/2", postJson) + } yield () + } + @Test def changeColumnKindWhichShouldFail(implicit c: TestContext): Unit = { okTest { diff --git a/src/test/scala/com/campudus/tableaux/api/structure/CreateColumnTest.scala b/src/test/scala/com/campudus/tableaux/api/structure/CreateColumnTest.scala index e6ae8294..56919308 100644 --- a/src/test/scala/com/campudus/tableaux/api/structure/CreateColumnTest.scala +++ b/src/test/scala/com/campudus/tableaux/api/structure/CreateColumnTest.scala @@ -228,7 +228,7 @@ class CreateColumnTest extends TableauxTestBase { def createNumberColumnWithCustomDecimalDigits(implicit c: TestContext): Unit = { okTest { val createColumn1 = createNumberColumnJson("column1", None) - val createColumn2 = createNumberColumnJson("column2", Some(5)) + val createColumn2 = createNumberColumnJson("column2", Some(10)) val expectedJson = Json.obj( "status" -> "ok", "columns" -> Json.arr( @@ -272,6 +272,18 @@ class CreateColumnTest extends TableauxTestBase { } } + @Test + def createNumberColumnWithCustomDecimalDigitsTooHigh(implicit c: TestContext): Unit = { + exceptionTest("error.json.decimalDigits") { + val createColumn = createNumberColumnJson("column2", Some(11)) + + for { + _ <- sendRequest("POST", "/tables", createTableJson) + test <- sendRequest("POST", "/tables/1/columns", createColumn) + } yield () + } + } + @Test def createIntegerColumn(implicit c: TestContext): Unit = { okTest {