Test openapi schema of Register

Fix some routes
Improve Schema Validation
This commit is contained in:
2021-03-16 00:53:10 +01:00
parent 235de4e5ff
commit 0cf1aea9bf
15 changed files with 179 additions and 73 deletions

View File

@@ -0,0 +1,10 @@
package fr.dcproject.common
interface BitMaskI {
val bit: Long
infix operator fun contains(which: BitMaskI): Boolean = bit and which.bit == which.bit
infix operator fun plus(mask: BitMaskI): BitMaskI = BitMask(mask.bit and this.bit)
}
class BitMask(override val bit: Long) : BitMaskI

View File

@@ -11,10 +11,12 @@ import fr.dcproject.component.citizen.database.CitizenI
import fr.dcproject.component.citizen.database.CitizenRepository
import io.ktor.application.call
import io.ktor.features.BadRequestException
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
@@ -61,8 +63,17 @@ object Register {
post<RegisterRequest> {
try {
val citizen = call.receiveOrBadRequest<Input>().toCitizen()
val createdCitizen = citizenRepo.insertWithUser(citizen)?.user ?: throw BadRequestException("Bad request")
call.respondText(createdCitizen.makeToken())
citizenRepo.insertWithUser(citizen)?.user?.makeToken()?.let { token ->
if (call.request.accept() == ContentType.Application.Json.toString()) {
call.respond(
object {
val token: String = token
}
)
} else {
call.respondText(token)
}
} ?: throw BadRequestException("Bad request")
} catch (e: MissingKotlinParameterException) {
call.respond(HttpStatusCode.BadRequest)
}

View File

@@ -256,6 +256,82 @@ paths:
type: string
example:
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBdXRoZW50aWNhdGlvbiIsImlzcyI6ImRjLXByb2plY3QuZnIiLCJpZCI6ImQ1NDRhNmE4LWJhYjgtNDU2MC05NWIxLThhZjAyMDNkOTEwNCIsImV4cCI6MTU2NzA3Mzc0Mn0.0VTetv8fZFjVgpJ-bwJpidGNHJUOmgj8vuZcZXzwnLa7TtFwcXWvh3bDPYHqB66nmOfXyM57XnHDbmRwtipCag'
/register:
post:
summary: Create account
tags:
- authentification
operationId: register
requestBody:
content:
application/json:
schema:
type: object
required:
- name
- birthday
- email
- user
properties:
name:
type: object
required:
- firstName
- lastName
properties:
firstName:
type: string
example:
john
lastName:
type: string
example:
Doe
birthday:
type: string
format: 'date'
example: '1984-12-25'
email:
type: string
format: email
example: my.email@dc-project.fr
user:
type: object
required:
- username
- password
properties:
username:
type: string
example:
john-doe
password:
type: string
example:
azerty
format: password
responses:
200:
description: User created and JWT returned
content:
text/plain:
example:
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBdXRoZW50aWNhdGlvbiIsImlzcyI6ImRjLXByb2plY3QuZnIiLCJpZCI6ImQ1NDRhNmE4LWJhYjgtNDU2MC05NWIxLThhZjAyMDNkOTEwNCIsImV4cCI6MTU2NzA3Mzc0Mn0.0VTetv8fZFjVgpJ-bwJpidGNHJUOmgj8vuZcZXzwnLa7TtFwcXWvh3bDPYHqB66nmOfXyM57XnHDbmRwtipCag'
application/json:
schema:
properties:
token:
type: string
example:
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBdXRoZW50aWNhdGlvbiIsImlzcyI6ImRjLXByb2plY3QuZnIiLCJpZCI6ImQ1NDRhNmE4LWJhYjgtNDU2MC05NWIxLThhZjAyMDNkOTEwNCIsImV4cCI6MTU2NzA3Mzc0Mn0.0VTetv8fZFjVgpJ-bwJpidGNHJUOmgj8vuZcZXzwnLa7TtFwcXWvh3bDPYHqB66nmOfXyM57XnHDbmRwtipCag'
400:
description: Bad request
content:
application/json:
schema:
description: sdf
components:
parameters:
page: