#42 Add tests for OpinionChoiceVoter

This commit is contained in:
2020-03-17 12:13:14 +01:00
parent 1055e14039
commit 86b569123c
2 changed files with 76 additions and 9 deletions

View File

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

View File

@@ -0,0 +1,75 @@
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.Tag
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
@KtorExperimentalLocationsAPI
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tag("voter")
internal class OpinionChoiceVoterTest {
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"),
followAnonymous = false
)
private val article1 = Article(
content = "Hi",
createdBy = tesla,
description = "blablabla",
title = "Super article"
)
private val choice1 = OpinionChoice(
name = "Opinion1",
target = listOf()
)
init {
mockkStatic("fr.dcproject.security.voter.VoterKt")
}
@Test
fun `support opinion choice`() = OpinionChoiceVoter().run {
val p = object : ActionI {}
mockk<ApplicationCall> {
every { user } returns tesla.user
}.let {
supports(OpinionChoiceVoter.Action.VIEW, it, choice1) `should be` true
supports(OpinionChoiceVoter.Action.VIEW, it, article1) `should be` false
supports(p, it, choice1) `should be` false
}
}
@Test
fun `can be view the opinion choice`() = listOf(OpinionChoiceVoter()).run {
mockk<ApplicationCall> {
every { user } returns tesla.user
}.let {
can(OpinionChoiceVoter.Action.VIEW, it, choice1) `should be` true
}
}
@Test
fun `can be view the opinion choice list`() = listOf(OpinionChoiceVoter()).run {
mockk<ApplicationCall> {
every { user } returns tesla.user
}.let {
can(OpinionChoiceVoter.Action.VIEW, it, listOf(choice1)) `should be` true
}
}
}