Skip to content

Commit

Permalink
fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisDuarte1 committed Jul 12, 2023
1 parent 81faf12 commit b1ee70c
Showing 1 changed file with 52 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ import pt.up.fe.ni.website.backend.utils.documentation.utils.MockMVCExtension.Co
import pt.up.fe.ni.website.backend.utils.documentation.utils.MockMVCExtension.Companion.andDocumentErrorResponse
import pt.up.fe.ni.website.backend.utils.documentation.utils.ModelDocumentation

class AccountRoleDocumentation : ModelDocumentation(
Tag.ROLES.name.lowercase() + "-accounts",
Tag.ROLES,
mutableListOf(
DocumentedJSONField("[]", "Array that only contains ONE account id", JsonFieldType.ARRAY)
)

)

@ControllerTest
@Transactional
Expand Down Expand Up @@ -87,7 +79,7 @@ internal class RoleControllerTest @Autowired constructor(
false
)

val testProject = Project(
val project = Project(
"UNI",
"Melhor app"
)
Expand Down Expand Up @@ -174,6 +166,7 @@ internal class RoleControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("POST /roles/{id}")
inner class CreateNewRole {

@BeforeEach
Expand Down Expand Up @@ -249,6 +242,7 @@ internal class RoleControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("DELETE /roles/{id}")
inner class DeleteRole {
lateinit var generation1: Generation
lateinit var role: Role
Expand Down Expand Up @@ -294,6 +288,7 @@ internal class RoleControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("POST /roles/{id}/permissions")
inner class GrantPermissionToRole {

@BeforeEach
Expand Down Expand Up @@ -343,6 +338,7 @@ internal class RoleControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("DELETE /roles/{id}/permissions")
inner class RevokePermissionFromRole {

@BeforeEach
Expand Down Expand Up @@ -392,6 +388,7 @@ internal class RoleControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("POST /roles/{id}/users")
inner class AddUserToRole {
@BeforeEach
fun addRoleAndUser() {
Expand Down Expand Up @@ -453,6 +450,7 @@ internal class RoleControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("DELETE /roles/{id}/users")
inner class RemoveUserFromRole {
@BeforeEach
fun addRoleAndUser() {
Expand Down Expand Up @@ -516,23 +514,27 @@ internal class RoleControllerTest @Autowired constructor(
.content(objectMapper.writeValueAsString(mapOf("userId" to testAccount.id)))
).andExpectAll(
status().isNotFound()
).andDocumentErrorResponse(documentationAccount, hasRequestPayload = true)
).andDocumentErrorResponse(
documentationAccount,
hasRequestPayload = true
)
assert(roleRepository.findByIdOrNull(testRole.id!!)!!.accounts.size != 0)
}
}

@NestedTest
@DisplayName("POST /roles/{id}/activities/{activityId}/permissions")
inner class AddPermissionToRoleActivity {
@BeforeEach
fun addAll() {
roleRepository.save(testRole)
projectRepository.save(testProject)
projectRepository.save(project)
}

@Test
fun `should add permission to role activity and create PerActivityRole`() {
mockMvc.perform(
post("/roles/${testRole.id}/activities/${testProject.id}/permissions")
post("/roles/${testRole.id}/activities/${project.id}/permissions")
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
Expand All @@ -543,7 +545,7 @@ internal class RoleControllerTest @Autowired constructor(
status().isOk
).andDocumentEmptyObjectResponse(
documentationPermissions,
"Adds an permission to an role activity",
"Adds an permission to a role activity",
"It will create a PerRoleActivity if it doesn't exist"
)
assert(roleRepository.findByIdOrNull(testRole.id!!)!!.associatedActivities.size == 1)
Expand All @@ -557,7 +559,7 @@ internal class RoleControllerTest @Autowired constructor(
@Test
fun `shouldn't add permission to role activity if roleId is invalid`() {
mockMvc.perform(
post("/roles/1234/activities/${testProject.id}/permissions")
post("/roles/1234/activities/${project.id}/permissions")
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
Expand All @@ -566,7 +568,10 @@ internal class RoleControllerTest @Autowired constructor(
)
).andExpectAll(
status().isNotFound()
).andDocumentErrorResponse(documentationPermissions, hasRequestPayload = true)
).andDocumentErrorResponse(
documentationPermissions,
hasRequestPayload = true
)
assert(roleRepository.findByIdOrNull(testRole.id!!)!!.associatedActivities.size == 0)
}

Expand All @@ -582,7 +587,10 @@ internal class RoleControllerTest @Autowired constructor(
)
).andExpectAll(
status().isNotFound()
).andDocumentErrorResponse(documentationPermissions, hasRequestPayload = true)
).andDocumentErrorResponse(
documentationPermissions,
hasRequestPayload = true
)
assert(roleRepository.findByIdOrNull(testRole.id!!)!!.associatedActivities.size == 0)
}

Expand All @@ -605,29 +613,28 @@ internal class RoleControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("DELETE /roles/{id}/activities/{activityId}/permissions")
inner class RemovePermissionFromPerRoleActivity {

private lateinit var project: Project

@BeforeEach
fun addAll() {
project = Project("test project", "test")
roleRepository.save(testRole)
projectRepository.save(testProject)
projectRepository.save(project)
val perActivityRole = PerActivityRole(Permissions(listOf(Permission.EDIT_ACTIVITY)))
perActivityRole.activity = testProject
perActivityRole.activity = project
perActivityRole.role = testRole
testProject.associatedRoles.add(perActivityRole)
projectRepository.save(testProject)
project.associatedRoles.add(perActivityRole)
projectRepository.save(project)
roleRepository.save(testRole)
}

@AfterEach
fun removeAll() {
testProject.associatedRoles.removeAt(0)
projectRepository.save(testProject)
}

@Test
fun `should remove an existing role activity permission`() {
mockMvc.perform(
delete("/roles/${testRole.id}/activities/${testProject.id}/permissions")
delete("/roles/${testRole.id}/activities/${project.id}/permissions")
.contentType(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
Expand All @@ -643,9 +650,9 @@ internal class RoleControllerTest @Autowired constructor(
"It will not create a PerRoleActivity if it doesn't exist, it will simply return, " +
"doesn't check if the permissions are already revoked..."
)
assert(projectRepository.findByIdOrNull(testProject.id!!)!!.associatedRoles.size == 1)
assert(projectRepository.findByIdOrNull(project.id!!)!!.associatedRoles.size == 1)
assert(
!projectRepository.findByIdOrNull(testProject.id!!)!!.associatedRoles[0].permissions.contains(
!projectRepository.findByIdOrNull(project.id!!)!!.associatedRoles[0].permissions.contains(
Permission.EDIT_ACTIVITY
)
)
Expand All @@ -654,7 +661,7 @@ internal class RoleControllerTest @Autowired constructor(
@Test
fun `should not remove an existing role activity permission on a non existing role`() {
mockMvc.perform(
delete("/roles/1234/activities/${testProject.id}/permissions")
delete("/roles/1234/activities/${project.id}/permissions")
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
Expand All @@ -664,10 +671,13 @@ internal class RoleControllerTest @Autowired constructor(

).andExpectAll(
status().isNotFound()
).andDocumentErrorResponse(documentationPermissions, hasRequestPayload = false)
assert(projectRepository.findByIdOrNull(testProject.id!!)!!.associatedRoles.size == 1)
).andDocumentErrorResponse(
documentationPermissions,
hasRequestPayload = false
)
assert(projectRepository.findByIdOrNull(project.id!!)!!.associatedRoles.size == 1)
assert(
projectRepository.findByIdOrNull(testProject.id!!)!!.associatedRoles[0].permissions.contains(
projectRepository.findByIdOrNull(project.id!!)!!.associatedRoles[0].permissions.contains(
Permission.EDIT_ACTIVITY
)
)
Expand All @@ -687,16 +697,16 @@ internal class RoleControllerTest @Autowired constructor(
).andExpectAll(
status().isNotFound()
).andDocumentErrorResponse(documentationPermissions, hasRequestPayload = false)
assert(projectRepository.findByIdOrNull(testProject.id!!)!!.associatedRoles.size == 1)
assert(projectRepository.findByIdOrNull(project.id!!)!!.associatedRoles.size == 1)
assert(
projectRepository.findByIdOrNull(testProject.id!!)!!.associatedRoles[0].permissions.contains(
projectRepository.findByIdOrNull(project.id!!)!!.associatedRoles[0].permissions.contains(
Permission.EDIT_ACTIVITY
)
)
}

@Test
fun `should not remove an existing role activity perm on a non existing activity and non existing role`() {
fun `should not remove an existing role permission when neither the activity and role don't exist`() {
mockMvc.perform(
delete("/roles/1234/activities/1234/permissions")
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -708,10 +718,13 @@ internal class RoleControllerTest @Autowired constructor(

).andExpectAll(
status().isNotFound()
).andDocumentErrorResponse(documentationPermissions, hasRequestPayload = false)
assert(projectRepository.findByIdOrNull(testProject.id!!)!!.associatedRoles.size == 1)
).andDocumentErrorResponse(
documentationPermissions,
hasRequestPayload = false
)
assert(projectRepository.findByIdOrNull(project.id!!)!!.associatedRoles.size == 1)
assert(
projectRepository.findByIdOrNull(testProject.id!!)!!.associatedRoles[0].permissions.contains(
projectRepository.findByIdOrNull(project.id!!)!!.associatedRoles[0].permissions.contains(
Permission.EDIT_ACTIVITY
)
)
Expand Down

0 comments on commit b1ee70c

Please sign in to comment.