Add Integration test for Register
This commit is contained in:
@@ -50,30 +50,36 @@ abstract class BaseTest : KoinTest {
|
||||
return engine.test()
|
||||
}
|
||||
|
||||
public fun TestApplicationEngine.handleGetRequest(uri: String? = null, body: TestApplicationRequest.() -> String): TestApplicationCall {
|
||||
public fun TestApplicationEngine.`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())
|
||||
setBody(body().trimIndent())
|
||||
setup?.let { it() }
|
||||
}
|
||||
return handleRequest(true, setupOveride)
|
||||
}
|
||||
|
||||
public fun TestApplicationEngine.handlePostRequest(uri: String? = null, body: TestApplicationRequest.() -> String): TestApplicationCall {
|
||||
public fun TestApplicationEngine.`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())
|
||||
setBody(body().trimIndent())
|
||||
setup?.let { it() }?.let {
|
||||
setBody(it.trimIndent())
|
||||
}
|
||||
}
|
||||
return handleRequest(true, setupOveride)
|
||||
}
|
||||
|
||||
fun TestApplicationRequest.`with body`(body: String) {
|
||||
setBody(body.trimIndent())
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
fun before() {
|
||||
if (init == false) {
|
||||
@@ -110,10 +116,11 @@ abstract class BaseTest : KoinTest {
|
||||
}
|
||||
|
||||
|
||||
fun TestApplicationCall.`should be respond`(status: HttpStatusCode? = null, block: TestApplicationResponse.() -> Unit) {
|
||||
fun TestApplicationCall.`Then the response should be`(status: HttpStatusCode? = null, block: TestApplicationResponse.() -> Unit): TestApplicationCall {
|
||||
if (status != null) {
|
||||
response.status().`should be`(status)
|
||||
}
|
||||
|
||||
block(response)
|
||||
return this
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
package integration
|
||||
package integration.auth
|
||||
|
||||
import integration.prerequisite.CitizenPrerequisite
|
||||
import integration.BaseTest
|
||||
import integration.`Then the response should be`
|
||||
import integration.prerequisite.`Given I have citizen`
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.util.KtorExperimentalAPI
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import org.amshove.kluent.`should be`
|
||||
import org.amshove.kluent.`should contain`
|
||||
import org.amshove.kluent.`should not be null`
|
||||
import org.junit.jupiter.api.Tag
|
||||
@@ -17,20 +18,20 @@ import org.junit.jupiter.api.TestInstance
|
||||
@KtorExperimentalLocationsAPI
|
||||
@KtorExperimentalAPI
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
@Tags(Tag("integration"))
|
||||
@Tags(Tag("integration"), Tag("auth"))
|
||||
class LoginTest : BaseTest() {
|
||||
@Test
|
||||
fun `I can login`() {
|
||||
fun `I can login with username and password`() {
|
||||
withIntegrationApplication {
|
||||
CitizenPrerequisite().createCitizen("Niels", "Bohr")
|
||||
handlePostRequest("/login") {
|
||||
`Given I have citizen`("Niels", "Bohr")
|
||||
`I send a POST request`("/login") {
|
||||
"""
|
||||
{
|
||||
"username": "niels-bohr",
|
||||
"password": "azerty"
|
||||
}
|
||||
"""
|
||||
}.`should be respond` (HttpStatusCode.OK) {
|
||||
}.`Then the response should be` (HttpStatusCode.OK) {
|
||||
content
|
||||
.`should not be null`()
|
||||
.`should contain`("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.")
|
||||
68
src/test/kotlin/integration/auth/RegisterTest.kt
Normal file
68
src/test/kotlin/integration/auth/RegisterTest.kt
Normal file
@@ -0,0 +1,68 @@
|
||||
package integration.auth
|
||||
|
||||
import integration.BaseTest
|
||||
import integration.`Then the response should be`
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.server.testing.setBody
|
||||
import io.ktor.util.KtorExperimentalAPI
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import org.amshove.kluent.`should be null`
|
||||
import org.amshove.kluent.`should contain`
|
||||
import org.amshove.kluent.`should not be null`
|
||||
import org.junit.experimental.categories.Category
|
||||
import org.junit.jupiter.api.Tag
|
||||
import org.junit.jupiter.api.Tags
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
@KtorExperimentalLocationsAPI
|
||||
@KtorExperimentalAPI
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
@Tags(Tag("integration"), Tag("auth"))
|
||||
class RegisterTest : BaseTest() {
|
||||
@Test
|
||||
@Category(RegisterTest::class)
|
||||
fun `I can register`() {
|
||||
withIntegrationApplication {
|
||||
`I send a POST request`("/register") {
|
||||
"""
|
||||
{
|
||||
"name": {"first_name":"George", "last_name":"MICHEL"},
|
||||
"birthday": "2001-01-01",
|
||||
"user":{
|
||||
"username": "george-junior",
|
||||
"password": "azerty"
|
||||
},
|
||||
"email": "george-junior@gmail.com"
|
||||
}
|
||||
"""
|
||||
}.`Then the response should be` (HttpStatusCode.OK) {
|
||||
content
|
||||
.`should not be null`()
|
||||
.`should contain`("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `I cannot register if no username was sent`() {
|
||||
withIntegrationApplication {
|
||||
`I send a POST request`("/register") {
|
||||
"""
|
||||
{
|
||||
"name": {"first_name":"George2", "last_name":"MICHEL2"},
|
||||
"birthday": "2001-01-01",
|
||||
"user":{
|
||||
"username": "",
|
||||
"password": ""
|
||||
}
|
||||
}
|
||||
"""
|
||||
}.`Then the response should be` (HttpStatusCode.BadRequest) {
|
||||
content.`should be null`()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,43 @@
|
||||
package integration.prerequisite
|
||||
|
||||
import com.auth0.jwt.JWT
|
||||
import fr.dcproject.component.auth.UserForCreate
|
||||
import fr.dcproject.component.auth.jwt.JwtConfig
|
||||
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.http.HttpHeaders
|
||||
import io.ktor.server.testing.TestApplicationEngine
|
||||
import io.ktor.server.testing.TestApplicationRequest
|
||||
import org.joda.time.DateTime
|
||||
import org.koin.core.KoinComponent
|
||||
import org.koin.core.context.GlobalContext
|
||||
import org.koin.core.get
|
||||
import org.koin.test.get
|
||||
import steps.KtorServerContext
|
||||
import java.util.UUID
|
||||
|
||||
class CitizenPrerequisite : KoinComponent {
|
||||
fun createCitizen(
|
||||
firstName: String,
|
||||
lastName: String,
|
||||
email: String = ("$firstName-$lastName".toLowerCase()) + "@dc-project.fr",
|
||||
id: UUID = UUID.randomUUID()
|
||||
): Citizen? {
|
||||
fun TestApplicationEngine.`Given I have citizen`(
|
||||
firstName: String,
|
||||
lastName: String,
|
||||
email: String = ("$firstName-$lastName".toLowerCase()) + "@dc-project.fr",
|
||||
id: UUID = UUID.randomUUID()
|
||||
): Citizen? {
|
||||
val repo: CitizenRepository by lazy<CitizenRepository> { GlobalContext.get().koin.get() }
|
||||
|
||||
val user = UserForCreate(
|
||||
id = id,
|
||||
username = "$firstName-$lastName".toLowerCase(),
|
||||
password = "azerty",
|
||||
)
|
||||
val citizen = CitizenForCreate(
|
||||
id = id,
|
||||
name = CitizenI.Name(firstName, lastName),
|
||||
email = email,
|
||||
birthday = DateTime.now(),
|
||||
user = user
|
||||
)
|
||||
val user = UserForCreate(
|
||||
id = id,
|
||||
username = "$firstName-$lastName".toLowerCase(),
|
||||
password = "azerty",
|
||||
)
|
||||
val citizen = CitizenForCreate(
|
||||
id = id,
|
||||
name = CitizenI.Name(firstName, lastName),
|
||||
email = email,
|
||||
birthday = DateTime.now(),
|
||||
user = user
|
||||
)
|
||||
|
||||
return get<CitizenRepository>().insertWithUser(citizen)
|
||||
}
|
||||
return repo.insertWithUser(citizen)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user