Refactoring Article Tests

This commit is contained in:
2020-03-16 13:30:24 +01:00
parent ca78db4155
commit 0221d7023a
3 changed files with 50 additions and 61 deletions

View File

@@ -42,7 +42,6 @@ class RunCucumberTest : En, KoinTest {
withTestApplication({ module(CUCUMBER) }) {
migrations()
fixtures()
}
unitialized = true
}

View File

@@ -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<CitizenRepository>().insertWithUser(citizen)
} catch (e: CompletionException) {
citizen = get<CitizenRepository>().findByUsername("john-doe")!!
}
val article = ArticleEntity(
id = UUID.fromString(id),
title = "hello",
content = "bla bla bla",
description = "A super article",
createdBy = citizen
)
get<ArticleRepository>().upsert(article)
}
Given("I have article") { extraData: DataTable ->
extraData.asMap<String, String>(String::class.java, String::class.java).let { params ->
val username = params["createdBy"]?.toLowerCase()?.replace(' ', '-')
?: error("You must provide the 'createdBy' parameter")
val citizen = get<CitizenRepository>().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<ArticleRepository>().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<CitizenRepository>().findByUsername(username)!!
val article = ArticleEntity(
id = UUID.fromString(id),
title = "hello",
content = "bla bla bla",
description = "A super article",
createdBy = citizen
)
get<ArticleRepository>().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<CommentArticle>().comment(comment)
}
}
private fun createArticle(extraData: DataTable? = null) {
val params = extraData?.asMap<String, String>(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<CitizenRepository>().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<CitizenRepository>().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<ArticleRepository>().upsert(article)
}
}

View File

@@ -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