|
|
|
|
@@ -1,27 +1,19 @@
|
|
|
|
|
package unit.voter
|
|
|
|
|
|
|
|
|
|
import fr.dcproject.component.article.ArticleForView
|
|
|
|
|
import fr.dcproject.component.auth.User
|
|
|
|
|
import fr.dcproject.component.auth.UserI
|
|
|
|
|
import fr.dcproject.component.citizen.CitizenBasic
|
|
|
|
|
import fr.dcproject.component.citizen.CitizenCart
|
|
|
|
|
import fr.dcproject.component.citizen.CitizenI
|
|
|
|
|
import fr.dcproject.component.workgroup.WorkgroupRef
|
|
|
|
|
import fr.dcproject.component.workgroup.WorkgroupVoter
|
|
|
|
|
import fr.dcproject.component.workgroup.WorkgroupWithMembersI
|
|
|
|
|
import fr.dcproject.security.voter.WorkgroupVoter
|
|
|
|
|
import fr.dcproject.user
|
|
|
|
|
import fr.dcproject.voter.NoSubjectDefinedException
|
|
|
|
|
import fr.ktorVoter.ActionI
|
|
|
|
|
import fr.ktorVoter.Vote
|
|
|
|
|
import fr.ktorVoter.VoterException
|
|
|
|
|
import fr.ktorVoter.can
|
|
|
|
|
import io.ktor.application.*
|
|
|
|
|
import io.mockk.every
|
|
|
|
|
import io.mockk.mockk
|
|
|
|
|
import io.mockk.mockkStatic
|
|
|
|
|
import fr.dcproject.voter.Vote.DENIED
|
|
|
|
|
import fr.dcproject.voter.Vote.GRANTED
|
|
|
|
|
import org.amshove.kluent.`should be`
|
|
|
|
|
import org.joda.time.DateTime
|
|
|
|
|
import org.junit.jupiter.api.*
|
|
|
|
|
import org.junit.jupiter.api.Tag
|
|
|
|
|
import org.junit.jupiter.api.Test
|
|
|
|
|
import org.junit.jupiter.api.TestInstance
|
|
|
|
|
import org.junit.jupiter.api.parallel.Execution
|
|
|
|
|
import org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT
|
|
|
|
|
import java.util.*
|
|
|
|
|
@@ -63,13 +55,6 @@ internal class WorkgroupVoterTest {
|
|
|
|
|
name = CitizenI.Name("Albert", "Einstein")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
private val article1 = ArticleForView(
|
|
|
|
|
content = "Hi",
|
|
|
|
|
createdBy = einstein2,
|
|
|
|
|
description = "blablabla",
|
|
|
|
|
title = "Super article"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
private val workgroupPublic = WorkgroupEntity(
|
|
|
|
|
createdBy = tesla,
|
|
|
|
|
description = "Super desc",
|
|
|
|
|
@@ -86,146 +71,80 @@ internal class WorkgroupVoterTest {
|
|
|
|
|
anonymous = true
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
private val workgroupref = WorkgroupRef()
|
|
|
|
|
|
|
|
|
|
init {
|
|
|
|
|
mockkStatic("fr.dcproject.ApplicationContextKt")
|
|
|
|
|
@Test
|
|
|
|
|
fun `can be view your workgroup`() {
|
|
|
|
|
WorkgroupVoter()
|
|
|
|
|
.canView(workgroupPublic, tesla)
|
|
|
|
|
.vote `should be` GRANTED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `support workgroup`(): Unit = WorkgroupVoter().run {
|
|
|
|
|
val p = object : ActionI {}
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns tesla.user
|
|
|
|
|
}.let {
|
|
|
|
|
this(WorkgroupVoter.Action.VIEW, it, workgroupPublic).vote `should be` Vote.GRANTED
|
|
|
|
|
this(WorkgroupVoter.Action.VIEW, it, article1).vote `should be` Vote.ABSTAIN
|
|
|
|
|
this(p, it, workgroupPublic).vote `should be` Vote.ABSTAIN
|
|
|
|
|
}
|
|
|
|
|
fun `can be view your workgroup if is not public`() {
|
|
|
|
|
WorkgroupVoter()
|
|
|
|
|
.canView(workgroupAnon, tesla)
|
|
|
|
|
.vote `should be` GRANTED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can be view your workgroup`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns tesla.user
|
|
|
|
|
}.let {
|
|
|
|
|
can(WorkgroupVoter.Action.VIEW, it, workgroupPublic) `should be` true
|
|
|
|
|
}
|
|
|
|
|
fun `can be view workgroup of other if is public`() {
|
|
|
|
|
WorkgroupVoter()
|
|
|
|
|
.canView(workgroupPublic, einstein)
|
|
|
|
|
.vote `should be` GRANTED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can be view your workgroup if is not public`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns tesla.user
|
|
|
|
|
}.let {
|
|
|
|
|
can(WorkgroupVoter.Action.VIEW, it, workgroupAnon) `should be` true
|
|
|
|
|
}
|
|
|
|
|
fun `can not be view workgroup of other if is not public`() {
|
|
|
|
|
WorkgroupVoter()
|
|
|
|
|
.canView(workgroupAnon, einstein)
|
|
|
|
|
.vote `should be` DENIED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can be view workgroup of other if is public`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns einstein.user
|
|
|
|
|
}.let {
|
|
|
|
|
can(WorkgroupVoter.Action.VIEW, it, workgroupPublic) `should be` true
|
|
|
|
|
}
|
|
|
|
|
fun `can be view your workgroup list`() {
|
|
|
|
|
WorkgroupVoter()
|
|
|
|
|
.canView(listOf(workgroupPublic, workgroupAnon), tesla)
|
|
|
|
|
.vote `should be` GRANTED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can not be view workgroup of other if is not public`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns einstein.user
|
|
|
|
|
}.let {
|
|
|
|
|
can(WorkgroupVoter.Action.VIEW, it, workgroupAnon) `should be` false
|
|
|
|
|
}
|
|
|
|
|
fun `can be create workgroup`() {
|
|
|
|
|
WorkgroupVoter()
|
|
|
|
|
.canCreate(workgroupPublic, tesla)
|
|
|
|
|
.vote `should be` GRANTED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can be not view the workgroup if is null`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns tesla.user
|
|
|
|
|
}.let {
|
|
|
|
|
assertThrows<NoSubjectDefinedException> {
|
|
|
|
|
can(WorkgroupVoter.Action.VIEW, it, null)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fun `can not be create workgroup if not connected`() {
|
|
|
|
|
WorkgroupVoter()
|
|
|
|
|
.canCreate(workgroupPublic, null)
|
|
|
|
|
.vote `should be` DENIED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can be view your workgroup list`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns tesla.user
|
|
|
|
|
}.let {
|
|
|
|
|
listOf(workgroupPublic).map { workgroup ->
|
|
|
|
|
can(WorkgroupVoter.Action.VIEW, it, workgroup)
|
|
|
|
|
}.all { it } `should be` true
|
|
|
|
|
}
|
|
|
|
|
fun `can be delete workgroup if owner`() {
|
|
|
|
|
WorkgroupVoter()
|
|
|
|
|
.canDelete(workgroupPublic, tesla)
|
|
|
|
|
.vote `should be` GRANTED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can be create workgroup`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns tesla.user
|
|
|
|
|
}.let {
|
|
|
|
|
can(WorkgroupVoter.Action.CREATE, it, workgroupPublic) `should be` true
|
|
|
|
|
}
|
|
|
|
|
fun `can not be delete workgroup if not owner`() {
|
|
|
|
|
WorkgroupVoter()
|
|
|
|
|
.canDelete(workgroupPublic, einstein)
|
|
|
|
|
.vote `should be` DENIED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can not be create workgroup if not connected`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns null
|
|
|
|
|
}.let {
|
|
|
|
|
can(WorkgroupVoter.Action.CREATE, it, workgroupPublic) `should be` false
|
|
|
|
|
}
|
|
|
|
|
fun `can be update workgroup if owner`() {
|
|
|
|
|
WorkgroupVoter()
|
|
|
|
|
.canUpdate(workgroupPublic, tesla)
|
|
|
|
|
.vote `should be` GRANTED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can be delete workgroup if owner`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns tesla.user
|
|
|
|
|
}.let {
|
|
|
|
|
can(WorkgroupVoter.Action.DELETE, it, workgroupPublic) `should be` true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can not be delete workgroup if not owner`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns einstein.user
|
|
|
|
|
}.let {
|
|
|
|
|
can(WorkgroupVoter.Action.DELETE, it, workgroupPublic) `should be` false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can be update workgroup if owner`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns tesla.user
|
|
|
|
|
}.let {
|
|
|
|
|
can(WorkgroupVoter.Action.UPDATE, it, workgroupPublic) `should be` true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can not be update workgroup if not owner`(): Unit = listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns einstein.user
|
|
|
|
|
}.let {
|
|
|
|
|
can(WorkgroupVoter.Action.UPDATE, it, workgroupPublic) `should be` false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun `can not be update workgroup if workgroup has no user`() {
|
|
|
|
|
Assertions.assertThrows(VoterException::class.java) {
|
|
|
|
|
listOf(WorkgroupVoter()).run {
|
|
|
|
|
mockk<ApplicationCall> {
|
|
|
|
|
every { user } returns tesla.user
|
|
|
|
|
}.let {
|
|
|
|
|
can(WorkgroupVoter.Action.UPDATE, it, workgroupref)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fun `can not be update workgroup if not owner`() {
|
|
|
|
|
WorkgroupVoter()
|
|
|
|
|
.canUpdate(workgroupPublic, einstein)
|
|
|
|
|
.vote `should be` DENIED
|
|
|
|
|
}
|
|
|
|
|
}
|