From 9862e112eb0e3ddaa8432e6450a3b677f4e343ab Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 22 Mar 2021 00:52:23 +0100 Subject: [PATCH] Test openapi schema of GET /constitutions/{constitution}/follows --- .../constitution/GetFollowConstitution.kt | 19 ++++- .../constitution/GetMyFollowsConstitution.kt | 20 +++++- src/main/resources/openapi2.yaml | 69 +++++++++++++++++++ .../Follow constitutions routes.kt | 2 +- 4 files changed, 107 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/component/follow/routes/constitution/GetFollowConstitution.kt b/src/main/kotlin/fr/dcproject/component/follow/routes/constitution/GetFollowConstitution.kt index 560dec4..a2d7568 100644 --- a/src/main/kotlin/fr/dcproject/component/follow/routes/constitution/GetFollowConstitution.kt +++ b/src/main/kotlin/fr/dcproject/component/follow/routes/constitution/GetFollowConstitution.kt @@ -1,5 +1,6 @@ package fr.dcproject.component.follow.routes.constitution +import fr.dcproject.common.response.toOutput import fr.dcproject.common.security.assert import fr.dcproject.component.auth.citizen import fr.dcproject.component.auth.citizenOrNull @@ -13,6 +14,7 @@ import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.response.respond import io.ktor.routing.Route +import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -26,7 +28,22 @@ object GetFollowConstitution { get { repo.findFollow(citizen, it.constitution)?.let { follow -> ac.assert { canView(follow, citizenOrNull) } - call.respond(follow) + call.respond( + HttpStatusCode.OK, + follow.let { f -> + object { + val id: UUID = f.id + val createdBy: Any = f.createdBy.toOutput() + val target: Any = f.target.let { t -> + object { + val id: UUID = t.id + val reference: String = f.target.reference + } + } + val createdAt: DateTime = f.createdAt + } + } + ) } ?: call.respond(HttpStatusCode.NotFound) } } diff --git a/src/main/kotlin/fr/dcproject/component/follow/routes/constitution/GetMyFollowsConstitution.kt b/src/main/kotlin/fr/dcproject/component/follow/routes/constitution/GetMyFollowsConstitution.kt index 3eb3f9f..bc398f2 100644 --- a/src/main/kotlin/fr/dcproject/component/follow/routes/constitution/GetMyFollowsConstitution.kt +++ b/src/main/kotlin/fr/dcproject/component/follow/routes/constitution/GetMyFollowsConstitution.kt @@ -1,16 +1,19 @@ package fr.dcproject.component.follow.routes.constitution +import fr.dcproject.common.response.toOutput import fr.dcproject.common.security.assert import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.citizen.database.CitizenRef import fr.dcproject.component.follow.FollowAccessControl import fr.dcproject.component.follow.database.FollowConstitutionRepository import io.ktor.application.call +import io.ktor.http.HttpStatusCode import io.ktor.locations.KtorExperimentalLocationsAPI import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.response.respond import io.ktor.routing.Route +import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -24,7 +27,22 @@ object GetMyFollowsConstitution { get { val follows = repo.findByCitizen(it.citizen) ac.assert { canView(follows.result, citizenOrNull) } - call.respond(follows) + call.respond( + HttpStatusCode.OK, + follows.toOutput { f -> + object { + val id: UUID = f.id + val createdBy: Any = f.createdBy.toOutput() + val target: Any = f.target.let { t -> + object { + val id: UUID = t.id + val reference: String = f.target.reference + } + } + val createdAt: DateTime = f.createdAt + } + } + ) } } } diff --git a/src/main/resources/openapi2.yaml b/src/main/resources/openapi2.yaml index 70c9a6a..83d6655 100644 --- a/src/main/resources/openapi2.yaml +++ b/src/main/resources/openapi2.yaml @@ -796,6 +796,75 @@ paths: items: $ref: '#/components/schemas/FollowResponse' + /constitutions/{constitution}/follows: + parameters: + - $ref: '#/components/parameters/constitution' + get: + security: + - JWTAuth: [ ] + summary: Return Follow or nothing if you not follow + tags: + - follow + - constitution + responses: + 200: + description: Return your follow + content: + application/json: + schema: + $ref: '#/components/schemas/FollowResponse' + 404: + description: You not follow this constitution + post: + security: + - JWTAuth: [ ] + summary: Follow one constitution + tags: + - follow + - constitution + responses: + 201: + description: Return only http status 201 on success + 401: + $ref: '#/components/responses/401' + delete: + security: + - JWTAuth: [ ] + summary: Unfollow one constitution + tags: + - follow + - constitution + responses: + 204: + description: Return only http status 204 on success + 401: + $ref: '#/components/responses/401' + /citizens/{citizen}/follows/constitutions: + parameters: + - $ref: '#/components/parameters/citizen' + get: + security: + - JWTAuth: [ ] + summary: Return Follow or nothing if you not follow + tags: + - follow + - constitution + - citizen + responses: + 200: + description: Return your follows + content: + application/json: + schema: + allOf: + - $ref: '#/components/schemas/Paginated' + - type: object + properties: + result: + type: array + items: + $ref: '#/components/schemas/FollowResponse' + components: parameters: page: diff --git a/src/test/kotlin/integration/Follow constitutions routes.kt b/src/test/kotlin/integration/Follow constitutions routes.kt index 375f2f6..fc20797 100644 --- a/src/test/kotlin/integration/Follow constitutions routes.kt +++ b/src/test/kotlin/integration/Follow constitutions routes.kt @@ -37,7 +37,7 @@ class `Follow constitutions routes` : BaseTest() { } @Test - fun `I can get follow constitution`() { + fun `I can get my follow constitution`() { withIntegrationApplication { `Given I have constitution`(id = "7f642078-8e74-47fc-9712-0c37531674a0") `Given I have citizen`("André-Marie", "Ampère", id = "877b45b0-302d-487d-8944-6d03ccdbd0f8") {