From 559adb7e2aada53381ac6336e5c7894a15678e79 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 17 Mar 2020 11:20:29 +0100 Subject: [PATCH] #42 Add tests for CommentVoter --- .../dcproject/security/voter/CommentVoter.kt | 10 +- .../security/voter/CommentVoterTest.kt | 109 ++++++++++++++++++ 2 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 src/test/kotlin/fr/dcproject/security/voter/CommentVoterTest.kt diff --git a/src/main/kotlin/fr/dcproject/security/voter/CommentVoter.kt b/src/main/kotlin/fr/dcproject/security/voter/CommentVoter.kt index b7760f6..dd129ea 100644 --- a/src/main/kotlin/fr/dcproject/security/voter/CommentVoter.kt +++ b/src/main/kotlin/fr/dcproject/security/voter/CommentVoter.kt @@ -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 } diff --git a/src/test/kotlin/fr/dcproject/security/voter/CommentVoterTest.kt b/src/test/kotlin/fr/dcproject/security/voter/CommentVoterTest.kt new file mode 100644 index 0000000..f3caabd --- /dev/null +++ b/src/test/kotlin/fr/dcproject/security/voter/CommentVoterTest.kt @@ -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 { + 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 { + 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 { + 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 { + 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 { + 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 { + every { user } returns tesla.user + }.let { + can(CommentVoter.Action.DELETE, it, comment1) `should be` false + } + } +} \ No newline at end of file