Refactoring of WorkgroupVoter

This commit is contained in:
2021-01-17 15:01:49 +01:00
parent ecda29abe5
commit c380ba47a5
17 changed files with 154 additions and 286 deletions

View File

@@ -11,7 +11,6 @@ import fr.dcproject.voter.Vote.GRANTED
import fr.postgresjson.connexion.Paginated
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import org.amshove.kluent.`should be`
import org.joda.time.DateTime
import org.junit.jupiter.api.Tag
@@ -48,10 +47,6 @@ internal class ArticleVoterTest {
}
}
init {
mockkStatic("fr.dcproject.ApplicationContextKt")
}
@Test
fun `creator can be view the article`() {
val article = getArticle(tesla).copy(draft = true)

View File

@@ -7,7 +7,6 @@ import fr.dcproject.component.citizen.CitizenI
import fr.dcproject.component.citizen.CitizenVoter
import fr.dcproject.voter.Vote.DENIED
import fr.dcproject.voter.Vote.GRANTED
import io.mockk.mockkStatic
import org.amshove.kluent.`should be`
import org.joda.time.DateTime
import org.junit.jupiter.api.Tag
@@ -50,10 +49,6 @@ internal class CitizenVoterTest {
deletedAt = DateTime.now()
)
init {
mockkStatic("fr.dcproject.ApplicationContextKt")
}
@Test
fun `can be view the citizen`() {
CitizenVoter()

View File

@@ -15,7 +15,6 @@ import fr.dcproject.voter.Vote.GRANTED
import fr.postgresjson.connexion.Paginated
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import org.amshove.kluent.`should be`
import org.joda.time.DateTime
import org.junit.jupiter.api.Tag
@@ -106,10 +105,6 @@ internal class CommentVoterTest {
every { findVersionsByVersionId(1, 1, any()) } returns Paginated(listOf(article1), 0, 1, 1)
}
init {
mockkStatic("fr.dcproject.ApplicationContextKt")
}
@Test
fun `can be view the comment`() {
CommentVoter()

View File

@@ -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
}
}