From f887b99536ca88bd31fb5f07a2843d4f3290e469 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 24 Aug 2019 02:03:29 +0200 Subject: [PATCH] feature #9: Create route for register --- src/main/kotlin/fr/dcproject/Application.kt | 2 +- .../kotlin/fr/dcproject/repository/Citizen.kt | 2 +- src/main/kotlin/fr/dcproject/routes/Auth.kt | 13 ++++++-- src/main/kotlin/fr/dcproject/routes/Paths.kt | 1 + .../kotlin/feature/KtorServerRestSteps.kt | 12 +++++++ src/test/resources/feature/auth.feature | 33 +++++++++++++++++++ 6 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/feature/auth.feature diff --git a/src/main/kotlin/fr/dcproject/Application.kt b/src/main/kotlin/fr/dcproject/Application.kt index 6a4017f..db14bf4 100644 --- a/src/main/kotlin/fr/dcproject/Application.kt +++ b/src/main/kotlin/fr/dcproject/Application.kt @@ -145,7 +145,7 @@ fun Application.module() { // trace { application.log.trace(it.buildText()) } authenticate(optional = true) { article(get()) - auth(get()) + auth(get(), get()) citizen(get()) constitution(get()) followArticle(get()) diff --git a/src/main/kotlin/fr/dcproject/repository/Citizen.kt b/src/main/kotlin/fr/dcproject/repository/Citizen.kt index d0123bd..0556ddc 100644 --- a/src/main/kotlin/fr/dcproject/repository/Citizen.kt +++ b/src/main/kotlin/fr/dcproject/repository/Citizen.kt @@ -40,7 +40,7 @@ class Citizen(override var requester: Requester) : RepositoryI { .selectOne("resource" to citizen) } - fun createWithUser(citizen: CitizenEntity): CitizenEntity? { + fun insertWithUser(citizen: CitizenEntity): CitizenEntity? { return requester .getFunction("insert_citizen_with_user") .selectOne("resource" to citizen) diff --git a/src/main/kotlin/fr/dcproject/routes/Auth.kt b/src/main/kotlin/fr/dcproject/routes/Auth.kt index 3dacd84..6b3d229 100644 --- a/src/main/kotlin/fr/dcproject/routes/Auth.kt +++ b/src/main/kotlin/fr/dcproject/routes/Auth.kt @@ -12,18 +12,27 @@ import io.ktor.request.receive import io.ktor.response.respondText import io.ktor.routing.Route import io.ktor.util.KtorExperimentalAPI +import fr.dcproject.entity.Citizen as CitizenEntity +import fr.dcproject.repository.Citizen as CitizenRepository import fr.dcproject.repository.User as UserRepository @KtorExperimentalLocationsAPI @KtorExperimentalAPI -fun Route.auth(repo: UserRepository) { +fun Route.auth(userRepo: UserRepository, citizenRepo: CitizenRepository) { post { try { val credentials = call.receive() - val user = repo.findByCredentials(credentials) ?: throw BadRequestException("Username not exist or password is wrong") + val user = userRepo.findByCredentials(credentials) ?: throw BadRequestException("Username not exist or password is wrong") call.respondText(JwtConfig.makeToken(user)) } catch (e: MismatchedInputException) { throw BadRequestException("You must be send name and password to the request") } } + + post { + val citizen = call.receive() + val created = citizenRepo.insertWithUser(citizen)?.user ?: throw BadRequestException("Bad request") + + call.respondText(JwtConfig.makeToken(created)) + } } diff --git a/src/main/kotlin/fr/dcproject/routes/Paths.kt b/src/main/kotlin/fr/dcproject/routes/Paths.kt index 316a008..1257452 100644 --- a/src/main/kotlin/fr/dcproject/routes/Paths.kt +++ b/src/main/kotlin/fr/dcproject/routes/Paths.kt @@ -8,6 +8,7 @@ import io.ktor.locations.Location @KtorExperimentalLocationsAPI object Paths { @Location("/login") class LoginRequest + @Location("/register") class RegisterRequest @Location("/articles") class ArticlesRequest(page: Int = 1, limit: Int = 50, val sort: String? = null, val direction: Direction? = null, val search: String? = null) { val page: Int = if (page < 1) 1 else page diff --git a/src/test/kotlin/feature/KtorServerRestSteps.kt b/src/test/kotlin/feature/KtorServerRestSteps.kt index 054034f..9c9191b 100644 --- a/src/test/kotlin/feature/KtorServerRestSteps.kt +++ b/src/test/kotlin/feature/KtorServerRestSteps.kt @@ -9,6 +9,7 @@ import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.parse import kotlin.test.assertEquals +import kotlin.test.assertTrue import kotlin.test.fail @ImplicitReflectionSerializer @@ -30,6 +31,14 @@ class KtorServerRestSteps : En { val jsonArray = responseJsonElement as? JsonArray ?: fail("The json response isn't array") assertEquals(count, jsonArray.size) } + + Then("the Response should be:") { body: String -> + assertEquals(body, response) + } + + Then("the Response should contain:") { body: String -> + assertTrue(response.contains(body)) + } } private fun findJsonElement(node: String): JsonElement { @@ -52,4 +61,7 @@ class KtorServerRestSteps : En { private val responseJsonElement: JsonElement get() = Json.parse(KtorServerContext.defaultServer.call?.response?.content ?: fail("The response isn't valid JSON")) + + private val response: String + get() = KtorServerContext.defaultServer.call?.response?.content ?: fail("The response isn't valid") } diff --git a/src/test/resources/feature/auth.feature b/src/test/resources/feature/auth.feature new file mode 100644 index 0000000..af05d57 --- /dev/null +++ b/src/test/resources/feature/auth.feature @@ -0,0 +1,33 @@ +Feature: Auth routes + + 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":"George", "last_name":"MICHEL"}, + "birthday": "2001-01-01", + "user":{ + "username": "george-junior", + "plain_password": "azerty" + } + } + """ + Then the response status code should be 200 + And the Response should contain: + """ + eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9. + """ + + Scenario: The route for create citizen must response a 200 and return object + When I send a POST request to "/login" with body: + """ + { + "name": "username1", + "password": "azerty" + } + """ + Then the response status code should be 200 + And the Response should contain: + """ + eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9. + """ \ No newline at end of file