#42 Improve tests for ArticleVoter

This commit is contained in:
2020-03-17 11:57:49 +01:00
parent d8251f1dd2
commit 45dbdecdb2

View File

@@ -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<ApplicationCall> {
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<ApplicationCall> {
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<ApplicationCall> {
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<ApplicationCall> {
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<ApplicationCall> {
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<ApplicationCall> {
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<ApplicationCall> {
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<ApplicationCall> {
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<ApplicationCall> {
every { user } returns einstein.user
}.let {
can(ArticleVoter.Action.UPDATE, it, article) `should be` false
}
}