-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add article creation feature to API and integration tests
- Loading branch information
Showing
4 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...n/kotlin/io/github/gunkim/realworld/web/api/article/model/request/CreateArticleRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.github.gunkim.realworld.web.api.article.model.request | ||
|
||
data class CreateArticleRequest( | ||
val title: String, | ||
val description: String, | ||
val body: String, | ||
val tagList: List<String> = listOf(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,31 @@ | ||
package io.github.gunkim.realworld.web.api | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import io.github.gunkim.realworld.domain.article.Article | ||
import io.github.gunkim.realworld.domain.article.service.CreateArticleService | ||
import io.github.gunkim.realworld.domain.user.model.User | ||
import io.github.gunkim.realworld.share.IntegrationTest | ||
import io.github.gunkim.realworld.web.api.article.model.request.CreateArticleRequest | ||
import io.kotest.core.annotation.Tags | ||
import io.kotest.core.spec.DisplayName | ||
import io.kotest.core.test.TestCase | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.test.web.servlet.get | ||
import org.springframework.test.web.servlet.post | ||
|
||
@Tags("Integration Test") | ||
@DisplayName("Articles Controller - Integration Test") | ||
class ArticlesControllerIntegrationTest( | ||
private val createArticleService: CreateArticleService, | ||
private val objectMapper: ObjectMapper, | ||
) : IntegrationTest() { | ||
lateinit var token: String | ||
lateinit var articles: List<Article> | ||
lateinit var author: User | ||
lateinit var authUser: User | ||
|
||
override suspend fun beforeEach(testCase: TestCase) { | ||
val (_, token) = createUser("[email protected]", "gunkim", "password") | ||
val (authUser, token) = createUser("[email protected]", "gunkim", "password") | ||
val (author, _) = createUser("[email protected]", "author gunkim", "password") | ||
val articles = listOf( | ||
createArticleService.createArticle( | ||
|
@@ -33,6 +38,7 @@ class ArticlesControllerIntegrationTest( | |
) | ||
|
||
this.token = token | ||
this.authUser = authUser | ||
this.articles = articles | ||
this.author = author | ||
} | ||
|
@@ -72,5 +78,34 @@ class ArticlesControllerIntegrationTest( | |
jsonPath("$.article.slug") { value(articles[0].slug) } | ||
}.andDo { print() } | ||
} | ||
|
||
"POST /api/articles - Create an article" { | ||
val request = CreateArticleRequest( | ||
title = "New Article", | ||
description = "New Article Description", | ||
body = "This is the body of the new article.", | ||
tagList = listOf("tag1", "tag3") | ||
) | ||
|
||
val requestBody = mapOf("article" to request) | ||
val requestJson = objectMapper.writeValueAsString(requestBody) | ||
|
||
mockMvc.post("/api/articles") { | ||
contentType = org.springframework.http.MediaType.APPLICATION_JSON | ||
header(HttpHeaders.AUTHORIZATION, token) | ||
content = requestJson | ||
}.andExpect { | ||
status { isCreated() } | ||
jsonPath("$.article.title") { value(request.title) } | ||
jsonPath("$.article.description") { value(request.description) } | ||
jsonPath("$.article.body") { value(request.body) } | ||
jsonPath("$.article.tagList[0]") { value(request.tagList[0]) } | ||
jsonPath("$.article.tagList[1]") { value(request.tagList[1]) } | ||
jsonPath("$.article.author.username") { value(authUser.name) } | ||
jsonPath("$.article.slug") { exists() } | ||
jsonPath("$.article.createdAt") { exists() } | ||
jsonPath("$.article.updatedAt") { exists() } | ||
}.andDo { print() } | ||
} | ||
} | ||
} |