From 7765e6d27a89d0d72f536ffcffdb6d2e8a89ffe9 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 17 Mar 2021 18:25:55 +0100 Subject: [PATCH] Test openapi schema of PUT /comments/{comment} --- .../article/routes/GetArticleComments.kt | 14 ++--- .../comment/generic/routes/EditComment.kt | 53 +++++++++++++++++-- .../comment/generic/routes/GetOneComment.kt | 14 ++--- src/main/resources/openapi2.yaml | 33 ++++++++++-- .../integration/Comment articles routes.kt | 4 +- 5 files changed, 99 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetArticleComments.kt b/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetArticleComments.kt index 7ea9a45..54c8908 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetArticleComments.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetArticleComments.kt @@ -74,12 +74,14 @@ object GetArticleComments { } } } - val votes: Any = object { - val up: Int = 0 - val neutral: Int = 0 - val down: Int = 0 - val total: Int = 0 - val score: Int = 0 + 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 + } } } } diff --git a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/EditComment.kt b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/EditComment.kt index a33778d..56932b4 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/EditComment.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/EditComment.kt @@ -1,6 +1,7 @@ package fr.dcproject.component.comment.generic.routes import fr.dcproject.common.security.assert +import fr.dcproject.common.utils.receiveOrBadRequest import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.comment.generic.CommentAccessControl 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.Location import io.ktor.locations.put -import io.ktor.request.receiveText import io.ktor.response.respond import io.ktor.routing.Route +import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -21,6 +22,7 @@ object EditComment { @Location("/comments/{comment}") class EditCommentRequest(comment: UUID) { val comment = CommentRef(comment) + class Input(val content: String) } 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") ac.assert { canUpdate(comment, citizenOrNull) } - comment.content = call.receiveText() + comment.content = call.receiveOrBadRequest().content 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 + } + } + } + ) } } } diff --git a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetOneComment.kt b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetOneComment.kt index 5e21c1b..278d33e 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetOneComment.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetOneComment.kt @@ -63,12 +63,14 @@ object GetOneComment { } } } - val votes: Any = object { - val up: Int = 0 - val neutral: Int = 0 - val down: Int = 0 - val total: Int = 0 - val score: Int = 0 + 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 + } } } ) diff --git a/src/main/resources/openapi2.yaml b/src/main/resources/openapi2.yaml index d49c75d..384d000 100644 --- a/src/main/resources/openapi2.yaml +++ b/src/main/resources/openapi2.yaml @@ -493,7 +493,7 @@ paths: - votes responses: 200: - description: Return Comment and children + description: Return paginated comments of article content: application/json: schema: @@ -526,7 +526,7 @@ paths: Lorem ipsum... responses: 201: - description: Return Comment and children + description: Return created Comment content: application/json: schema: @@ -542,11 +542,38 @@ paths: - comment responses: 200: - description: Return Comment and children + description: Return Comment content: application/json: schema: $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: parameters: page: diff --git a/src/test/kotlin/integration/Comment articles routes.kt b/src/test/kotlin/integration/Comment articles routes.kt index 6bc4d51..ba8e850 100644 --- a/src/test/kotlin/integration/Comment articles routes.kt +++ b/src/test/kotlin/integration/Comment articles routes.kt @@ -100,7 +100,9 @@ class `Comment articles routes` : BaseTest() { `When I send a PUT request`("/comments/fd30d20f-656c-42c6-8955-f61c04537464") { `authenticated as`("Hubert", "Reeves") `with body`(""" - Hello boy + { + "content": "Hello boy" + } """) } `Then the response should be` OK and { `And the response should not be null`()