From a1f25ff4e6d1317437cede3dd5f04614d0d392f0 Mon Sep 17 00:00:00 2001 From: Manfred Zingl Date: Fri, 6 Oct 2023 18:09:23 +0200 Subject: [PATCH] add row permission test with more than one perm --- .../database/model/tableaux/RowModel.scala | 2 +- .../api/content/CreateHistoryTest.scala | 72 +++++++------- .../api/content/RowLevelAnnotationsTest.scala | 97 +++++++++++++++++++ 3 files changed, 133 insertions(+), 38 deletions(-) diff --git a/src/main/scala/com/campudus/tableaux/database/model/tableaux/RowModel.scala b/src/main/scala/com/campudus/tableaux/database/model/tableaux/RowModel.scala index d1ed61f7..a664d4cc 100644 --- a/src/main/scala/com/campudus/tableaux/database/model/tableaux/RowModel.scala +++ b/src/main/scala/com/campudus/tableaux/database/model/tableaux/RowModel.scala @@ -607,7 +607,7 @@ class UpdateRowModel(val connection: DatabaseConnection) extends DatabaseQuery w case rowPermissions: RowPermissions => connection.query( s"UPDATE user_table_$tableId SET row_permissions = ?", - Json.arr(rowPermissions.value.toString()) + Json.arr(rowPermissions.rowPermissions.toString()) ) } }) diff --git a/src/test/scala/com/campudus/tableaux/api/content/CreateHistoryTest.scala b/src/test/scala/com/campudus/tableaux/api/content/CreateHistoryTest.scala index a8809808..60ce3bde 100644 --- a/src/test/scala/com/campudus/tableaux/api/content/CreateHistoryTest.scala +++ b/src/test/scala/com/campudus/tableaux/api/content/CreateHistoryTest.scala @@ -2586,43 +2586,41 @@ class CreateAnnotationHistoryTest extends TableauxTestBase with TestHelper { } } - // TODO add test for adding annotations for all rows at once (currently failing with runtime error) - - // @Test - // def addRowPermissionAnnotation_addPermissionFlagToMultipleRows(implicit c: TestContext): Unit = { - // okTest { - // val expected = - // """ - // |{ - // | "event": "annotation_changed", - // | "historyType": "row_permissions", - // | "valueType": "permissions", - // | "value": ["perm_1"] - // |} - // """.stripMargin - - // for { - - // _ <- createTableWithMultilanguageColumns("history test") - // _ <- sendRequest("POST", "/tables/1/rows") - // _ <- sendRequest("POST", "/tables/1/rows") - // _ <- sendRequest("POST", "/tables/1/rows") - - // _ <- sendRequest("PATCH", "/tables/1/rows/1/annotations", Json.obj("permissions" -> Json.arr("perm_1"))) - - // rows <- sendRequest("GET", "/tables/1/history?historyType=row_permissions").map(toRowsArray) - - // row1 = rows.get[JsonObject](0) - // row2 = rows.get[JsonObject](1) - // row3 = rows.get[JsonObject](2) - // } yield { - // assertEquals(3, rows.size()) - // assertJSONEquals(expected, row1.toString) - // assertJSONEquals(expected, row2.toString) - // assertJSONEquals(expected, row3.toString) - // } - // } - // } + @Test + def addRowPermissionAnnotation_addPermissionFlagToMultipleRows(implicit c: TestContext): Unit = { + okTest { + val expected = + """ + |{ + | "event": "annotation_changed", + | "historyType": "row_permissions", + | "valueType": "permissions", + | "value": ["perm_1"] + |} + """.stripMargin + + for { + + _ <- createTableWithMultilanguageColumns("history test") + _ <- sendRequest("POST", "/tables/1/rows") + _ <- sendRequest("POST", "/tables/1/rows") + _ <- sendRequest("POST", "/tables/1/rows") + + _ <- sendRequest("PATCH", "/tables/1/rows/annotations", Json.obj("permissions" -> Json.arr("perm_1"))) + + rows <- sendRequest("GET", "/tables/1/history?historyType=row_permissions").map(toRowsArray) + + row1 = rows.get[JsonObject](0) + row2 = rows.get[JsonObject](1) + row3 = rows.get[JsonObject](2) + } yield { + assertEquals(3, rows.size()) + assertJSONEquals(expected, row1.toString) + assertJSONEquals(expected, row2.toString) + assertJSONEquals(expected, row3.toString) + } + } + } } @RunWith(classOf[VertxUnitRunner]) diff --git a/src/test/scala/com/campudus/tableaux/api/content/RowLevelAnnotationsTest.scala b/src/test/scala/com/campudus/tableaux/api/content/RowLevelAnnotationsTest.scala index 58ff081a..662423b8 100644 --- a/src/test/scala/com/campudus/tableaux/api/content/RowLevelAnnotationsTest.scala +++ b/src/test/scala/com/campudus/tableaux/api/content/RowLevelAnnotationsTest.scala @@ -98,3 +98,100 @@ class RowLevelAnnotationsTest extends TableauxTestBase { } } } + +@RunWith(classOf[VertxUnitRunner]) +class RowPermissionAnnotationsTest extends TableauxTestBase { + + @Test + def createRowAndSetPermissionFlagsForSingleRow(implicit c: TestContext): Unit = { + okTest { + for { + tableId <- createEmptyDefaultTable() + + // empty row + result <- sendRequest("POST", s"/tables/$tableId/rows") + rowId = result.getLong("id") + + _ <- sendRequest( + "PATCH", + s"/tables/$tableId/rows/$rowId/annotations", + Json.obj("permissions" -> Json.arr("perm_1", "perm_2")) + ) + + rowJson1 <- sendRequest("GET", s"/tables/$tableId/rows/$rowId") + + _ <- sendRequest("PATCH", s"/tables/$tableId/rows/$rowId/annotations", Json.obj("permissions" -> Json.arr())) + + rowJson2 <- sendRequest("GET", s"/tables/$tableId/rows/$rowId") + } yield { + val expectedRowJson1 = Json.obj( + "status" -> "ok", + "id" -> rowId, + "permissions" -> Json.arr("perm_1", "perm_2"), + "values" -> Json.arr(null, null) + ) + + assertJSONEquals(expectedRowJson1, rowJson1) + + val expectedRowJson2 = Json.obj( + "status" -> "ok", + "id" -> rowId, + "permissions" -> Json.arr(), + "values" -> Json.arr(null, null) + ) + + assertJSONEquals(expectedRowJson2, rowJson2) + } + } + } + + @Test + def createRowAndSetPermissionFlagsForTable(implicit c: TestContext): Unit = { + okTest { + for { + (tableId, _, _) <- createSimpleTableWithValues( + "table", + Seq(Identifier(TextCol("text"))), + Seq( + Seq("row 1"), + Seq("row 2"), + Seq("row 3") + ) + ) + + _ <- sendRequest( + "PATCH", + s"/tables/$tableId/rows/annotations", + Json.obj("permissions" -> Json.arr("perm_1", "perm_2")) + ) + + rowsAllFinal <- sendRequest("GET", s"/tables/$tableId/rows") + + _ <- sendRequest("PATCH", s"/tables/$tableId/rows/annotations", Json.obj("permissions" -> Json.arr())) + + rowsAllNotFinal <- sendRequest("GET", s"/tables/$tableId/rows") + } yield { + val rowsAreFinal = rowsAllFinal + .getJsonArray("rows", Json.emptyArr()) + .asScala + .toList + .map(_.asInstanceOf[JsonObject]) + .forall(_.getJsonArray("permissions").asScala.toList == List("perm_1", "perm_2")) + // .forall(permissions => permission.eq .getJsonArray("permissions").is) + + print(rowsAreFinal) + // assertTrue(rowsAreFinal) + + val rowsAreNotFinal = rowsAllNotFinal + .getJsonArray("rows", Json.emptyArr()) + .asScala + .toList + .map(_.asInstanceOf[JsonObject]) + .forall(_.getJsonArray("permissions").asScala.toList.isEmpty) + // .forall(!_.getBoolean("final")) + + // assertTrue(rowsAreNotFinal) + } + } + } +}