From 86b569123c3203eb18e859cc91d2f2046e5db10f Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 17 Mar 2020 12:13:14 +0100 Subject: [PATCH] #42 Add tests for OpinionChoiceVoter --- .../security/voter/OpinionChoiceVoter.kt | 10 +-- .../security/voter/OpinionChoiceVoterTest.kt | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 src/test/kotlin/fr/dcproject/security/voter/OpinionChoiceVoterTest.kt diff --git a/src/main/kotlin/fr/dcproject/security/voter/OpinionChoiceVoter.kt b/src/main/kotlin/fr/dcproject/security/voter/OpinionChoiceVoter.kt index 601aab8..6dcde00 100644 --- a/src/main/kotlin/fr/dcproject/security/voter/OpinionChoiceVoter.kt +++ b/src/main/kotlin/fr/dcproject/security/voter/OpinionChoiceVoter.kt @@ -10,7 +10,7 @@ class OpinionChoiceVoter : Voter { override fun supports(action: ActionI, call: ApplicationCall, subject: Any?): Boolean { 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 { @@ -18,14 +18,6 @@ class OpinionChoiceVoter : Voter { if (subject is OpinionChoice) { return Vote.GRANTED } - if (subject is List<*>) { - subject.forEach { - if (it !is OpinionChoice) { - return Vote.DENIED - } - } - return Vote.GRANTED - } return Vote.DENIED } diff --git a/src/test/kotlin/fr/dcproject/security/voter/OpinionChoiceVoterTest.kt b/src/test/kotlin/fr/dcproject/security/voter/OpinionChoiceVoterTest.kt new file mode 100644 index 0000000..573f3e8 --- /dev/null +++ b/src/test/kotlin/fr/dcproject/security/voter/OpinionChoiceVoterTest.kt @@ -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 { + 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 { + 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 { + every { user } returns tesla.user + }.let { + can(OpinionChoiceVoter.Action.VIEW, it, listOf(choice1)) `should be` true + } + } +} \ No newline at end of file