From d911109cd25dd49a413e7393b1b18d853248625d Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sun, 25 Aug 2019 01:19:35 +0200 Subject: [PATCH] Add test for bad request --- src/main/kotlin/fr/dcproject/Application.kt | 22 +++++++++++++++++---- src/test/resources/feature/auth.feature | 14 +++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/Application.kt b/src/main/kotlin/fr/dcproject/Application.kt index db14bf4..9c9e88f 100644 --- a/src/main/kotlin/fr/dcproject/Application.kt +++ b/src/main/kotlin/fr/dcproject/Application.kt @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.PropertyNamingStrategy import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.datatype.joda.JodaModule +import com.github.jasync.sql.db.postgresql.exceptions.GenericDatabaseException import fr.dcproject.entity.Article import fr.dcproject.entity.Citizen import fr.dcproject.entity.Constitution @@ -16,17 +17,17 @@ import fr.dcproject.security.voter.AuthorizationVoter import fr.dcproject.security.voter.CitizenVoter import fr.postgresjson.migration.Migrations import io.ktor.application.Application +import io.ktor.application.call import io.ktor.application.install import io.ktor.auth.Authentication import io.ktor.auth.authenticate import io.ktor.auth.jwt.jwt -import io.ktor.features.AutoHeadResponse -import io.ktor.features.CallLogging -import io.ktor.features.ContentNegotiation -import io.ktor.features.DataConversion +import io.ktor.features.* +import io.ktor.http.HttpStatusCode import io.ktor.jackson.jackson import io.ktor.locations.KtorExperimentalLocationsAPI import io.ktor.locations.Locations +import io.ktor.response.respond import io.ktor.routing.Routing import io.ktor.util.KtorExperimentalAPI import org.eclipse.jetty.util.log.Slf4jLog @@ -34,6 +35,7 @@ import org.koin.ktor.ext.Koin import org.koin.ktor.ext.get import org.slf4j.event.Level import java.util.* +import java.util.concurrent.CompletionException import fr.dcproject.repository.Article as RepositoryArticle import fr.dcproject.repository.Citizen as RepositoryCitizen import fr.dcproject.repository.Constitution as RepositoryConstitution @@ -153,6 +155,18 @@ fun Application.module() { } } + install(StatusPages) { + // TODO move to postgresJson lib + exception { e -> + val parent = e.cause?.cause + if (parent is GenericDatabaseException) { + call.respond(HttpStatusCode.BadRequest, parent.errorMessage.message!!) + } else { + throw e + } + } + } + // TODO move to postgresJson lib get().run() } diff --git a/src/test/resources/feature/auth.feature b/src/test/resources/feature/auth.feature index af05d57..a00a766 100644 --- a/src/test/resources/feature/auth.feature +++ b/src/test/resources/feature/auth.feature @@ -18,6 +18,20 @@ Feature: Auth routes eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9. """ + Scenario: The route for create citizen must response a 200 and return object + When I send a POST request to "/register" with body: + """ + { + "name": {"first_name":"George2", "last_name":"MICHEL2"}, + "birthday": "2001-01-01", + "user":{ + "username": "", + "plain_password": "" + } + } + """ + Then the response status code should be 400 + Scenario: The route for create citizen must response a 200 and return object When I send a POST request to "/login" with body: """