Refactoring of OpinionVoter
This commit is contained in:
@@ -77,6 +77,6 @@ class OpinionSteps : En, KoinTest {
|
||||
} ?: error("You must provide the 'article' parameter"),
|
||||
createdBy = get<CitizenRepository>().findByUsername(username) ?: error("Citizen not exist")
|
||||
)
|
||||
get<OpinionRepository>().updateOpinions(opinion.choice, opinion.createdBy, opinion.target)
|
||||
get<OpinionRepository>().updateOpinions(opinion)
|
||||
}
|
||||
}
|
||||
@@ -10,18 +10,16 @@ import fr.dcproject.component.citizen.CitizenI
|
||||
import fr.dcproject.entity.Opinion
|
||||
import fr.dcproject.entity.OpinionChoice
|
||||
import fr.dcproject.security.voter.OpinionVoter
|
||||
import fr.dcproject.voter.NoSubjectDefinedException
|
||||
import fr.dcproject.voter.Vote.DENIED
|
||||
import fr.dcproject.voter.Vote.GRANTED
|
||||
import fr.ktorVoter.*
|
||||
import io.ktor.application.*
|
||||
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.Tag
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import org.junit.jupiter.api.parallel.Execution
|
||||
import org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT
|
||||
import java.util.*
|
||||
@@ -83,89 +81,51 @@ internal class OpinionVoterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `support opinion`(): Unit = OpinionVoter().run {
|
||||
val p = object : ActionI {}
|
||||
mockk<ApplicationCall> {
|
||||
every { user } returns tesla.user
|
||||
}.let {
|
||||
this(OpinionVoter.Action.VIEW, it, opinion1).vote `should be` Vote.GRANTED
|
||||
this(OpinionVoter.Action.VIEW, it, article1).vote `should be` Vote.GRANTED
|
||||
this(OpinionVoter.Action.VIEW, it, einstein).vote `should be` Vote.ABSTAIN
|
||||
this(p, it, opinion1).vote `should be` Vote.ABSTAIN
|
||||
}
|
||||
fun `can be view the opinion`() {
|
||||
OpinionVoter()
|
||||
.canView(opinion1, tesla)
|
||||
.vote `should be` GRANTED
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can be view the opinion`(): Unit = listOf(OpinionVoter()).run {
|
||||
mockk<ApplicationCall> {
|
||||
every { user } returns tesla.user
|
||||
}.let {
|
||||
can(OpinionVoter.Action.VIEW, it, opinion1) `should be` true
|
||||
}
|
||||
fun `can be view the opinion list`() {
|
||||
OpinionVoter()
|
||||
.canView(listOf(opinion1), tesla)
|
||||
.vote `should be` GRANTED
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can be not view the opinion if is null`(): Unit = listOf(OpinionVoter()).run {
|
||||
mockk<ApplicationCall> {
|
||||
every { user } returns tesla.user
|
||||
}.let {
|
||||
assertThrows<NoSubjectDefinedException> {
|
||||
assertCan(OpinionVoter.Action.VIEW, it, null)
|
||||
}
|
||||
}
|
||||
fun `can be opinion an article`() {
|
||||
OpinionVoter()
|
||||
.canCreate(opinion1, tesla)
|
||||
.vote `should be` GRANTED
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can be view the opinion list`(): Unit = listOf(OpinionVoter()).run {
|
||||
mockk<ApplicationCall> {
|
||||
every { user } returns tesla.user
|
||||
}.let {
|
||||
canAll(OpinionVoter.Action.VIEW, it, listOf(opinion1)) `should be` true
|
||||
}
|
||||
fun `can not be opinion if not connected`() {
|
||||
OpinionVoter()
|
||||
.canCreate(opinion1, null)
|
||||
.vote `should be` DENIED
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can be opinion an article`(): Unit = listOf(OpinionVoter()).run {
|
||||
mockk<ApplicationCall> {
|
||||
every { user } returns tesla.user
|
||||
}.let {
|
||||
can(OpinionVoter.Action.CREATE, it, opinion1) `should be` true
|
||||
}
|
||||
fun `can be remove opinion`() {
|
||||
OpinionVoter()
|
||||
.canDelete(opinion1, tesla)
|
||||
.vote `should be` GRANTED
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can not be opinion if not connected`() = listOf(OpinionVoter()).run {
|
||||
mockk<ApplicationCall> {
|
||||
every { user } returns null
|
||||
}.let {
|
||||
can(OpinionVoter.Action.CREATE, it, opinion1) `should be` false
|
||||
}
|
||||
fun `can not be remove opinion if not connected`() {
|
||||
OpinionVoter()
|
||||
.canDelete(opinion1, null)
|
||||
.vote `should be` DENIED
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can be remove opinion`(): Unit = listOf(OpinionVoter()).run {
|
||||
mockk<ApplicationCall> {
|
||||
every { user } returns tesla.user
|
||||
}.let {
|
||||
can(OpinionVoter.Action.DELETE, it, opinion1) `should be` true
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can not be remove opinion if not connected`(): Unit = listOf(OpinionVoter()).run {
|
||||
mockk<ApplicationCall> {
|
||||
every { user } returns null
|
||||
}.let {
|
||||
can(OpinionVoter.Action.DELETE, it, opinion1) `should be` false
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can not be remove opinion of other user`(): Unit = listOf(OpinionVoter()).run {
|
||||
mockk<ApplicationCall> {
|
||||
every { user } returns einstein.user
|
||||
}.let {
|
||||
can(OpinionVoter.Action.DELETE, it, opinion1) `should be` false
|
||||
}
|
||||
fun `can not be remove opinion of other user`() {
|
||||
OpinionVoter()
|
||||
.canDelete(opinion1, einstein)
|
||||
.vote `should be` DENIED
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,6 @@ Feature: Opinion
|
||||
| id | 9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b |
|
||||
| createdBy | Isaac Newton |
|
||||
And I have an opinion choice "Opinion4" with ID "0f4f1721-3136-44f1-9f31-1459f3317b15"
|
||||
And I have an opinion "Opinion4" on article "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" created by Isaac Newton with ID "74e93e12-556b-4399-95a6-04f93a4dd66c"
|
||||
When I send a PUT request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/opinions" with body:
|
||||
"""
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user