Test openapi schema of PUT /comments/{comment}

This commit is contained in:
2021-03-17 18:25:55 +01:00
parent ca402dbea7
commit 7765e6d27a
5 changed files with 99 additions and 19 deletions

View File

@@ -74,12 +74,14 @@ object GetArticleComments {
} }
} }
} }
val votes: Any = object { val votes: Any = comment.votes.let { v ->
val up: Int = 0 object {
val neutral: Int = 0 val up: Int = v.up
val down: Int = 0 val neutral: Int = v.neutral
val total: Int = 0 val down: Int = v.down
val score: Int = 0 val total: Int = v.total
val score: Int = v.score
}
} }
} }
} }

View File

@@ -1,6 +1,7 @@
package fr.dcproject.component.comment.generic.routes package fr.dcproject.component.comment.generic.routes
import fr.dcproject.common.security.assert import fr.dcproject.common.security.assert
import fr.dcproject.common.utils.receiveOrBadRequest
import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.auth.citizenOrNull
import fr.dcproject.component.comment.generic.CommentAccessControl import fr.dcproject.component.comment.generic.CommentAccessControl
import fr.dcproject.component.comment.generic.database.CommentRef import fr.dcproject.component.comment.generic.database.CommentRef
@@ -11,9 +12,9 @@ import io.ktor.http.HttpStatusCode
import io.ktor.locations.KtorExperimentalLocationsAPI import io.ktor.locations.KtorExperimentalLocationsAPI
import io.ktor.locations.Location import io.ktor.locations.Location
import io.ktor.locations.put import io.ktor.locations.put
import io.ktor.request.receiveText
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.Route import io.ktor.routing.Route
import org.joda.time.DateTime
import java.util.UUID import java.util.UUID
@KtorExperimentalLocationsAPI @KtorExperimentalLocationsAPI
@@ -21,6 +22,7 @@ object EditComment {
@Location("/comments/{comment}") @Location("/comments/{comment}")
class EditCommentRequest(comment: UUID) { class EditCommentRequest(comment: UUID) {
val comment = CommentRef(comment) val comment = CommentRef(comment)
class Input(val content: String)
} }
fun Route.editComment(repo: CommentRepository, ac: CommentAccessControl) { fun Route.editComment(repo: CommentRepository, ac: CommentAccessControl) {
@@ -28,10 +30,55 @@ object EditComment {
val comment = repo.findById(it.comment.id) ?: throw NotFoundException("Comment not found") val comment = repo.findById(it.comment.id) ?: throw NotFoundException("Comment not found")
ac.assert { canUpdate(comment, citizenOrNull) } ac.assert { canUpdate(comment, citizenOrNull) }
comment.content = call.receiveText() comment.content = call.receiveOrBadRequest<EditCommentRequest.Input>().content
repo.edit(comment) repo.edit(comment)
call.respond(HttpStatusCode.OK, comment) call.respond(
HttpStatusCode.OK,
object {
val id: UUID = comment.id
val content: String = comment.content
val childrenCount: Int = comment.childrenCount ?: 0
val createdAt: DateTime = comment.createdAt
val parent: Any? = comment.parent?.let { p ->
object {
val id: UUID = p.id
val reference: String = p.reference
}
}
val target: Any = comment.target.let { t ->
object {
val id: UUID = t.id
val reference: String = t.reference
}
}
val createdBy: Any = comment.createdBy.let { c ->
object {
val id: UUID = c.id
val name: Any = c.name.let { n ->
object {
val firstName: String = n.firstName
val lastName: String = n.lastName
}
}
val user: Any = c.user.let { u ->
object {
val username: String = u.username
}
}
}
}
val votes: Any = comment.votes.let { v ->
object {
val up: Int = v.up
val neutral: Int = v.neutral
val down: Int = v.down
val total: Int = v.total
val score: Int = v.score
}
}
}
)
} }
} }
} }

View File

@@ -63,12 +63,14 @@ object GetOneComment {
} }
} }
} }
val votes: Any = object { val votes: Any = comment.votes.let { v ->
val up: Int = 0 object {
val neutral: Int = 0 val up: Int = v.up
val down: Int = 0 val neutral: Int = v.neutral
val total: Int = 0 val down: Int = v.down
val score: Int = 0 val total: Int = v.total
val score: Int = v.score
}
} }
} }
) )

View File

@@ -493,7 +493,7 @@ paths:
- votes - votes
responses: responses:
200: 200:
description: Return Comment and children description: Return paginated comments of article
content: content:
application/json: application/json:
schema: schema:
@@ -526,7 +526,7 @@ paths:
Lorem ipsum... Lorem ipsum...
responses: responses:
201: 201:
description: Return Comment and children description: Return created Comment
content: content:
application/json: application/json:
schema: schema:
@@ -542,11 +542,38 @@ paths:
- comment - comment
responses: responses:
200: 200:
description: Return Comment and children description: Return Comment
content: content:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/CommentResponse' $ref: '#/components/schemas/CommentResponse'
put:
security:
- JWTAuth: []
summary: Edit existing comment
tags:
- comment
requestBody:
content:
application/json:
schema:
required:
- content
properties:
content:
type: string
example:
Lorem ipsum...
responses:
200:
description: Return updated comment
content:
application/json:
schema:
$ref: '#/components/schemas/CommentResponse'
401:
$ref: '#/components/responses/401'
components: components:
parameters: parameters:
page: page:

View File

@@ -100,7 +100,9 @@ class `Comment articles routes` : BaseTest() {
`When I send a PUT request`("/comments/fd30d20f-656c-42c6-8955-f61c04537464") { `When I send a PUT request`("/comments/fd30d20f-656c-42c6-8955-f61c04537464") {
`authenticated as`("Hubert", "Reeves") `authenticated as`("Hubert", "Reeves")
`with body`(""" `with body`("""
Hello boy {
"content": "Hello boy"
}
""") """)
} `Then the response should be` OK and { } `Then the response should be` OK and {
`And the response should not be null`() `And the response should not be null`()