From 0221d7023a35d9c9e358c2e680c31aa3039579b0 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 16 Mar 2020 13:30:24 +0100 Subject: [PATCH 01/24] Refactoring Article Tests --- src/test/kotlin/RunCucumberTest.kt | 1 - src/test/kotlin/feature/ArticleSteps.kt | 91 +++++++++------------ src/test/resources/feature/articles.feature | 19 +++-- 3 files changed, 50 insertions(+), 61 deletions(-) diff --git a/src/test/kotlin/RunCucumberTest.kt b/src/test/kotlin/RunCucumberTest.kt index ccb2f1e..521fe75 100644 --- a/src/test/kotlin/RunCucumberTest.kt +++ b/src/test/kotlin/RunCucumberTest.kt @@ -42,7 +42,6 @@ class RunCucumberTest : En, KoinTest { withTestApplication({ module(CUCUMBER) }) { migrations() - fixtures() } unitialized = true } diff --git a/src/test/kotlin/feature/ArticleSteps.kt b/src/test/kotlin/feature/ArticleSteps.kt index 3d086c3..f0b33e0 100644 --- a/src/test/kotlin/feature/ArticleSteps.kt +++ b/src/test/kotlin/feature/ArticleSteps.kt @@ -18,61 +18,14 @@ import fr.dcproject.repository.Citizen as CitizenRepository class ArticleSteps : En, KoinTest { init { - /** - * @deprecated - */ - Given("I have article with id {string}") { id: String -> - var citizen = Citizen( - name = CitizenI.Name("John", "Doe"), - email = "john.doe@gmail.com", - birthday = DateTime.now(), - user = UserEntity(username = "john-doe", plainPassword = "azerty") - ) - - try { - get().insertWithUser(citizen) - } catch (e: CompletionException) { - citizen = get().findByUsername("john-doe")!! - } - - val article = ArticleEntity( - id = UUID.fromString(id), - title = "hello", - content = "bla bla bla", - description = "A super article", - createdBy = citizen - ) - get().upsert(article) - } - - Given("I have article") { extraData: DataTable -> - extraData.asMap(String::class.java, String::class.java).let { params -> - val username = params["createdBy"]?.toLowerCase()?.replace(' ', '-') - ?: error("You must provide the 'createdBy' parameter") - val citizen = get().findByUsername(username) ?: error("Citizen not exist") - val id = params["id"]?.toUUID() ?: UUID.randomUUID() - val article = ArticleEntity( - id = id, - title = "hello", - content = "bla bla bla", - description = "A super article", - createdBy = citizen - ) - get().upsert(article) + Given("I have {int} article") { nb: Int -> + repeat(nb) { + createArticle() } } - Given("I have article with id {string} created by {string}") { id: String, username: String -> - val citizen = get().findByUsername(username)!! - - val article = ArticleEntity( - id = UUID.fromString(id), - title = "hello", - content = "bla bla bla", - description = "A super article", - createdBy = citizen - ) - get().upsert(article) + Given("I have article") { extraData: DataTable? -> + createArticle(extraData) } Given("I have comment {string} on article {string}") { commentId: String, articleId: String -> @@ -107,4 +60,38 @@ class ArticleSteps : En, KoinTest { get().comment(comment) } } + + private fun createArticle(extraData: DataTable? = null) { + val params = extraData?.asMap(String::class.java, String::class.java) + val createdByUsername = params?.get("createdBy") + val username = (createdByUsername ?: UUID.randomUUID().toString()) + .toLowerCase().replace(' ', '-') + + val createdBy = if (createdByUsername != null) { + get().findByUsername(username) ?: error("Citizen not exist") + } else { + val first = "firstName"+UUID.randomUUID().toString() + val last = "lastName"+UUID.randomUUID().toString() + Citizen( + birthday = DateTime.now(), + name = CitizenI.Name( + first, + last + ), + email = "$first@fakeemail.com", + user = UserEntity(username = username, plainPassword = "azerty") + ).also { + get().insertWithUser(it) + } + } + + val article = ArticleEntity( + id = params?.get("id")?.toUUID() ?: UUID.randomUUID(), + title = "hello", + content = "bla bla bla", + description = "A super article", + createdBy = createdBy + ) + get().upsert(article) + } } \ No newline at end of file diff --git a/src/test/resources/feature/articles.feature b/src/test/resources/feature/articles.feature index f18fcd0..56174b1 100644 --- a/src/test/resources/feature/articles.feature +++ b/src/test/resources/feature/articles.feature @@ -2,21 +2,27 @@ Feature: articles routes Scenario: The route for get articles must response a 200 + Given I have 3 article When I send a GET request to "/articles" Then the response status code should be 200 Scenario: Can get versions of article by the id - When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/versions" + Given I have article + | id | 13e6091c-8fed-4600-b079-a97a6b7a9800 | + When I send a GET request to "/articles/13e6091c-8fed-4600-b079-a97a6b7a9800/versions" Then the response status code should be 200 Scenario: The route for get one article must response a 200 and return article - When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" + Given I have article + | id | 65cda9f3-8991-4420-8d41-1da9da72c9bb | + When I send a GET request to "/articles/65cda9f3-8991-4420-8d41-1da9da72c9bb" Then the response status code should be 200 And the response should contain object: - | id | 9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b | + | id | 65cda9f3-8991-4420-8d41-1da9da72c9bb | Scenario: The route for create article must response a 200 and return object - Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" + Given I have citizen John Doe + And I am authenticated as John Doe When I send a POST request to "/articles" with body: """ { @@ -27,10 +33,7 @@ Feature: articles routes "description": "description2", "tags": [ "green" - ], - "created_by": { - "id": "64b7b379-2298-43ec-b428-ba134930cabd" - } + ] } """ Then the response status code should be 200 From 68238494e53020e1c7cd856d95124abe834a1513 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 16 Mar 2020 13:35:12 +0100 Subject: [PATCH 02/24] Refactoring Auth Tests --- .idea/runConfigurations/Auth_Tests.xml | 24 ++++++++++++++++++++++++ src/test/resources/feature/auth.feature | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 .idea/runConfigurations/Auth_Tests.xml diff --git a/.idea/runConfigurations/Auth_Tests.xml b/.idea/runConfigurations/Auth_Tests.xml new file mode 100644 index 0000000..21c536b --- /dev/null +++ b/.idea/runConfigurations/Auth_Tests.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/feature/auth.feature b/src/test/resources/feature/auth.feature index 199d490..9ff7e3e 100644 --- a/src/test/resources/feature/auth.feature +++ b/src/test/resources/feature/auth.feature @@ -1,3 +1,4 @@ +@auth Feature: Auth routes Scenario: The route for create citizen must response a 200 and return object @@ -34,10 +35,11 @@ Feature: Auth routes Then the response status code should be 400 Scenario: The route for create citizen must response a 200 and return object + Given I have citizen Niels Bohr When I send a POST request to "/login" with body: """ { - "name": "username-1", + "name": "niels-bohr", "password": "azerty" } """ From 561905f7abd8a44bfd632015ea8bb47ffe6f9dd1 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 16 Mar 2020 13:55:25 +0100 Subject: [PATCH 03/24] 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", From 74503c9d3a62c46050dee20cfac127d4c64a7ce0 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 16 Mar 2020 15:02:43 +0100 Subject: [PATCH 04/24] Refactoring Comment Tests --- .idea/runConfigurations/Citizen_Tests.xml | 24 ++++++++++ .idea/runConfigurations/Comment_Tests.xml | 24 ++++++++++ src/test/kotlin/feature/ArticleSteps.kt | 52 +++++++++------------- src/test/resources/feature/comment.feature | 10 +++-- 4 files changed, 76 insertions(+), 34 deletions(-) create mode 100644 .idea/runConfigurations/Citizen_Tests.xml create mode 100644 .idea/runConfigurations/Comment_Tests.xml diff --git a/.idea/runConfigurations/Citizen_Tests.xml b/.idea/runConfigurations/Citizen_Tests.xml new file mode 100644 index 0000000..eb19d1e --- /dev/null +++ b/.idea/runConfigurations/Citizen_Tests.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Comment_Tests.xml b/.idea/runConfigurations/Comment_Tests.xml new file mode 100644 index 0000000..6ae0bd4 --- /dev/null +++ b/.idea/runConfigurations/Comment_Tests.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/kotlin/feature/ArticleSteps.kt b/src/test/kotlin/feature/ArticleSteps.kt index f0b33e0..aa91800 100644 --- a/src/test/kotlin/feature/ArticleSteps.kt +++ b/src/test/kotlin/feature/ArticleSteps.kt @@ -9,7 +9,6 @@ import org.joda.time.DateTime import org.koin.test.KoinTest import org.koin.test.get import java.util.* -import java.util.concurrent.CompletionException import fr.dcproject.entity.Article as ArticleEntity import fr.dcproject.entity.Comment as CommentEntity import fr.dcproject.entity.User as UserEntity @@ -28,36 +27,11 @@ class ArticleSteps : En, KoinTest { createArticle(extraData) } - Given("I have comment {string} on article {string}") { commentId: String, articleId: String -> - var citizen = Citizen( - name = CitizenI.Name("John", "Doe"), - email = "john.doe@gmail.com", - birthday = DateTime.now(), - user = UserEntity(username = "john-doe", plainPassword = "azerty") - ) - - try { - get().insertWithUser(citizen) - } catch (e: CompletionException) { - citizen = get().findByUsername("john-doe")!! - } - - val article = ArticleEntity( - id = UUID.fromString(articleId), - title = "hello", - content = "bla bla bla", - description = "A super article", - createdBy = citizen - ) - get().upsert(article) - - val comment: CommentEntity = CommentEntity( - id = UUID.fromString(commentId), - createdBy = citizen, - target = article, - content = "hello" - ) - get().comment(comment) + Given("I have comment created by {word} {word} on article {string}:") { firstName: String, lastName: String, articleId: String, extraData: DataTable? -> + commentArticle(articleId, firstName, lastName, extraData) + } + Given("I have comment created by {word} {word} on article {string}") { firstName: String, lastName: String, articleId: String -> + commentArticle(articleId, firstName, lastName) } } @@ -94,4 +68,20 @@ class ArticleSteps : En, KoinTest { ) get().upsert(article) } + + private fun commentArticle(articleId: String, firstName: String, lastName: String, extraData: DataTable? = null) { + val params = extraData?.asMap(String::class.java, String::class.java) + + val article = get().findById(UUID.fromString(articleId)) ?: error("Article not exist") + + val citizen = get().findByUsername(("$firstName-$lastName".toLowerCase()).toLowerCase().replace(' ', '-')) ?: error("Citizen not exist") + + val comment: CommentEntity = CommentEntity( + id = params?.get("commentId")?.let { UUID.fromString(it) } ?: UUID.randomUUID(), + createdBy = citizen, + target = article, + content = params?.get("content") ?: "hello" + ) + get().comment(comment) + } } \ No newline at end of file diff --git a/src/test/resources/feature/comment.feature b/src/test/resources/feature/comment.feature index 77d607d..f4fcc2e 100644 --- a/src/test/resources/feature/comment.feature +++ b/src/test/resources/feature/comment.feature @@ -1,7 +1,11 @@ +@comment Feature: comment Scenario: Can comment childrens - Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" - And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" - When I send a GET request to "/comments/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/children" + Given I have citizen John Dalton + And I am authenticated as John Dalton + And I have article + | id | 4c948e8f-eada-4e10-8d7d-7192affe1313 | + And I have comment created by John Dalton on article "4c948e8f-eada-4e10-8d7d-7192affe1313" + When I send a GET request to "/comments/4c948e8f-eada-4e10-8d7d-7192affe1313/children" Then the response status code should be 200 \ No newline at end of file From bbe54c31159551ffe369d898102a53d9ac661c92 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 16 Mar 2020 20:27:20 +0100 Subject: [PATCH 05/24] Refactoring CommentArticle Tests --- src/test/kotlin/feature/ArticleSteps.kt | 4 +- .../resources/feature/commentArticle.feature | 61 ++++++++++++++----- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/test/kotlin/feature/ArticleSteps.kt b/src/test/kotlin/feature/ArticleSteps.kt index aa91800..cf3b539 100644 --- a/src/test/kotlin/feature/ArticleSteps.kt +++ b/src/test/kotlin/feature/ArticleSteps.kt @@ -38,7 +38,7 @@ class ArticleSteps : En, KoinTest { private fun createArticle(extraData: DataTable? = null) { val params = extraData?.asMap(String::class.java, String::class.java) val createdByUsername = params?.get("createdBy") - val username = (createdByUsername ?: UUID.randomUUID().toString()) + val username = (createdByUsername ?: "username"+UUID.randomUUID().toString()) .toLowerCase().replace(' ', '-') val createdBy = if (createdByUsername != null) { @@ -77,7 +77,7 @@ class ArticleSteps : En, KoinTest { val citizen = get().findByUsername(("$firstName-$lastName".toLowerCase()).toLowerCase().replace(' ', '-')) ?: error("Citizen not exist") val comment: CommentEntity = CommentEntity( - id = params?.get("commentId")?.let { UUID.fromString(it) } ?: UUID.randomUUID(), + id = params?.get("id")?.let { UUID.fromString(it) } ?: UUID.randomUUID(), createdBy = citizen, target = article, content = params?.get("content") ?: "hello" diff --git a/src/test/resources/feature/commentArticle.feature b/src/test/resources/feature/commentArticle.feature index 7016ed1..5a1ead1 100644 --- a/src/test/resources/feature/commentArticle.feature +++ b/src/test/resources/feature/commentArticle.feature @@ -1,9 +1,12 @@ +@comment Feature: comment Article Scenario: Can comment an article - Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" - And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" - When I send a POST request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/comments" with body: + Given I have citizen Michael Faraday + And I am authenticated as Michael Faraday + And I have article + | id | aa16c635-28da-46f0-9a89-934eef88c7ca | + When I send a POST request to "/articles/aa16c635-28da-46f0-9a89-934eef88c7ca/comments" with body: """ { "content": "Hello mister" @@ -11,32 +14,52 @@ Feature: comment Article """ Then the response status code should be 201 + # TODO add more comment on article Scenario: Can get all comment on article - Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" - And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" - When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/comments" + Given I have citizen Enrico Fermi + And I am authenticated as Enrico Fermi + And I have article + | id | 6166c078-ca97-4366-b0aa-2a5cd558c78a | + And I have comment created by Enrico Fermi on article "6166c078-ca97-4366-b0aa-2a5cd558c78a" + When I send a GET request to "/articles/6166c078-ca97-4366-b0aa-2a5cd558c78a/comments" Then the response status code should be 200 + # TODO add votes Scenario: Can get all comment on article sorted by votes - Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" - And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" - When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/comments?sort=votes" + Given I have citizen Pierre Curie + And I am authenticated as Pierre Curie + And I have article + | id | 5e209f63-57ce-43ca-922a-273b0d62f567 | + And I have comment created by Enrico Fermi on article "5e209f63-57ce-43ca-922a-273b0d62f567" + When I send a GET request to "/articles/5e209f63-57ce-43ca-922a-273b0d62f567/comments?sort=votes" Then the response status code should be 200 And the response should contain object: - | result[1].votes.up | 1 | + | $.result[0].votes.up | 0 | Scenario: Can get comments on articles of the current citizen - Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" - And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" - When I send a GET request to "/citizens/64b7b379-2298-43ec-b428-ba134930cabd/comments/articles" + Given I have citizen Erwin Schrodinger with ID "292a20cc-4a60-489e-9866-a95d38ffaf47" + And I have article + | id | 17df7fb9-b388-4e20-ab19-29c29972da01 | + | createdBy | Erwin Schrodinger | + And I have comment created by Erwin Schrodinger on article "17df7fb9-b388-4e20-ab19-29c29972da01" + When I send a GET request to "/citizens/292a20cc-4a60-489e-9866-a95d38ffaf47/comments/articles" Then the response status code should be 200 And the response should contain object: | current_page | 1 | | limit | 50 | + And the Response should contain: + """ + 292a20cc-4a60-489e-9866-a95d38ffaf47 + """ Scenario: Can edit a comment - Given I am authenticated as username 3 with id "92877af7-0a45-fd6a-2ed7-fe81e1236b78" - When I send a PUT request to "/comments/2f01c257-cf20-3466-fb10-a3b8eff12a97" with body: + Given I have citizen Hubert Reeves + And I have article + | id | bb05e4a3-55a1-4088-85e7-8d8c23be29b1 | + And I am authenticated as Hubert Reeves + And I have comment created by Hubert Reeves on article "bb05e4a3-55a1-4088-85e7-8d8c23be29b1": + | id | fd30d20f-656c-42c6-8955-f61c04537464 | + When I send a PUT request to "/comments/fd30d20f-656c-42c6-8955-f61c04537464" with body: """ Hello boy """ @@ -45,7 +68,13 @@ Feature: comment Article | content | Hello boy | Scenario: Can get comment by its ID - When I send a GET request to "/comments/2f01c257-cf20-3466-fb10-a3b8eff12a97" + Given I have citizen Alfred Kastler + And I have article + | id | 3897465b-19d2-43a0-86ea-1e29dbb11ec9 | + And I have comment created by Alfred Kastler on article "3897465b-19d2-43a0-86ea-1e29dbb11ec9": + | id | edd296a8-fc7a-4717-a2bb-9f035ceca3c2 | + | content | Hello boy | + When I send a GET request to "/comments/edd296a8-fc7a-4717-a2bb-9f035ceca3c2" Then the response status code should be 200 And the JSON should contain: | content | Hello boy | From 10b28446203f280deb1bbbc8812c412d087f05e0 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 16 Mar 2020 20:39:16 +0100 Subject: [PATCH 06/24] Refactoring CommentArticle Tests --- src/test/kotlin/feature/ConstitutionSteps.kt | 113 +++++++++++------- .../feature/commentConstitution.feature | 40 +++---- 2 files changed, 89 insertions(+), 64 deletions(-) diff --git a/src/test/kotlin/feature/ConstitutionSteps.kt b/src/test/kotlin/feature/ConstitutionSteps.kt index 6d4ea23..463e671 100644 --- a/src/test/kotlin/feature/ConstitutionSteps.kt +++ b/src/test/kotlin/feature/ConstitutionSteps.kt @@ -1,62 +1,91 @@ package feature import fr.dcproject.entity.* -import fr.dcproject.entity.ConstitutionSimple.TitleSimple -import fr.dcproject.entity.request.Constitution +import fr.dcproject.repository.CommentConstitution +import fr.dcproject.utils.toUUID +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 java.util.concurrent.CompletionException +import fr.dcproject.entity.Constitution as ConstitutionEntity +import fr.dcproject.entity.Comment as CommentEntity import fr.dcproject.entity.User as UserEntity -import fr.dcproject.repository.Citizen as CitizenRepository import fr.dcproject.repository.Constitution as ConstitutionRepository +import fr.dcproject.repository.Citizen as CitizenRepository class ConstitutionSteps : En, KoinTest { init { - Given("I have constitution with id {string}") { id: String -> - var citizen = Citizen( - id = UUID.fromString(id), - name = CitizenI.Name("John", "Doe"), - email = "john.doe@gmail.com", - birthday = DateTime.now(), - user = UserEntity(username = "john-doe", plainPassword = "azerty") - ) - - try { - get().insertWithUser(citizen) - } catch (e: CompletionException) { - citizen = get().findByUsername("john-doe")!! + Given("I have {int} constitution") { nb: Int -> + repeat(nb) { + createConstitution() } - - val title1 = Constitution.Title( - name = "My Title" - ) - - val constitution = Constitution( - title = "hello", - titles = mutableListOf(title1), - anonymous = false - ) - get().upsert(constitution.create(citizen)) } - Given("I have constitution with id {string} created by {string}") { id: String, username: String -> - val citizen = get().findByUsername(username)!! + Given("I have constitution") { extraData: DataTable? -> + createConstitution(extraData) + } - val title1 = TitleSimple( - name = "My Title" - ) - - val constitution = ConstitutionSimple>( - id = UUID.fromString(id), - title = "hello", - titles = mutableListOf(title1), - anonymous = false, - createdBy = citizen - ) - get().upsert(constitution) + Given("I have comment created by {word} {word} on constitution {string}:") { firstName: String, lastName: String, constitutionId: String, extraData: DataTable? -> + commentConstitution(constitutionId, firstName, lastName, extraData) + } + Given("I have comment created by {word} {word} on constitution {string}") { firstName: String, lastName: String, constitutionId: String -> + commentConstitution(constitutionId, firstName, lastName) } } + + private fun createConstitution(extraData: DataTable? = null) { + val params = extraData?.asMap(String::class.java, String::class.java) + val createdByUsername = params?.get("createdBy") + val username = (createdByUsername ?: "username"+UUID.randomUUID().toString()) + .toLowerCase().replace(' ', '-') + + val createdBy = if (createdByUsername != null) { + get().findByUsername(username) ?: error("Citizen not exist") + } else { + val first = "firstName"+UUID.randomUUID().toString() + val last = "lastName"+UUID.randomUUID().toString() + Citizen( + birthday = DateTime.now(), + name = CitizenI.Name( + first, + last + ), + email = "$first@fakeemail.com", + user = UserEntity(username = username, plainPassword = "azerty") + ).also { + get().insertWithUser(it) + } + } + + val title1 = ConstitutionSimple.TitleSimple( + name = "My Title" + ) + + val constitution = ConstitutionSimple>( + id = params?.get("id")?.toUUID() ?: UUID.randomUUID(), + title = "hello", + titles = mutableListOf(title1), + anonymous = false, + createdBy = createdBy + ) + get().upsert(constitution) + } + + private fun commentConstitution(constitutionId: String, firstName: String, lastName: String, extraData: DataTable? = null) { + val params = extraData?.asMap(String::class.java, String::class.java) + + val constitution = get().findById(UUID.fromString(constitutionId)) ?: error("Constitution not exist") + + val citizen = get().findByUsername(("$firstName-$lastName".toLowerCase()).toLowerCase().replace(' ', '-')) ?: error("Citizen not exist") + + val comment: CommentEntity = CommentEntity( + id = params?.get("id")?.let { UUID.fromString(it) } ?: UUID.randomUUID(), + createdBy = citizen, + target = constitution, + content = params?.get("content") ?: "hello" + ) + get().comment(comment) + } } \ No newline at end of file diff --git a/src/test/resources/feature/commentConstitution.feature b/src/test/resources/feature/commentConstitution.feature index 8cfb9ce..6a1001c 100644 --- a/src/test/resources/feature/commentConstitution.feature +++ b/src/test/resources/feature/commentConstitution.feature @@ -1,35 +1,31 @@ +@comment Feature: comment Constitution Scenario: Can comment an constitution - Given I am authenticated as John Toe with id "a6eb1f5a-8c02-42f4-8e8e-a722f26841ef" - And I have constitution with id "d7e20f0b-3fdd-4638-817a-bbd87054eb82" created by "john-toe" - When I send a POST request to "/constitutions/d7e20f0b-3fdd-4638-817a-bbd87054eb82/comments" with body: + Given I have citizen Nicolas Copernic + And I am authenticated as Nicolas Copernic + And I have constitution + | id | 1707c287-a472-4a62-89f2-9e85030e915c | + When I send a POST request to "/constitutions/1707c287-a472-4a62-89f2-9e85030e915c/comments" with body: """ - Hello mister + { + "content": "Hello mister" + } """ Then the response status code should be 201 - Scenario: Can get comments on constitution of the current citizen - Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" - And I have constitution with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" - When I send a GET request to "/citizens/64b7b379-2298-43ec-b428-ba134930cabd/comments/constitutions" + Scenario: Can get comments on constitutions of the current citizen + Given I have citizen Charles Darwin with ID "46e0bda9-ca6a-4c65-a58b-7e7267a0bbc5" + And I have constitution + | id | 34ddd50a-da00-4a90-a869-08baa2a121be | + | createdBy | Charles Darwin | + And I have comment created by Charles Darwin on constitution "34ddd50a-da00-4a90-a869-08baa2a121be" + When I send a GET request to "/citizens/46e0bda9-ca6a-4c65-a58b-7e7267a0bbc5/comments/constitutions" Then the response status code should be 200 And the response should contain object: | current_page | 1 | | limit | 50 | - - Scenario: Can edit a comment - Given I am authenticated as username 3 with id "92877af7-0a45-fd6a-2ed7-fe81e1236b78" - When I send a PUT request to "/comments/b0422e48-687f-bea7-b45f-b6b301246e97" with body: + And the Response should contain: """ - Hello boy + 34ddd50a-da00-4a90-a869-08baa2a121be """ - Then the response status code should be 200 - And the JSON should contain: - | content | Hello boy | - - Scenario: Can get comment by its ID - When I send a GET request to "/comments/b0422e48-687f-bea7-b45f-b6b301246e97" - Then the response status code should be 200 - And the JSON should contain: - | content | Hello boy | From 0e912ef4b134fe0e2a496ec9682fb33742a4b177 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Mon, 16 Mar 2020 21:24:24 +0100 Subject: [PATCH 07/24] Refactoring Follows Tests Add missing route GET /constitutions/{constitution}/follows --- .idea/runConfigurations/Follow_Tests.xml | 2 +- .../fr/dcproject/routes/Constitution.kt | 3 -- .../fr/dcproject/routes/FollowConstitution.kt | 9 +++- src/test/kotlin/feature/ArticleSteps.kt | 18 ++++--- src/test/kotlin/feature/ConstitutionSteps.kt | 11 ++-- src/test/kotlin/feature/FollowSteps.kt | 10 +++- src/test/resources/feature/follow.feature | 50 ------------------- .../resources/feature/followArticle.feature | 42 ++++++++++++++++ .../feature/followConstitution.feature | 42 ++++++++++++++++ 9 files changed, 121 insertions(+), 66 deletions(-) delete mode 100644 src/test/resources/feature/follow.feature create mode 100644 src/test/resources/feature/followArticle.feature create mode 100644 src/test/resources/feature/followConstitution.feature diff --git a/.idea/runConfigurations/Follow_Tests.xml b/.idea/runConfigurations/Follow_Tests.xml index 819c167..5920595 100644 --- a/.idea/runConfigurations/Follow_Tests.xml +++ b/.idea/runConfigurations/Follow_Tests.xml @@ -7,7 +7,7 @@