#42 Add tests for CommentVoter

This commit is contained in:
2020-03-17 11:20:29 +01:00
parent 60d887c5cc
commit 559adb7e2a
2 changed files with 110 additions and 9 deletions

View File

@@ -13,7 +13,7 @@ class CommentVoter : Voter {
override fun supports(action: ActionI, call: ApplicationCall, subject: Any?): Boolean {
return (action is Action)
.and(subject is Comment<*>? || subject is List<*>)
.and(subject is Comment<*>?)
}
override fun vote(action: ActionI, call: ApplicationCall, subject: Any?): Vote {
@@ -27,14 +27,6 @@ class CommentVoter : Voter {
return if (subject.isDeleted()) Vote.DENIED
else Vote.GRANTED
}
if (subject is List<*>) {
subject.forEach {
if (it !is Comment<*> || it.isDeleted()) {
return Vote.DENIED
}
}
return Vote.GRANTED
}
return Vote.DENIED
}

View File

@@ -0,0 +1,109 @@
package fr.dcproject.security.voter
import fr.dcproject.entity.*
import io.ktor.application.ApplicationCall
import io.ktor.locations.KtorExperimentalLocationsAPI
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.Test
import org.junit.jupiter.api.TestInstance
@KtorExperimentalLocationsAPI
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class CommentVoterTest {
private val tesla = CitizenBasic(
user = User(
username = "nicolas-tesla",
roles = listOf(UserI.Roles.ROLE_USER)
),
birthday = DateTime.now(),
email = "tesla@best.com",
name = CitizenI.Name("Nicolas", "Tesla")
)
private val einstein = CitizenBasic(
user = User(
username = "albert-einstein",
roles = listOf(UserI.Roles.ROLE_USER)
),
birthday = DateTime.now(),
email = "einstein@best.com",
name = CitizenI.Name("Albert", "Einstein")
)
private val article1 = Article(
content = "Hi",
createdBy = einstein,
description = "blablabla",
title = "Super article"
)
private val comment1 = Comment(
content = "Hello",
createdBy = tesla,
target = article1
)
init {
mockkStatic("fr.dcproject.security.voter.VoterKt")
}
@Test
fun `support comment`() = CommentVoter().run {
val p = object : ActionI {}
mockk<ApplicationCall> {
every { user } returns tesla.user
}.let {
supports(CommentVoter.Action.VIEW, it, comment1) `should be` true
supports(CommentVoter.Action.VIEW, it, article1) `should be` false
supports(p, it, comment1) `should be` false
}
}
@Test
fun `can be view the comment`() = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { user } returns tesla.user
}.let {
can(CommentVoter.Action.VIEW, it, comment1) `should be` true
}
}
@Test
fun `can be view the comment list`() = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { user } returns einstein.user
}.let {
can(CommentVoter.Action.VIEW, it, listOf(comment1)) `should be` true
}
}
@Test
fun `can be update your comment`() = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { user } returns tesla.user
}.let {
can(CommentVoter.Action.UPDATE, it, comment1) `should be` true
}
}
@Test
fun `can not be update other comment`() = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { user } returns einstein.user
}.let {
can(CommentVoter.Action.UPDATE, it, comment1) `should be` false
}
}
@Test
fun `can not be delete your comment`() = listOf(CommentVoter()).run {
mockk<ApplicationCall> {
every { user } returns tesla.user
}.let {
can(CommentVoter.Action.DELETE, it, comment1) `should be` false
}
}
}