From 561905f7abd8a44bfd632015ea8bb47ffe6f9dd1 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 16 Mar 2020 13:55:25 +0100 Subject: [PATCH] Refactoring Citizen Tests --- src/test/kotlin/feature/CitizenSteps.kt | 55 ++++++++++ .../kotlin/feature/KtorServerAuthSteps.kt | 100 ------------------ src/test/resources/feature/citizen.feature | 33 +++--- 3 files changed, 75 insertions(+), 113 deletions(-) create mode 100644 src/test/kotlin/feature/CitizenSteps.kt diff --git a/src/test/kotlin/feature/CitizenSteps.kt b/src/test/kotlin/feature/CitizenSteps.kt new file mode 100644 index 0000000..727d1aa --- /dev/null +++ b/src/test/kotlin/feature/CitizenSteps.kt @@ -0,0 +1,55 @@ +package feature + +import fr.dcproject.entity.Citizen +import fr.dcproject.entity.CitizenI +import fr.dcproject.entity.User +import io.cucumber.datatable.DataTable +import io.cucumber.java8.En +import org.joda.time.DateTime +import org.koin.test.KoinTest +import org.koin.test.get +import java.util.* +import fr.dcproject.repository.Citizen as CitizenRepository + +class CitizenSteps : En, KoinTest { + init { + Given("I have citizen") { extraData: DataTable? -> + val params = extraData?.asMap(String::class.java, String::class.java) + createCitizen( + params?.get("firstName") ?: "firstName"+UUID.randomUUID(), + params?.get("lastName") ?: "lastName"+UUID.randomUUID(), + extraData + ) + } + + Given("I have citizen {word} {word}") { firstName: String, lastName: String -> + createCitizen(firstName, lastName) + } + + Given("I have citizen {word} {word} with ID {string}") { firstName: String, lastName: String, id: String -> + createCitizen(firstName, lastName, id = UUID.fromString(id)) + } + } + + private fun createCitizen (firstName: String, lastName: String, extraData: DataTable? = null, id: UUID? = null) { + + val params = extraData?.asMap(String::class.java, String::class.java) + val id: UUID = id ?: params?.get("id")?.let { UUID.fromString(it) } ?: UUID.randomUUID() + val email = params?.get("email") ?: ("$firstName-$lastName".toLowerCase()) + "@dc-project.fr" + + val user = User( + id = id, + username = "$firstName-$lastName".toLowerCase(), + plainPassword = "azerty" + ) + val citizen = Citizen( + id = id, + name = CitizenI.Name(firstName, lastName), + email = email, + birthday = DateTime.now(), + user = user + ) + + get().insertWithUser(citizen) + } +} \ No newline at end of file diff --git a/src/test/kotlin/feature/KtorServerAuthSteps.kt b/src/test/kotlin/feature/KtorServerAuthSteps.kt index 33ef0f4..0038a84 100644 --- a/src/test/kotlin/feature/KtorServerAuthSteps.kt +++ b/src/test/kotlin/feature/KtorServerAuthSteps.kt @@ -2,91 +2,14 @@ package feature import com.auth0.jwt.JWT import fr.dcproject.JwtConfig -import fr.dcproject.entity.Citizen -import fr.dcproject.entity.CitizenI -import fr.dcproject.entity.User -import fr.postgresjson.connexion.Requester -import io.cucumber.datatable.DataTable import io.cucumber.java8.En import io.ktor.http.HttpHeaders -import org.joda.time.DateTime import org.koin.test.KoinTest import org.koin.test.get -import org.koin.test.inject -import java.util.* -import java.util.concurrent.CompletionException import fr.dcproject.repository.Citizen as CitizenRepository class KtorServerAuthSteps : En, KoinTest { - private val requester: Requester by inject() init { - When("I have citizen:") { body: DataTable -> - val data = body.asMap(String::class.java, String::class.java) - val username = (data["firstName"] + "-" + data["lastName"]).toLowerCase() - val user = User( - username = username, - plainPassword = "azerty" - ) - val citizen = Citizen( - id = UUID.fromString(data["id"]), - name = CitizenI.Name(data["firstName"]!!, data["lastName"]!!), - email = data["email"] ?: "$username@dc-project.com", - birthday = DateTime.now(), - user = user - ) - - get().insertWithUser(citizen) - } - - 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) - .sign(JwtConfig.algorithm) - - val user = User( - id = UUID.fromString(id), - username = "$firstName-$lastName".toLowerCase(), - plainPassword = "azerty" - ) - val citizen = Citizen( - id = UUID.fromString(id), - name = CitizenI.Name(firstName, lastName), - email = ("$firstName-$lastName".toLowerCase()) + "@dc-project.fr", - 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}") { firstName: String, lastName: String -> - val id: UUID = UUID.randomUUID() - - val user = User( - id = id, - username = "$firstName-$lastName".toLowerCase(), - plainPassword = "azerty" - ) - val citizen = Citizen( - id = id, - name = CitizenI.Name(firstName, lastName), - email = ("$firstName-$lastName".toLowerCase()) + "@dc-project.fr", - birthday = DateTime.now(), - user = user - ) - - get().insertWithUser(citizen) - } - Given("I am authenticated as {word} {word}") { firstName: String, lastName: String -> val username = "$firstName-$lastName".toLowerCase() val citizen = get().findByUsername(username) ?: error("Cititzen not exist with username $username") @@ -99,28 +22,5 @@ class KtorServerAuthSteps : En, KoinTest { 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 = CitizenI.Name(firstName, lastName), - email = "$firstName-$lastName".toLowerCase() + "@gmail.com", - birthday = DateTime.now(), - user = user, - followAnonymous = false, - voteAnonymous = false - ) - - try { - get().insertWithUser(citizen) - } catch (e: CompletionException) { - // Nothing - } - } } } \ No newline at end of file diff --git a/src/test/resources/feature/citizen.feature b/src/test/resources/feature/citizen.feature index 33e71a2..45bf75d 100644 --- a/src/test/resources/feature/citizen.feature +++ b/src/test/resources/feature/citizen.feature @@ -1,31 +1,35 @@ +@citizen Feature: citizens routes Scenario: The route for get citizens must response a 200 - Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" + Given I have citizen Jean Perrin with ID "5267a5c6-af42-4a02-aa2b-6b71d2e43973" + And I am authenticated as Jean Perrin When I send a GET request to "/citizens" Then the response status code should be 200 Scenario: The route for get one citizen must response a 200 and return citizen - Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" - When I send a GET request to "/citizens/6434f4f9-f570-f22a-c134-8668350651ff" + Given I have citizen Linus Pauling with ID "47a05c0f-7329-46c3-a7d0-325db37e9114" + Given I am authenticated as Linus Pauling + When I send a GET request to "/citizens/47a05c0f-7329-46c3-a7d0-325db37e9114" Then the response status code should be 200 And the response should contain object: - | id | 6434f4f9-f570-f22a-c134-8668350651ff | + | id | 47a05c0f-7329-46c3-a7d0-325db37e9114 | Scenario: Can get connected citizen - Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" + Given I have citizen Henri Becquerel with ID "47356809-c8ef-4649-8b99-1c5cb9886d38" + Given I am authenticated as Henri Becquerel When I send a GET request to "/citizens/current" Then the response status code should be 200 And the response should contain object: - | id | 64b7b379-2298-43ec-b428-ba134930cabd | + | id | 47356809-c8ef-4649-8b99-1c5cb9886d38 | @online Scenario: Can be connect with SSO - Given I have citizen: + Given I have citizen | id | c606110c-ff0e-4d09-a79e-74632d7bf7bd | - | firstName | John | - | lastName | Doe | | email | fabrice.lecomte.be@gmail.com | + | firstName | Leonhard | + | lastName | Euler | When I send a POST request to "/sso" with body: """ { @@ -36,8 +40,9 @@ Feature: citizens routes Then the response status code should be 204 Scenario: Can be change my password - Given I am authenticated as Joe Patate with id "c211dca6-aa21-45c2-95ba-c7f2179ee37e" - When I send a PUT request to "/citizens/c211dca6-aa21-45c2-95ba-c7f2179ee37e/password/change" with body: + Given I have citizen Georges Charpak with ID "0c966522-4071-43e5-a3ca-cfff2557f2cf" + And I am authenticated as Georges Charpak + When I send a PUT request to "/citizens/0c966522-4071-43e5-a3ca-cfff2557f2cf/password/change" with body: """ { "old_password": "azerty", @@ -47,8 +52,10 @@ Feature: citizens routes Then the response status code should be 201 Scenario: If a send bad request when a change password, that return a 400 Bad request - Given I am authenticated as Joe Carotte with id "19110bb5-58a2-4ef1-9497-0207d4b4f48f" - When I send a PUT request to "/citizens/19110bb5-58a2-4ef1-9497-0207d4b4f48f/password/change" with body: + + Given I have citizen Louis Breguet with ID "6cf2a19d-d15d-4ee5-b2a9-907afd26b525" + And I am authenticated as Louis Breguet + When I send a PUT request to "/citizens/6cf2a19d-d15d-4ee5-b2a9-907afd26b525/password/change" with body: """ { "plup": "azerty",