From 45dbdecdb2cdf10c31baac3093f49a3a647bacee Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Tue, 17 Mar 2020 11:57:49 +0100 Subject: [PATCH] #42 Improve tests for ArticleVoter --- .../security/voter/ArticleVoterTest.kt | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/test/kotlin/fr/dcproject/security/voter/ArticleVoterTest.kt b/src/test/kotlin/fr/dcproject/security/voter/ArticleVoterTest.kt index 290e0ec..e651fa3 100644 --- a/src/test/kotlin/fr/dcproject/security/voter/ArticleVoterTest.kt +++ b/src/test/kotlin/fr/dcproject/security/voter/ArticleVoterTest.kt @@ -7,10 +7,12 @@ 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 @TestInstance(TestInstance.Lifecycle.PER_CLASS) +@Tag("voter") internal class ArticleVoterTest { val tesla = CitizenBasic( user = User( @@ -48,14 +50,13 @@ internal class ArticleVoterTest { } @Test - fun `other user can be view the article`() = ArticleVoter().run { + fun `other user can be view the article`() = listOf(ArticleVoter()).run { val article = getArticle(tesla) mockk { every { user } returns einstein.user }.let { - supports(ArticleVoter.Action.VIEW, it, article) `should be` true - vote(ArticleVoter.Action.VIEW, it, article) `should be` Vote.GRANTED + can(ArticleVoter.Action.VIEW, it, article) `should be` true } } @@ -72,14 +73,13 @@ internal class ArticleVoterTest { } @Test - fun `the no creator can not be view the article on draft`() = ArticleVoter().run { + fun `the no creator can not be view the article on draft`() = listOf(ArticleVoter()).run { val article = getArticle(tesla).apply { draft = true } mockk { every { user } returns einstein.user }.let { - supports(ArticleVoter.Action.VIEW, it, article) `should be` true - vote(ArticleVoter.Action.VIEW, it, article) `should be` Vote.DENIED + can(ArticleVoter.Action.VIEW, it, article) `should be` false } } @@ -96,62 +96,79 @@ internal class ArticleVoterTest { } @Test - fun `can not view deleted article`() = ArticleVoter().run { + fun `can not view deleted article`() = listOf(ArticleVoter()).run { val article = getArticle(tesla).apply { deletedAt = DateTime.now() } mockk { every { user } returns tesla.user }.let { - supports(ArticleVoter.Action.VIEW, it, article) `should be` true - vote(ArticleVoter.Action.VIEW, it, article) `should be` Vote.DENIED + can(ArticleVoter.Action.VIEW, it, article) `should be` false } } @Test - fun `can delete article if owner`() = ArticleVoter().run { + fun `can delete article if owner`() = listOf(ArticleVoter()).run { val article = getArticle(tesla) mockk { every { user } returns tesla.user }.let { - supports(ArticleVoter.Action.DELETE, it, article) `should be` true - vote(ArticleVoter.Action.DELETE, it, article) `should be` Vote.GRANTED + can(ArticleVoter.Action.DELETE, it, article) `should be` true } } @Test - fun `can not delete article if not owner`() = ArticleVoter().run { + fun `can not delete article if not owner`() = listOf(ArticleVoter()).run { val article = getArticle(tesla).apply { deletedAt = DateTime.now() } mockk { every { user } returns einstein.user }.let { - supports(ArticleVoter.Action.DELETE, it, article) `should be` true - vote(ArticleVoter.Action.DELETE, it, article) `should be` Vote.DENIED + can(ArticleVoter.Action.DELETE, it, article) `should be` false } } @Test - fun `can create article if logged`() = ArticleVoter().run { + fun `can create article if logged`() = listOf(ArticleVoter()).run { val article = getArticle(tesla) mockk { every { user } returns tesla.user }.let { - supports(ArticleVoter.Action.CREATE, it, article) `should be` true - vote(ArticleVoter.Action.CREATE, it, article) `should be` Vote.GRANTED + can(ArticleVoter.Action.CREATE, it, article) `should be` true } } @Test - fun `can not create article if not logged`() = ArticleVoter().run { + fun `can not create article if not logged`() = listOf(ArticleVoter()).run { val article = getArticle(tesla) mockk { every { user } returns null }.let { - supports(ArticleVoter.Action.CREATE, it, article) `should be` true - vote(ArticleVoter.Action.CREATE, it, article) `should be` Vote.DENIED + can(ArticleVoter.Action.CREATE, it, article) `should be` false + } + } + + @Test + fun `can update article if yours`() = listOf(ArticleVoter()).run { + val article = getArticle(tesla) + + mockk { + every { user } returns tesla.user + }.let { + can(ArticleVoter.Action.UPDATE, it, article) `should be` true + } + } + + @Test + fun `can not update article if not yours`() = listOf(ArticleVoter()).run { + val article = getArticle(tesla) + + mockk { + every { user } returns einstein.user + }.let { + can(ArticleVoter.Action.UPDATE, it, article) `should be` false } }