#42 Improve tests for ArticleVoter
This commit is contained in:
@@ -7,10 +7,12 @@ import io.mockk.mockk
|
|||||||
import io.mockk.mockkStatic
|
import io.mockk.mockkStatic
|
||||||
import org.amshove.kluent.`should be`
|
import org.amshove.kluent.`should be`
|
||||||
import org.joda.time.DateTime
|
import org.joda.time.DateTime
|
||||||
|
import org.junit.jupiter.api.Tag
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
|
@Tag("voter")
|
||||||
internal class ArticleVoterTest {
|
internal class ArticleVoterTest {
|
||||||
val tesla = CitizenBasic(
|
val tesla = CitizenBasic(
|
||||||
user = User(
|
user = User(
|
||||||
@@ -48,14 +50,13 @@ internal class ArticleVoterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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)
|
val article = getArticle(tesla)
|
||||||
|
|
||||||
mockk<ApplicationCall> {
|
mockk<ApplicationCall> {
|
||||||
every { user } returns einstein.user
|
every { user } returns einstein.user
|
||||||
}.let {
|
}.let {
|
||||||
supports(ArticleVoter.Action.VIEW, it, article) `should be` true
|
can(ArticleVoter.Action.VIEW, it, article) `should be` true
|
||||||
vote(ArticleVoter.Action.VIEW, it, article) `should be` Vote.GRANTED
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,14 +73,13 @@ internal class ArticleVoterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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 }
|
val article = getArticle(tesla).apply { draft = true }
|
||||||
|
|
||||||
mockk<ApplicationCall> {
|
mockk<ApplicationCall> {
|
||||||
every { user } returns einstein.user
|
every { user } returns einstein.user
|
||||||
}.let {
|
}.let {
|
||||||
supports(ArticleVoter.Action.VIEW, it, article) `should be` true
|
can(ArticleVoter.Action.VIEW, it, article) `should be` false
|
||||||
vote(ArticleVoter.Action.VIEW, it, article) `should be` Vote.DENIED
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,62 +96,79 @@ internal class ArticleVoterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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() }
|
val article = getArticle(tesla).apply { deletedAt = DateTime.now() }
|
||||||
|
|
||||||
mockk<ApplicationCall> {
|
mockk<ApplicationCall> {
|
||||||
every { user } returns tesla.user
|
every { user } returns tesla.user
|
||||||
}.let {
|
}.let {
|
||||||
supports(ArticleVoter.Action.VIEW, it, article) `should be` true
|
can(ArticleVoter.Action.VIEW, it, article) `should be` false
|
||||||
vote(ArticleVoter.Action.VIEW, it, article) `should be` Vote.DENIED
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `can delete article if owner`() = ArticleVoter().run {
|
fun `can delete article if owner`() = listOf(ArticleVoter()).run {
|
||||||
val article = getArticle(tesla)
|
val article = getArticle(tesla)
|
||||||
|
|
||||||
mockk<ApplicationCall> {
|
mockk<ApplicationCall> {
|
||||||
every { user } returns tesla.user
|
every { user } returns tesla.user
|
||||||
}.let {
|
}.let {
|
||||||
supports(ArticleVoter.Action.DELETE, it, article) `should be` true
|
can(ArticleVoter.Action.DELETE, it, article) `should be` true
|
||||||
vote(ArticleVoter.Action.DELETE, it, article) `should be` Vote.GRANTED
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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() }
|
val article = getArticle(tesla).apply { deletedAt = DateTime.now() }
|
||||||
|
|
||||||
mockk<ApplicationCall> {
|
mockk<ApplicationCall> {
|
||||||
every { user } returns einstein.user
|
every { user } returns einstein.user
|
||||||
}.let {
|
}.let {
|
||||||
supports(ArticleVoter.Action.DELETE, it, article) `should be` true
|
can(ArticleVoter.Action.DELETE, it, article) `should be` false
|
||||||
vote(ArticleVoter.Action.DELETE, it, article) `should be` Vote.DENIED
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `can create article if logged`() = ArticleVoter().run {
|
fun `can create article if logged`() = listOf(ArticleVoter()).run {
|
||||||
val article = getArticle(tesla)
|
val article = getArticle(tesla)
|
||||||
|
|
||||||
mockk<ApplicationCall> {
|
mockk<ApplicationCall> {
|
||||||
every { user } returns tesla.user
|
every { user } returns tesla.user
|
||||||
}.let {
|
}.let {
|
||||||
supports(ArticleVoter.Action.CREATE, it, article) `should be` true
|
can(ArticleVoter.Action.CREATE, it, article) `should be` true
|
||||||
vote(ArticleVoter.Action.CREATE, it, article) `should be` Vote.GRANTED
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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)
|
val article = getArticle(tesla)
|
||||||
|
|
||||||
mockk<ApplicationCall> {
|
mockk<ApplicationCall> {
|
||||||
every { user } returns null
|
every { user } returns null
|
||||||
}.let {
|
}.let {
|
||||||
supports(ArticleVoter.Action.CREATE, it, article) `should be` true
|
can(ArticleVoter.Action.CREATE, it, article) `should be` false
|
||||||
vote(ArticleVoter.Action.CREATE, it, article) `should be` Vote.DENIED
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user