From 189aa8d5493c5f9516159b2749ef4930d41deeac Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 15 Mar 2021 02:02:26 +0100 Subject: [PATCH] Test openapi schema response of Login --- .../dcproject/component/auth/routes/Login.kt | 14 +++++- src/main/resources/openapi2.yaml | 48 +++++++++++++++++-- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/component/auth/routes/Login.kt b/src/main/kotlin/fr/dcproject/component/auth/routes/Login.kt index f4d29c0..6f0b10d 100644 --- a/src/main/kotlin/fr/dcproject/component/auth/routes/Login.kt +++ b/src/main/kotlin/fr/dcproject/component/auth/routes/Login.kt @@ -7,10 +7,12 @@ import fr.dcproject.component.auth.jwt.makeToken import fr.dcproject.component.auth.routes.Login.LoginRequest.Input import io.ktor.application.call import io.ktor.auth.UserPasswordCredential +import io.ktor.http.ContentType import io.ktor.http.HttpStatusCode import io.ktor.locations.KtorExperimentalLocationsAPI import io.ktor.locations.Location import io.ktor.locations.post +import io.ktor.request.accept import io.ktor.response.respond import io.ktor.response.respondText import io.ktor.routing.Route @@ -32,8 +34,16 @@ object Login { UserPasswordCredential(username, password) } - userRepo.findByCredentials(credentials)?.let { user -> - call.respondText(user.makeToken()) + userRepo.findByCredentials(credentials)?.makeToken()?.let { token -> + if (call.request.accept() == ContentType.Application.Json.toString()) { + call.respond( + object { + val token: String = token + } + ) + } else { + call.respondText(token) + } } ?: call.respond(HttpStatusCode.BadRequest, "Username not exist or password is wrong") } catch (e: MismatchedInputException) { call.respond(HttpStatusCode.BadRequest, "You must be send name and password to the request") diff --git a/src/main/resources/openapi2.yaml b/src/main/resources/openapi2.yaml index 855d010..1499980 100644 --- a/src/main/resources/openapi2.yaml +++ b/src/main/resources/openapi2.yaml @@ -2,7 +2,7 @@ openapi: 3.0.2 info: version: '' title: 'DC Project' - description: 'A free comunity program for create constitution' + description: 'A free community program for create constitution' paths: /articles: @@ -91,8 +91,6 @@ paths: - content - description - tags - - anonymous - - draft properties: title: type: string @@ -156,7 +154,6 @@ paths: application/json: schema: $ref: '#/components/schemas/ArticleResponse' - /articles/{article}/versions: parameters: - $ref: '#/components/parameters/article' @@ -216,6 +213,49 @@ paths: name: type: string + /login: + post: + summary: sign in + tags: + - authentification + operationId: login + requestBody: + description: Login + required: true + content: + application/json: + schema: + required: + - username + - password + properties: + username: + type: string + description: username + example: + john-doe + password: + type: string + description: Pasword + example: + azerty + responses: + 200: + description: return JWT + content: + text/plain: + schema: + type: string + format: byte + example: + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBdXRoZW50aWNhdGlvbiIsImlzcyI6ImRjLXByb2plY3QuZnIiLCJpZCI6ImQ1NDRhNmE4LWJhYjgtNDU2MC05NWIxLThhZjAyMDNkOTEwNCIsImV4cCI6MTU2NzA3Mzc0Mn0.0VTetv8fZFjVgpJ-bwJpidGNHJUOmgj8vuZcZXzwnLa7TtFwcXWvh3bDPYHqB66nmOfXyM57XnHDbmRwtipCag' + application/json: + schema: + properties: + token: + type: string + example: + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBdXRoZW50aWNhdGlvbiIsImlzcyI6ImRjLXByb2plY3QuZnIiLCJpZCI6ImQ1NDRhNmE4LWJhYjgtNDU2MC05NWIxLThhZjAyMDNkOTEwNCIsImV4cCI6MTU2NzA3Mzc0Mn0.0VTetv8fZFjVgpJ-bwJpidGNHJUOmgj8vuZcZXzwnLa7TtFwcXWvh3bDPYHqB66nmOfXyM57XnHDbmRwtipCag' components: parameters: page: