Test openapi schema response of Login

This commit is contained in:
2021-03-15 02:02:26 +01:00
parent 97b07fb424
commit 189aa8d549
2 changed files with 56 additions and 6 deletions

View File

@@ -7,10 +7,12 @@ import fr.dcproject.component.auth.jwt.makeToken
import fr.dcproject.component.auth.routes.Login.LoginRequest.Input import fr.dcproject.component.auth.routes.Login.LoginRequest.Input
import io.ktor.application.call import io.ktor.application.call
import io.ktor.auth.UserPasswordCredential import io.ktor.auth.UserPasswordCredential
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode 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.post import io.ktor.locations.post
import io.ktor.request.accept
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.response.respondText import io.ktor.response.respondText
import io.ktor.routing.Route import io.ktor.routing.Route
@@ -32,8 +34,16 @@ object Login {
UserPasswordCredential(username, password) UserPasswordCredential(username, password)
} }
userRepo.findByCredentials(credentials)?.let { user -> userRepo.findByCredentials(credentials)?.makeToken()?.let { token ->
call.respondText(user.makeToken()) 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") } ?: call.respond(HttpStatusCode.BadRequest, "Username not exist or password is wrong")
} catch (e: MismatchedInputException) { } catch (e: MismatchedInputException) {
call.respond(HttpStatusCode.BadRequest, "You must be send name and password to the request") call.respond(HttpStatusCode.BadRequest, "You must be send name and password to the request")

View File

@@ -2,7 +2,7 @@ openapi: 3.0.2
info: info:
version: '' version: ''
title: 'DC Project' title: 'DC Project'
description: 'A free comunity program for create constitution' description: 'A free community program for create constitution'
paths: paths:
/articles: /articles:
@@ -91,8 +91,6 @@ paths:
- content - content
- description - description
- tags - tags
- anonymous
- draft
properties: properties:
title: title:
type: string type: string
@@ -156,7 +154,6 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/ArticleResponse' $ref: '#/components/schemas/ArticleResponse'
/articles/{article}/versions: /articles/{article}/versions:
parameters: parameters:
- $ref: '#/components/parameters/article' - $ref: '#/components/parameters/article'
@@ -216,6 +213,49 @@ paths:
name: name:
type: string 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: components:
parameters: parameters:
page: page: