From 3a18ef0554c7beacab53cddeed29c11c18e38024 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 3 Apr 2021 00:38:56 +0200 Subject: [PATCH] Improve articles request test --- src/test/kotlin/integration/Article routes.kt | 20 ++++++++++++++----- .../kotlin/integration/steps/given/Article.kt | 17 +++++++++++----- .../kotlin/integration/steps/given/Citizen.kt | 7 ++++--- .../kotlin/integration/steps/when/request.kt | 4 ++-- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/test/kotlin/integration/Article routes.kt b/src/test/kotlin/integration/Article routes.kt index 0ed6685..83e7792 100644 --- a/src/test/kotlin/integration/Article routes.kt +++ b/src/test/kotlin/integration/Article routes.kt @@ -1,5 +1,7 @@ package integration +import fr.dcproject.common.utils.toUUID +import integration.steps.`when`.Validate import integration.steps.`when`.`When I send a GET request` import integration.steps.`when`.`When I send a POST request` import integration.steps.`when`.`with body` @@ -18,6 +20,7 @@ import integration.steps.then.`And the response should not contain` import integration.steps.then.`Then the response should be` import integration.steps.then.`whish contains` import integration.steps.then.and +import io.ktor.http.HttpStatusCode.Companion.BadRequest import io.ktor.http.HttpStatusCode.Companion.Forbidden import io.ktor.http.HttpStatusCode.Companion.OK import org.junit.jupiter.api.Tag @@ -32,17 +35,24 @@ class `Article routes` : BaseTest() { fun `I can get article list`() { withIntegrationApplication { `Given I have articles`(3) - `When I send a GET request`("/articles") `Then the response should be` OK and { + `Given I have article`(createdBy = "ddb17f17-e8ab-4ada-bdf7-bfd6b0f1b5ed".toUUID()) + `When I send a GET request`("/articles?page=1&limit=10&sort=title&createdBy=ddb17f17-e8ab-4ada-bdf7-bfd6b0f1b5ed") `Then the response should be` OK and { `And the response should not be null`() `And the response should contain pattern`("$.result[0].createdBy.name.firstName", "firstName.+") - `And the response should contain pattern`("$.result[1].createdBy.name.firstName", "firstName.+") - `And the response should contain pattern`("$.result[2].createdBy.name.firstName", "firstName.+") - `And the response should not contain`("$.result[3]") - `And the response should contain list`("$.result", 3) + `And the response should not contain`("$.result[1]") + `And the response should contain list`("$.result", 1) } } } + @Test + fun `I cannot get article list`() { + withIntegrationApplication { + `Given I have articles`(3) + `When I send a GET request`("/articles?page=1&limit=10&sort=title&createdBy=hello", Validate.ALL - Validate.REQUEST_PARAM) `Then the response should be` BadRequest + } + } + @Test fun `I can get articles filtered by workgroup`() { withIntegrationApplication { diff --git a/src/test/kotlin/integration/steps/given/Article.kt b/src/test/kotlin/integration/steps/given/Article.kt index a9d978d..b32f431 100644 --- a/src/test/kotlin/integration/steps/given/Article.kt +++ b/src/test/kotlin/integration/steps/given/Article.kt @@ -6,6 +6,7 @@ import fr.dcproject.component.article.database.ArticleForUpdate import fr.dcproject.component.article.database.ArticleForView import fr.dcproject.component.article.database.ArticleRepository import fr.dcproject.component.citizen.database.CitizenI.Name +import fr.dcproject.component.citizen.database.CitizenRef import fr.dcproject.component.workgroup.database.WorkgroupRef import io.ktor.server.testing.TestApplicationEngine import org.koin.core.context.GlobalContext @@ -16,7 +17,15 @@ fun TestApplicationEngine.`Given I have article`( workgroup: WorkgroupRef? = null, createdBy: Name? = null ) { - createArticle(id?.toUUID(), workgroup, createdBy) + createArticle(id?.toUUID(), workgroup, createCitizen(name = createdBy)) +} + +fun TestApplicationEngine.`Given I have article`( + id: String? = null, + workgroup: WorkgroupRef? = null, + createdBy: UUID +) { + createArticle(id?.toUUID(), workgroup, createCitizen(id = createdBy)) } fun TestApplicationEngine.`Given I have articles`( @@ -35,18 +44,16 @@ fun TestApplicationEngine.`Given I have article created by workgroup`( fun createArticle( id: UUID? = null, workgroup: WorkgroupRef? = null, - createdBy: Name? = null + createdBy: CitizenRef = createCitizen() ): ArticleForView { val articleRepository: ArticleRepository by lazy { GlobalContext.get().koin.get() } - val citizen = createCitizen(createdBy) - val article = ArticleForUpdate( id = id ?: UUID.randomUUID(), title = LoremIpsum().getTitle(3), content = LoremIpsum().getParagraphs(1, 2), description = LoremIpsum().getParagraphs(1, 2), - createdBy = citizen, + createdBy = createdBy, workgroup = workgroup, versionId = UUID.randomUUID() ) diff --git a/src/test/kotlin/integration/steps/given/Citizen.kt b/src/test/kotlin/integration/steps/given/Citizen.kt index 9d81861..951491c 100644 --- a/src/test/kotlin/integration/steps/given/Citizen.kt +++ b/src/test/kotlin/integration/steps/given/Citizen.kt @@ -36,16 +36,17 @@ fun TestApplicationEngine.`Given I have citizen`( return repo.insertWithUser(citizen)?.also { callback(it) } } -fun createCitizen(createdBy: CitizenI.Name? = null): Citizen { +fun createCitizen(name: CitizenI.Name? = null, id: UUID = UUID.randomUUID()): Citizen { val citizenRepository: CitizenRepository by lazy { GlobalContext.get().koin.get() } - return if (createdBy != null) { - citizenRepository.findByName(createdBy) ?: error("Citizen not exist") + return if (name != null) { + citizenRepository.findByName(name) ?: error("Citizen not exist") } else { val first = "firstName" + UUID.randomUUID().toString() val last = "lastName" + UUID.randomUUID().toString() val username = ("username" + UUID.randomUUID().toString()) CitizenForCreate( + id = id, birthday = DateTime.now(), name = CitizenI.Name( first, diff --git a/src/test/kotlin/integration/steps/when/request.kt b/src/test/kotlin/integration/steps/when/request.kt index b09e19b..a64a448 100644 --- a/src/test/kotlin/integration/steps/when/request.kt +++ b/src/test/kotlin/integration/steps/when/request.kt @@ -40,7 +40,7 @@ fun TestApplicationCall.valid(validate: BitMaskI): TestApplicationCall { return this } -fun TestApplicationEngine.`When I send a GET request`(uri: String? = null, validate: Validate = Validate.ALL, setup: (TestApplicationRequest.() -> Unit)? = null): TestApplicationCall { +fun TestApplicationEngine.`When I send a GET request`(uri: String? = null, validate: BitMaskI = Validate.ALL, setup: (TestApplicationRequest.() -> Unit)? = null): TestApplicationCall { return handleRequest(true) { method = HttpMethod.Get if (uri != null) { @@ -74,7 +74,7 @@ fun TestApplicationEngine.`When I send a PUT request`(uri: String? = null, valid }.valid(validate) } -fun TestApplicationEngine.`When I send a DELETE request`(uri: String? = null, validate: Validate = Validate.ALL, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall { +fun TestApplicationEngine.`When I send a DELETE request`(uri: String? = null, validate: BitMaskI = Validate.ALL, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall { return handleRequest(true) { method = HttpMethod.Delete if (uri != null) {