move Integration step test
This commit is contained in:
76
src/test/kotlin/integration/steps/given/Article.kt
Normal file
76
src/test/kotlin/integration/steps/given/Article.kt
Normal file
@@ -0,0 +1,76 @@
|
||||
package integration.steps.given
|
||||
|
||||
import fr.dcproject.common.utils.toUUID
|
||||
import fr.dcproject.component.article.ArticleForUpdate
|
||||
import fr.dcproject.component.article.ArticleRepository
|
||||
import fr.dcproject.component.auth.UserForCreate
|
||||
import fr.dcproject.component.citizen.CitizenForCreate
|
||||
import fr.dcproject.component.citizen.CitizenI
|
||||
import fr.dcproject.component.citizen.CitizenRepository
|
||||
import fr.dcproject.component.workgroup.WorkgroupRef
|
||||
import io.ktor.server.testing.TestApplicationEngine
|
||||
import org.joda.time.DateTime
|
||||
import org.koin.core.context.GlobalContext
|
||||
import java.util.UUID
|
||||
|
||||
fun TestApplicationEngine.`Given I have article`(
|
||||
id: String? = null,
|
||||
workgroup: WorkgroupRef? = null,
|
||||
createdByUsername: String? = null
|
||||
) {
|
||||
createArticle(id?.toUUID(), workgroup, createdByUsername)
|
||||
}
|
||||
|
||||
fun TestApplicationEngine.`Given I have articles`(
|
||||
numbers: Int,
|
||||
) {
|
||||
repeat(numbers) {
|
||||
createArticle()
|
||||
}
|
||||
}
|
||||
fun TestApplicationEngine.`Given I have article created by workgroup`(
|
||||
workgroupId: String,
|
||||
) {
|
||||
createArticle(workgroup = WorkgroupRef(workgroupId.toUUID()))
|
||||
}
|
||||
|
||||
private fun createArticle(
|
||||
id: UUID? = null,
|
||||
workgroup: WorkgroupRef? = null,
|
||||
createdByUsername: String? = null
|
||||
) {
|
||||
val username = (createdByUsername ?: "username" + UUID.randomUUID().toString())
|
||||
.toLowerCase().replace(' ', '-')
|
||||
|
||||
val citizenRepository: CitizenRepository by lazy<CitizenRepository> { GlobalContext.get().koin.get() }
|
||||
val articleRepository: ArticleRepository by lazy<ArticleRepository> { GlobalContext.get().koin.get() }
|
||||
|
||||
val createdBy = if (createdByUsername != null) {
|
||||
citizenRepository.findByUsername(username) ?: error("Citizen not exist")
|
||||
} else {
|
||||
val first = "firstName" + UUID.randomUUID().toString()
|
||||
val last = "lastName" + UUID.randomUUID().toString()
|
||||
CitizenForCreate(
|
||||
birthday = DateTime.now(),
|
||||
name = CitizenI.Name(
|
||||
first,
|
||||
last
|
||||
),
|
||||
email = "$first@fakeemail.com",
|
||||
user = UserForCreate(username = username, password = "azerty")
|
||||
).let {
|
||||
citizenRepository.insertWithUser(it) ?: error("Unable to create User")
|
||||
}
|
||||
}
|
||||
|
||||
val article = ArticleForUpdate(
|
||||
id = id ?: UUID.randomUUID(),
|
||||
title = "hello",
|
||||
content = "bla bla bla",
|
||||
description = "A super article",
|
||||
createdBy = createdBy,
|
||||
workgroup = workgroup,
|
||||
versionId = UUID.randomUUID()
|
||||
)
|
||||
articleRepository.upsert(article)
|
||||
}
|
||||
26
src/test/kotlin/integration/steps/given/Auth.kt
Normal file
26
src/test/kotlin/integration/steps/given/Auth.kt
Normal file
@@ -0,0 +1,26 @@
|
||||
package integration.steps.given
|
||||
|
||||
import com.auth0.jwt.JWT
|
||||
import fr.dcproject.component.auth.jwt.JwtConfig
|
||||
import fr.dcproject.component.citizen.Citizen
|
||||
import fr.dcproject.component.citizen.CitizenRepository
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.server.testing.TestApplicationRequest
|
||||
import org.koin.core.context.GlobalContext
|
||||
|
||||
fun TestApplicationRequest.`authenticated as`(
|
||||
firstName: String,
|
||||
lastName: String,
|
||||
): Citizen {
|
||||
val username = "$firstName-$lastName".toLowerCase()
|
||||
val repo: CitizenRepository by lazy<CitizenRepository> { GlobalContext.get().koin.get() }
|
||||
val citizen = repo.findByUsername(username) ?: error("Cititzen not exist with username $username")
|
||||
val jwtAsString: String = JWT.create()
|
||||
.withIssuer("dc-project.fr")
|
||||
.withClaim("id", citizen.user.id.toString())
|
||||
.sign(JwtConfig.algorithm)
|
||||
|
||||
addHeader(HttpHeaders.Authorization, "Bearer $jwtAsString")
|
||||
|
||||
return citizen
|
||||
}
|
||||
36
src/test/kotlin/integration/steps/given/Citizen.kt
Normal file
36
src/test/kotlin/integration/steps/given/Citizen.kt
Normal file
@@ -0,0 +1,36 @@
|
||||
package integration.steps.given
|
||||
|
||||
import fr.dcproject.common.utils.toUUID
|
||||
import fr.dcproject.component.auth.UserForCreate
|
||||
import fr.dcproject.component.citizen.Citizen
|
||||
import fr.dcproject.component.citizen.CitizenForCreate
|
||||
import fr.dcproject.component.citizen.CitizenI
|
||||
import fr.dcproject.component.citizen.CitizenRepository
|
||||
import io.ktor.server.testing.TestApplicationEngine
|
||||
import org.joda.time.DateTime
|
||||
import org.koin.core.context.GlobalContext
|
||||
import java.util.UUID
|
||||
|
||||
fun TestApplicationEngine.`Given I have citizen`(
|
||||
firstName: String,
|
||||
lastName: String,
|
||||
email: String = ("$firstName-$lastName".toLowerCase()) + "@dc-project.fr",
|
||||
id: String = UUID.randomUUID().toString()
|
||||
): Citizen? {
|
||||
val repo: CitizenRepository by lazy<CitizenRepository> { GlobalContext.get().koin.get() }
|
||||
|
||||
val user = UserForCreate(
|
||||
id = id.toUUID(),
|
||||
username = "$firstName-$lastName".toLowerCase(),
|
||||
password = "azerty",
|
||||
)
|
||||
val citizen = CitizenForCreate(
|
||||
id = id.toUUID(),
|
||||
name = CitizenI.Name(firstName, lastName),
|
||||
email = email,
|
||||
birthday = DateTime.now(),
|
||||
user = user
|
||||
)
|
||||
|
||||
return repo.insertWithUser(citizen)
|
||||
}
|
||||
67
src/test/kotlin/integration/steps/given/Workgroup.kt
Normal file
67
src/test/kotlin/integration/steps/given/Workgroup.kt
Normal file
@@ -0,0 +1,67 @@
|
||||
package integration.steps.given
|
||||
|
||||
import fr.dcproject.common.utils.toUUID
|
||||
import fr.dcproject.component.auth.UserForCreate
|
||||
import fr.dcproject.component.citizen.CitizenBasic
|
||||
import fr.dcproject.component.citizen.CitizenForCreate
|
||||
import fr.dcproject.component.citizen.CitizenI
|
||||
import fr.dcproject.component.citizen.CitizenRepository
|
||||
import fr.dcproject.component.workgroup.Workgroup
|
||||
import fr.dcproject.component.workgroup.WorkgroupRepository
|
||||
import fr.dcproject.component.workgroup.WorkgroupWithMembersI.Member
|
||||
import fr.dcproject.component.workgroup.WorkgroupWithMembersI.Member.Role.MASTER
|
||||
import io.ktor.server.testing.TestApplicationEngine
|
||||
import org.joda.time.DateTime
|
||||
import org.koin.core.context.GlobalContext
|
||||
import java.util.UUID
|
||||
|
||||
fun TestApplicationEngine.`Given I have workgroup`(
|
||||
id: String? = null,
|
||||
name: String? = null,
|
||||
description: String? = null,
|
||||
anonymous: Boolean? = null,
|
||||
createdByUsername: String? = null
|
||||
) {
|
||||
createWorkgroup(id?.toUUID(), name, description, anonymous, createdByUsername)
|
||||
}
|
||||
|
||||
private fun createWorkgroup(
|
||||
id: UUID? = null,
|
||||
name: String? = null,
|
||||
description: String? = null,
|
||||
anonymous: Boolean? = null,
|
||||
createdByUsername: String? = null
|
||||
): Workgroup<CitizenBasic> {
|
||||
val username = (createdByUsername ?: "username" + UUID.randomUUID().toString())
|
||||
.toLowerCase().replace(' ', '-')
|
||||
|
||||
val citizenRepository: CitizenRepository by lazy<CitizenRepository> { GlobalContext.get().koin.get() }
|
||||
val workgroupRepository: WorkgroupRepository by lazy<WorkgroupRepository> { GlobalContext.get().koin.get() }
|
||||
|
||||
val creator = citizenRepository.findByUsername(username.toLowerCase().replace(' ', '-'))
|
||||
?: run {
|
||||
val user = UserForCreate(
|
||||
username = username,
|
||||
password = "azerty",
|
||||
)
|
||||
CitizenForCreate(
|
||||
name = CitizenI.Name("Paul", "Langevin"),
|
||||
email = "$username@dc-project.fr",
|
||||
birthday = DateTime.now(),
|
||||
user = user
|
||||
).let {
|
||||
citizenRepository.insertWithUser(it) ?: error("Unable to create User")
|
||||
}
|
||||
}
|
||||
|
||||
val workgroup = Workgroup(
|
||||
id = id ?: UUID.randomUUID(),
|
||||
name = name ?: "Les Incoruptible",
|
||||
description = description ?: "La vie est notre jeux",
|
||||
createdBy = creator,
|
||||
anonymous = (anonymous ?: false) == true,
|
||||
members = listOf(Member(creator, listOf(MASTER)))
|
||||
)
|
||||
|
||||
return workgroupRepository.upsert(workgroup)
|
||||
}
|
||||
53
src/test/kotlin/integration/steps/then/request.kt
Normal file
53
src/test/kotlin/integration/steps/then/request.kt
Normal file
@@ -0,0 +1,53 @@
|
||||
package integration.steps
|
||||
|
||||
import com.jayway.jsonpath.JsonPath
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.testing.TestApplicationCall
|
||||
import io.ktor.server.testing.TestApplicationResponse
|
||||
import org.amshove.kluent.`should be equal to`
|
||||
import org.amshove.kluent.`should be`
|
||||
import org.amshove.kluent.`should not be null`
|
||||
import org.amshove.kluent.shouldContain
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun TestApplicationCall.`Then the response should be`(status: HttpStatusCode? = null, block: TestApplicationResponse.() -> Unit): TestApplicationCall = this.apply {
|
||||
if (status != null) {
|
||||
response.status().`should be`(status)
|
||||
}
|
||||
block(response)
|
||||
}
|
||||
|
||||
infix fun TestApplicationCall.`Then the response should be`(status: HttpStatusCode): TestApplicationCall = this.apply {
|
||||
response.status().`should be`(status)
|
||||
}
|
||||
|
||||
infix fun TestApplicationCall.and(block: TestApplicationResponse.() -> Unit): TestApplicationCall = this.apply {
|
||||
block(response)
|
||||
}
|
||||
|
||||
infix fun TestApplicationCall.`has property`(path: String): Pair<JsonPath, Any> =
|
||||
JsonPath.compile(path).let { jsonPath ->
|
||||
jsonPath.read<Any>(response.content)?.let { result ->
|
||||
Pair(jsonPath, result)
|
||||
} ?: throw AssertionError("\"${path}\" element not found on json response")
|
||||
}
|
||||
|
||||
infix fun TestApplicationResponse.`And have property`(path: String): Pair<JsonPath, Any> =
|
||||
JsonPath.compile(path).let { jsonPath ->
|
||||
jsonPath.read<Any>(content)?.let { result ->
|
||||
Pair(jsonPath, result)
|
||||
} ?: throw AssertionError("\"${path}\" element not found on json response")
|
||||
}
|
||||
|
||||
infix fun Pair<JsonPath, Any>.`whish contains`(expected: Any): Pair<JsonPath, Any> = this.apply {
|
||||
second `should be equal to` expected
|
||||
}
|
||||
|
||||
fun TestApplicationResponse.`And the response should contain`(path: String, valueExpected: String) {
|
||||
assertEquals(valueExpected, JsonPath.read<Any>(content, path)?.toString() ?: throw AssertionError("\"$path -> ${valueExpected}\" element not found on json response"))
|
||||
}
|
||||
|
||||
val TestApplicationResponse.`And the response should not be null` get() = content.`should not be null`()
|
||||
infix fun String.`and should contains`(expected: String) = this
|
||||
.`should not be null`()
|
||||
.shouldContain(expected)
|
||||
53
src/test/kotlin/integration/steps/when/request.kt
Normal file
53
src/test/kotlin/integration/steps/when/request.kt
Normal file
@@ -0,0 +1,53 @@
|
||||
package integration.steps.`when`
|
||||
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpMethod
|
||||
import io.ktor.server.testing.TestApplicationCall
|
||||
import io.ktor.server.testing.TestApplicationEngine
|
||||
import io.ktor.server.testing.TestApplicationRequest
|
||||
import io.ktor.server.testing.setBody
|
||||
|
||||
public fun TestApplicationEngine.`When I send a GET request`(uri: String? = null, setup: (TestApplicationRequest.() -> Unit)? = null): TestApplicationCall {
|
||||
val setupOveride: TestApplicationRequest.() -> Unit = {
|
||||
method = HttpMethod.Get
|
||||
if (uri != null) {
|
||||
this.uri = uri
|
||||
}
|
||||
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
|
||||
setup?.let { it() }
|
||||
}
|
||||
return handleRequest(true, setupOveride)
|
||||
}
|
||||
|
||||
public fun TestApplicationEngine.`When I send a POST request`(uri: String? = null, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall {
|
||||
val setupOveride: TestApplicationRequest.() -> Unit = {
|
||||
method = HttpMethod.Post
|
||||
if (uri != null) {
|
||||
this.uri = uri
|
||||
}
|
||||
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
|
||||
setup?.let { it() }?.let {
|
||||
setBody(it.trimIndent())
|
||||
}
|
||||
}
|
||||
return handleRequest(true, setupOveride)
|
||||
}
|
||||
|
||||
public fun TestApplicationEngine.`When I send a PUT request`(uri: String? = null, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall {
|
||||
val setupOveride: TestApplicationRequest.() -> Unit = {
|
||||
method = HttpMethod.Put
|
||||
if (uri != null) {
|
||||
this.uri = uri
|
||||
}
|
||||
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
|
||||
setup?.let { it() }?.let {
|
||||
setBody(it.trimIndent())
|
||||
}
|
||||
}
|
||||
return handleRequest(true, setupOveride)
|
||||
}
|
||||
|
||||
fun TestApplicationRequest.`with body`(body: String) {
|
||||
setBody(body.trimIndent())
|
||||
}
|
||||
Reference in New Issue
Block a user