From adba5a5aadc5c4a808424211cc6d99218ec92a2d Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 26 Aug 2019 17:11:38 +0200 Subject: [PATCH] Refactor cucumber steps --- .../kotlin/feature/KtorServerAuthSteps.kt | 47 ++++++++++++++++--- src/test/resources/feature/articles.feature | 6 +-- .../resources/feature/constitution.feature | 5 +- src/test/resources/feature/follow.feature | 30 +++--------- 4 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/test/kotlin/feature/KtorServerAuthSteps.kt b/src/test/kotlin/feature/KtorServerAuthSteps.kt index 04a5c76..35e13d4 100644 --- a/src/test/kotlin/feature/KtorServerAuthSteps.kt +++ b/src/test/kotlin/feature/KtorServerAuthSteps.kt @@ -14,8 +14,9 @@ import org.koin.test.KoinTest import org.koin.test.get import org.koin.test.inject import java.util.* +import java.util.concurrent.CompletionException import kotlin.random.Random -import fr.dcproject.repository.User as UserRepository +import fr.dcproject.repository.Citizen as CitizenRepository class KtorServerAuthSteps: En, KoinTest { private val requester: Requester by inject() @@ -38,19 +39,53 @@ class KtorServerAuthSteps: En, KoinTest { .selectOne(citizen) } - Given("I am authenticated as an user") { - val id = UUID.randomUUID() + Given("I am authenticated as {word} {word} with id {string}") { firstName: String, lastName: String, id: String -> val jwtAsString: String = JWT.create() .withIssuer("dc-project.fr") - .withClaim("id", id.toString()) + .withClaim("id", id) .sign(Algorithm.HMAC512(JwtConfig.secret)) - val user = User(id = id, username = "user", plainPassword = "azerty") - get().insert(user) + val user = User( + id = UUID.fromString(id), + username = "$firstName-$lastName".toLowerCase(), + plainPassword = "azerty" + ) + val citizen = Citizen( + id = UUID.fromString(id), + name = Citizen.Name(firstName, lastName), + birthday = DateTime.now(), + user = user + ) + + try { + get().insertWithUser(citizen) + } catch (e: CompletionException) { + // Nothing + } KtorServerContext.defaultServer.addPreRequestSetup { addHeader(HttpHeaders.Authorization, "Bearer $jwtAsString") } } + + Given("I have citizen {word} {word} with id {string}") { firstName: String, lastName: String, id: String -> + val user = User( + id = UUID.randomUUID(), + username = "$firstName-$lastName".toLowerCase(), + plainPassword = "azerty" + ) + val citizen = Citizen( + id = UUID.fromString(id), + name = Citizen.Name(firstName, lastName), + birthday = DateTime.now(), + user = user + ) + + try { + get().insertWithUser(citizen) + } catch (e: CompletionException) { + // Nothing + } + } } } \ No newline at end of file diff --git a/src/test/resources/feature/articles.feature b/src/test/resources/feature/articles.feature index 9980a7f..000e4e9 100644 --- a/src/test/resources/feature/articles.feature +++ b/src/test/resources/feature/articles.feature @@ -11,11 +11,7 @@ Feature: articles routes | id | 9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b | Scenario: The route for create article must response a 200 and return object - Given I have citizen: - | id | 64b7b379-2298-43ec-b428-ba134930cabd | - | firstName | Jaque | - | lastName | Dupuis | - And I am authenticated as an user + Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" When I send a POST request to "/articles" with body: """ { diff --git a/src/test/resources/feature/constitution.feature b/src/test/resources/feature/constitution.feature index a03313b..02bc72c 100644 --- a/src/test/resources/feature/constitution.feature +++ b/src/test/resources/feature/constitution.feature @@ -11,10 +11,7 @@ Feature: constitution routes | id | 0ca489a6-ef68-8bd5-2355-5793d4b3d66c | Scenario: The route for create constitution must response a 200 and return object - Given I have citizen: - | id | 64b7b379-2298-43ec-b428-ba134930cabd | - | firstName | Jaque | - | lastName | Dupuis | + Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" When I send a POST request to "/constitutions" with body: """ { diff --git a/src/test/resources/feature/follow.feature b/src/test/resources/feature/follow.feature index dd9eeb1..96aba76 100644 --- a/src/test/resources/feature/follow.feature +++ b/src/test/resources/feature/follow.feature @@ -2,18 +2,12 @@ Feature: follow Article and Constitution # Article Scenario: The route for follow article must response a 201 and return object - Given I have citizen: - | id | 64b7b379-2298-43ec-b428-ba134930cabd | - | firstName | Jaque | - | lastName | Dupuis | + Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" When I send a POST request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/follow" Then the response status code should be 201 Scenario: The route for get follows of articles must response a 200 and return objects - Given I have citizen: - | id | 64b7b379-2298-43ec-b428-ba134930cabd | - | firstName | Jaque | - | lastName | Dupuis | + Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" When I send a GET request to "/citizens/64b7b379-2298-43ec-b428-ba134930cabd/follows/articles" Then the response status code should be 200 And the response should contain object: @@ -21,27 +15,18 @@ Feature: follow Article and Constitution | limit | 50 | Scenario: The route for unfollow article must response a 204 - Given I have citizen: - | id | 64b7b379-2298-43ec-b428-ba134930cabd | - | firstName | Jaque | - | lastName | Dupuis | + Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" When I send a DELETE request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/follow" Then the response status code should be 204 # Constitution Scenario: The route for follow constitution must response a 201 and return object - Given I have citizen: - | id | 64b7b379-2298-43ec-b428-ba134930cabd | - | firstName | Jaque | - | lastName | Dupuis | + Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" When I send a POST request to "/constitutions/72aa1ee1-4963-eb44-c9e0-5ce6e0f18f00/follow" Then the response status code should be 201 Scenario: The route for get follows of constitutions must response a 200 and return objects - Given I have citizen: - | id | 64b7b379-2298-43ec-b428-ba134930cabd | - | firstName | Jaque | - | lastName | Dupuis | + Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" When I send a GET request to "/citizens/64b7b379-2298-43ec-b428-ba134930cabd/follows/constitutions" Then the response status code should be 200 And the response should contain object: @@ -49,9 +34,6 @@ Feature: follow Article and Constitution | limit | 50 | Scenario: The route for unfollow constitution must response a 204 - Given I have citizen: - | id | 64b7b379-2298-43ec-b428-ba134930cabd | - | firstName | Jaque | - | lastName | Dupuis | + Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" When I send a DELETE request to "/constitutions/72aa1ee1-4963-eb44-c9e0-5ce6e0f18f00/follow" Then the response status code should be 204