Refactoring of CommentVoter

This commit is contained in:
2021-01-14 23:38:59 +01:00
parent caadc2a969
commit 7c106f7cf8
13 changed files with 169 additions and 192 deletions

View File

@@ -1,6 +1,5 @@
package fr.dcproject.security.voter
import fr.dcproject.citizenOrNull
import fr.dcproject.component.article.ArticleForView
import fr.dcproject.component.article.ArticleRef
import fr.dcproject.component.citizen.Citizen
@@ -11,10 +10,9 @@ import fr.dcproject.component.comment.generic.CommentForView
import fr.dcproject.component.comment.generic.CommentVoter
import fr.dcproject.entity.User
import fr.dcproject.entity.UserI
import fr.dcproject.voter.NoSubjectDefinedException
import fr.ktorVoter.*
import fr.dcproject.voter.Vote.DENIED
import fr.dcproject.voter.Vote.GRANTED
import fr.postgresjson.connexion.Paginated
import io.ktor.application.*
import io.ktor.locations.*
import io.mockk.every
import io.mockk.mockk
@@ -24,7 +22,6 @@ import org.joda.time.DateTime
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.assertThrows
import java.util.*
import fr.dcproject.component.article.ArticleRepository as ArticleRepo
@@ -112,109 +109,59 @@ internal class CommentVoterTest {
mockkStatic("fr.dcproject.ApplicationContextKt")
}
@Test
fun `support comment`(): Unit = CommentVoter().run {
val p = object : ActionI {}
mockk<ApplicationCall> {
every { citizenOrNull } returns tesla
}.let {
this(CommentVoter.Action.VIEW, it, comment1).vote `should be` Vote.GRANTED
this(CommentVoter.Action.VIEW, it, article1).vote `should be` Vote.ABSTAIN
this(p, it, comment1).vote `should be` Vote.ABSTAIN
}
}
@Test
fun `can be view the comment`() {
listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { citizenOrNull } returns tesla
}.let {
can(CommentVoter.Action.VIEW, it, comment1) `should be` true
}
}
CommentVoter()
.canView(comment1, tesla)
.vote `should be` GRANTED
}
@Test
fun `can be view the comment list`(): Unit = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { citizenOrNull } returns einstein
}.let {
canAll(CommentVoter.Action.VIEW, it, listOf(comment1)) `should be` true
}
fun `can be view the comment list`() {
CommentVoter()
.canView(listOf(comment1, comment2), einstein)
.vote `should be` GRANTED
}
@Test
fun `can be update your comment`(): Unit = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { citizenOrNull } returns tesla
}.let {
can(CommentVoter.Action.UPDATE, it, comment1) `should be` true
}
fun `can be update your comment`() {
CommentVoter()
.canUpdate(comment1, tesla)
.vote `should be` GRANTED
}
@Test
fun `can not be update other comment`(): Unit = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { citizenOrNull } returns einstein
}.let {
can(CommentVoter.Action.UPDATE, it, comment1) `should be` false
}
fun `can not be update other comment`() {
CommentVoter()
.canUpdate(comment1, einstein)
.vote `should be` DENIED
}
@Test
fun `can not be delete your comment`(): Unit = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { citizenOrNull } returns tesla
}.let {
can(CommentVoter.Action.DELETE, it, comment1) `should be` false
}
fun `can be create a comment`() {
CommentVoter()
.canCreate(comment1, tesla)
.vote `should be` GRANTED
}
@Test
fun `can be create a comment`(): Unit = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { citizenOrNull } returns tesla
}.let {
can(CommentVoter.Action.CREATE, it, comment1) `should be` true
}
fun `can not be create a comment if target is deleted`() {
CommentVoter()
.canCreate(commentTargetDeleted, tesla)
.vote `should be` DENIED
}
@Test
fun `can not be create a comment if target is deleted`(): Unit = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { citizenOrNull } returns tesla
}.let {
can(CommentVoter.Action.CREATE, it, commentTargetDeleted) `should be` false
}
fun `can not be create a comment with other creator`() {
CommentVoter()
.canCreate(comment1, einstein)
.vote `should be` DENIED
}
@Test
fun `can not be create a comment with other creator`(): Unit = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { citizenOrNull } returns einstein
}.let {
can(CommentVoter.Action.CREATE, it, comment1) `should be` false
}
}
@Test
fun `can not be create a comment if is null`(): Unit = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { citizenOrNull } returns einstein
}.let {
assertThrows<NoSubjectDefinedException> {
assertCan(CommentVoter.Action.CREATE, it, null)
}
}
}
@Test
fun `can not be create a comment if not connected`(): Unit = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { citizenOrNull } returns null
}.let {
can(CommentVoter.Action.CREATE, it, comment1) `should be` false
}
fun `can not be create a comment if not connected`() {
CommentVoter()
.canCreate(comment1, null)
.vote `should be` DENIED
}
}