Refactoring Citizen Tests
This commit is contained in:
55
src/test/kotlin/feature/CitizenSteps.kt
Normal file
55
src/test/kotlin/feature/CitizenSteps.kt
Normal file
@@ -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, String>(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, String>(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<CitizenRepository>().insertWithUser(citizen)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,91 +2,14 @@ package feature
|
|||||||
|
|
||||||
import com.auth0.jwt.JWT
|
import com.auth0.jwt.JWT
|
||||||
import fr.dcproject.JwtConfig
|
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.cucumber.java8.En
|
||||||
import io.ktor.http.HttpHeaders
|
import io.ktor.http.HttpHeaders
|
||||||
import org.joda.time.DateTime
|
|
||||||
import org.koin.test.KoinTest
|
import org.koin.test.KoinTest
|
||||||
import org.koin.test.get
|
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
|
import fr.dcproject.repository.Citizen as CitizenRepository
|
||||||
|
|
||||||
class KtorServerAuthSteps : En, KoinTest {
|
class KtorServerAuthSteps : En, KoinTest {
|
||||||
private val requester: Requester by inject()
|
|
||||||
init {
|
init {
|
||||||
When("I have citizen:") { body: DataTable ->
|
|
||||||
val data = body.asMap<String, String>(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<CitizenRepository>().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<CitizenRepository>().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<CitizenRepository>().insertWithUser(citizen)
|
|
||||||
}
|
|
||||||
|
|
||||||
Given("I am authenticated as {word} {word}") { firstName: String, lastName: String ->
|
Given("I am authenticated as {word} {word}") { firstName: String, lastName: String ->
|
||||||
val username = "$firstName-$lastName".toLowerCase()
|
val username = "$firstName-$lastName".toLowerCase()
|
||||||
val citizen = get<CitizenRepository>().findByUsername(username) ?: error("Cititzen not exist with username $username")
|
val citizen = get<CitizenRepository>().findByUsername(username) ?: error("Cititzen not exist with username $username")
|
||||||
@@ -99,28 +22,5 @@ class KtorServerAuthSteps : En, KoinTest {
|
|||||||
addHeader(HttpHeaders.Authorization, "Bearer $jwtAsString")
|
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<CitizenRepository>().insertWithUser(citizen)
|
|
||||||
} catch (e: CompletionException) {
|
|
||||||
// Nothing
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,31 +1,35 @@
|
|||||||
|
@citizen
|
||||||
Feature: citizens routes
|
Feature: citizens routes
|
||||||
|
|
||||||
Scenario: The route for get citizens must response a 200
|
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"
|
When I send a GET request to "/citizens"
|
||||||
Then the response status code should be 200
|
Then the response status code should be 200
|
||||||
|
|
||||||
Scenario: The route for get one citizen must response a 200 and return citizen
|
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"
|
Given I have citizen Linus Pauling with ID "47a05c0f-7329-46c3-a7d0-325db37e9114"
|
||||||
When I send a GET request to "/citizens/6434f4f9-f570-f22a-c134-8668350651ff"
|
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
|
Then the response status code should be 200
|
||||||
And the response should contain object:
|
And the response should contain object:
|
||||||
| id | 6434f4f9-f570-f22a-c134-8668350651ff |
|
| id | 47a05c0f-7329-46c3-a7d0-325db37e9114 |
|
||||||
|
|
||||||
Scenario: Can get connected citizen
|
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"
|
When I send a GET request to "/citizens/current"
|
||||||
Then the response status code should be 200
|
Then the response status code should be 200
|
||||||
And the response should contain object:
|
And the response should contain object:
|
||||||
| id | 64b7b379-2298-43ec-b428-ba134930cabd |
|
| id | 47356809-c8ef-4649-8b99-1c5cb9886d38 |
|
||||||
|
|
||||||
@online
|
@online
|
||||||
Scenario: Can be connect with SSO
|
Scenario: Can be connect with SSO
|
||||||
Given I have citizen:
|
Given I have citizen
|
||||||
| id | c606110c-ff0e-4d09-a79e-74632d7bf7bd |
|
| id | c606110c-ff0e-4d09-a79e-74632d7bf7bd |
|
||||||
| firstName | John |
|
|
||||||
| lastName | Doe |
|
|
||||||
| email | fabrice.lecomte.be@gmail.com |
|
| email | fabrice.lecomte.be@gmail.com |
|
||||||
|
| firstName | Leonhard |
|
||||||
|
| lastName | Euler |
|
||||||
When I send a POST request to "/sso" with body:
|
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
|
Then the response status code should be 204
|
||||||
|
|
||||||
Scenario: Can be change my password
|
Scenario: Can be change my password
|
||||||
Given I am authenticated as Joe Patate with id "c211dca6-aa21-45c2-95ba-c7f2179ee37e"
|
Given I have citizen Georges Charpak with ID "0c966522-4071-43e5-a3ca-cfff2557f2cf"
|
||||||
When I send a PUT request to "/citizens/c211dca6-aa21-45c2-95ba-c7f2179ee37e/password/change" with body:
|
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",
|
"old_password": "azerty",
|
||||||
@@ -47,8 +52,10 @@ Feature: citizens routes
|
|||||||
Then the response status code should be 201
|
Then the response status code should be 201
|
||||||
|
|
||||||
Scenario: If a send bad request when a change password, that return a 400 Bad request
|
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",
|
"plup": "azerty",
|
||||||
|
|||||||
Reference in New Issue
Block a user