Skip to content

Commit

Permalink
add row permission test with more than one perm
Browse files Browse the repository at this point in the history
  • Loading branch information
zingmane committed Oct 6, 2023
1 parent 81e89e7 commit a1f25ff
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

0 comments on commit a1f25ff

Please sign in to comment.