#42 Add tests to ArticleVoter

Refactor ArticleVoter
This commit is contained in:
2020-03-16 03:46:31 +01:00
parent aa7ca26b51
commit ca78db4155
8 changed files with 271 additions and 87 deletions

View File

@@ -21,37 +21,57 @@ class ArticleTest {
@Language("JSON")
private val articleJson: String = """
{
"id" : "83b0b60a-5ab3-44f2-b243-1dc469a7564f",
"version_id" : "fff2311c-07cc-43a6-bab1-aec6b649a903",
"version_number" : null,
"title" : "Hello world!",
"anonymous" : true,
"content" : "bla bla bla",
"description" : "this is the changement !",
"tags" : [ ],
"created_by" : {
"id" : "3fff09e4-5ff2-46ee-9fd2-3803a1ffb600",
"name" : {
"first_name" : "Jaque",
"last_name" : "Bono",
"civility" : null
},
"birthday" : "2019-08-03T13:43:13.765Z",
"user_id" : null,
"vote_anonymous" : null,
"follow_anonymous" : null,
"user" : {
"id" : "151ec430-3aad-4792-9a14-e394b2be491b",
"username" : "jaque",
"blocked_at" : null,
"plain_password" : "azerty",
"created_at" : null,
"updated_at" : null
"id": "83b0b60a-5ab3-44f2-b243-1dc469a7564f",
"title": "Hello world!",
"anonymous": true,
"content": "bla bla bla",
"description": "this is the changement !",
"tags": [],
"draft": false,
"last_version": false,
"created_by": {
"id": "94a0d350-7eab-4a6e-9f84-0c2e7635b67c",
"name": {
"first_name": "Jaque",
"last_name": "Bono",
"civility": null
},
"email": "jaque.bono@gmail.com",
"created_at" : null
"birthday": "2020-03-16T01:48:27.020Z",
"vote_anonymous": true,
"follow_anonymous": true,
"user": {
"id": "2bc356a2-4d3e-46ff-91f4-ae30fb7fa67d",
"username": "jaque",
"blocked_at": null,
"plain_password": "azerty",
"roles": [],
"created_at": "2020-03-16T01:48:24.153Z",
"updated_at": "2020-03-16T01:48:24.516Z"
},
"deleted_at": null,
"deleted": false
},
"created_at" : null
"reference": "article",
"views": {
"total": 0,
"unique": 0,
"updated_at": "2020-03-16T01:48:31.070Z"
},
"version_id": "27cb4f5d-d425-4e10-95ca-6c50fac73408",
"version_number": null,
"created_at": "2020-03-16T01:48:31.004Z",
"deleted_at": null,
"deleted": false,
"votes": {
"up": 0,
"neutral": 0,
"down": 0,
"total": 0,
"score": 0,
"updated_at": null
},
"opinions": {}
}
""".trimIndent()

View File

@@ -0,0 +1,165 @@
package fr.dcproject.security.voter
import fr.dcproject.entity.*
import io.ktor.application.ApplicationCall
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
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class ArticleVoterTest {
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")
)
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")
)
init {
mockkStatic("fr.dcproject.security.voter.VoterKt")
}
@Test
fun `creator can be view the article`() = ArticleVoter().run {
val article = getArticle(tesla).apply { draft = true }
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.GRANTED
}
}
@Test
fun `other user can be view the article`() = ArticleVoter().run {
val article = getArticle(tesla)
val article2 = 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
}
}
@Test
fun `other user can be view the article list`() = listOf(ArticleVoter()).run {
val article = getArticle(tesla)
val article2 = getArticle(tesla)
mockk<ApplicationCall> {
every { user } returns einstein.user
}.let {
can(ArticleVoter.Action.VIEW, it, listOf(article, article2)) `should be` true
}
}
@Test
fun `the no creator can not be view the article on draft`() = 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
}
}
@Test
fun `the no creator can not be view list of articles if one is on draft`() = listOf(ArticleVoter()).run {
val article = getArticle(tesla)
val article2 = getArticle(tesla).apply { draft = true }
mockk<ApplicationCall> {
every { user } returns einstein.user
}.let {
can(ArticleVoter.Action.VIEW, it, listOf(article, article2)) `should be` false
}
}
@Test
fun `can not view deleted article`() = 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
}
}
@Test
fun `can delete article if owner`() = 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
}
}
@Test
fun `can not delete article if not owner`() = 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
}
}
@Test
fun `can create article if logged`() = 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
}
}
@Test
fun `can not create article if not logged`() = 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
}
}
private fun getArticle(createdBy: CitizenBasic = tesla) = Article(
title = "Hello world",
content = "Super",
description = "I Rocks",
createdBy = createdBy
)
}