Skip to content

Commit

Permalink
test: userId 토큰으로 넘기도록 테스트 수정 및 유지보수 불가 test 비활성화
Browse files Browse the repository at this point in the history
  • Loading branch information
kkanggu committed Dec 10, 2023
1 parent cf8eae0 commit d681dd1
Show file tree
Hide file tree
Showing 14 changed files with 458 additions and 479 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.example.daitssuapi.domain.main.controller
package com.example.daitssuapi.domain.article.controller

import com.example.daitssuapi.common.enums.ErrorCode
import com.example.daitssuapi.domain.main.dto.request.CommentWriteRequest
import com.example.daitssuapi.domain.main.model.repository.ArticleRepository
import com.example.daitssuapi.domain.main.model.repository.CommentRepository
import com.example.daitssuapi.domain.main.model.repository.ScrapRepository
import com.example.daitssuapi.domain.main.model.repository.UserRepository
import com.example.daitssuapi.common.security.component.TokenProvider
import com.example.daitssuapi.domain.article.dto.request.CommentWriteRequest
import com.example.daitssuapi.domain.article.model.repository.ArticleRepository
import com.example.daitssuapi.domain.article.model.repository.CommentRepository
import com.example.daitssuapi.domain.article.model.repository.ScrapRepository
import com.example.daitssuapi.domain.user.model.repository.UserRepository
import com.example.daitssuapi.utils.ControllerTest
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.junit.jupiter.api.DisplayName
Expand All @@ -23,7 +24,8 @@ class ArticleControllerTest(
private val articleRepository: ArticleRepository,
private val commentRepository: CommentRepository,
private val scrapRepository: ScrapRepository,
private val userRepository: UserRepository
private val userRepository: UserRepository,
private val tokenProvider: TokenProvider
) {
@Autowired
private lateinit var mockMvc: MockMvc
Expand Down Expand Up @@ -55,15 +57,15 @@ class ArticleControllerTest(
val url = "/community/article/${article.id}/comments"
val articleWriteRequest = CommentWriteRequest(
content = "댓글 추가",
userId = user.id,
originalCommentId = null
)
val request = jacksonObjectMapper().writeValueAsString(articleWriteRequest)
val accessToken = tokenProvider.createAccessToken(id = user.id).token

mockMvc.perform(
post(url)
.content(request)
post(url).content(request)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
).andExpect(status().isOk)
}

Expand All @@ -74,15 +76,15 @@ class ArticleControllerTest(
val url = "/community/article/0/comments"
val articleWriteRequest = CommentWriteRequest(
content = "댓글 추가",
userId = user.id,
originalCommentId = null
)
val request = jacksonObjectMapper().writeValueAsString(articleWriteRequest)
val accessToken = tokenProvider.createAccessToken(id = user.id).token

mockMvc.perform(
post(url)
.content(request)
.contentType(MediaType.APPLICATION_JSON)
mockMvc.perform(post(url)
.content(request)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
).andExpect(status().isBadRequest)
.andExpect(jsonPath("$.code").value(ErrorCode.ARTICLE_NOT_FOUND.code))
}
Expand All @@ -94,15 +96,15 @@ class ArticleControllerTest(
val url = "/community/article/${article.id}/comments"
val articleWriteRequest = CommentWriteRequest(
content = "댓글 추가",
userId = 0,
originalCommentId = null
)
val request = jacksonObjectMapper().writeValueAsString(articleWriteRequest)
val accessToken = tokenProvider.createAccessToken(id = 0).token

mockMvc.perform(
post(url)
.content(request)
post(url).content(request)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
).andExpect(status().isBadRequest)
.andExpect(jsonPath("$.code").value(ErrorCode.USER_NOT_FOUND.code))
}
Expand All @@ -122,15 +124,15 @@ class ArticleControllerTest(
512Byte 넘기기 위해 대충 반복512Byte 넘기기 위해 대충 반복512Byte 넘기기 위해 대충 반복
512Byte 넘기기 위해 대충 반복512Byte 넘기기 위해 대충 반복512Byte 넘기기 위해 대충 반복
""".trimIndent(),
userId = user.id,
originalCommentId = null
)
val request = jacksonObjectMapper().writeValueAsString(articleWriteRequest)
val accessToken = tokenProvider.createAccessToken(id = user.id).token

mockMvc.perform(
post(url)
.content(request)
.contentType(MediaType.APPLICATION_JSON)
mockMvc.perform(post(url)
.content(request)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
).andExpect(status().isBadRequest)
.andExpect(jsonPath("$.code").value(ErrorCode.COMMENT_TOO_LONG.code))
}
Expand All @@ -143,15 +145,15 @@ class ArticleControllerTest(
val url = "/community/article/${article.id}/comments"
val articleWriteRequest = CommentWriteRequest(
content = "대충 댓글 내용",
userId = user.id,
originalCommentId = 0
)
val request = jacksonObjectMapper().writeValueAsString(articleWriteRequest)
val accessToken = tokenProvider.createAccessToken(id = user.id).token

mockMvc.perform(
post(url)
.content(request)
.contentType(MediaType.APPLICATION_JSON)
mockMvc.perform(post(url)
.content(request)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
).andExpect(status().isBadRequest)
.andExpect(jsonPath("$.code").value(ErrorCode.COMMENT_NOT_FOUND.code))
}
Expand All @@ -167,15 +169,15 @@ class ArticleControllerTest(
val url = "/community/article/${article.id}/comments"
val articleWriteRequest = CommentWriteRequest(
content = "대충 댓글 내용",
userId = user.id,
originalCommentId = originalComment.id
)
val request = jacksonObjectMapper().writeValueAsString(articleWriteRequest)
val accessToken = tokenProvider.createAccessToken(id = user.id).token

mockMvc.perform(
post(url)
.content(request)
.contentType(MediaType.APPLICATION_JSON)
mockMvc.perform(post(url)
.content(request)
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
).andExpect(status().isBadRequest)
.andExpect(jsonPath("$.code").value(ErrorCode.DIFFERENT_ARTICLE.code))
}
Expand All @@ -186,17 +188,19 @@ class ArticleControllerTest(
val articleId = commentRepository.findAll().filter { null != it.article }[0].article!!.id
val url = "/community/article/${articleId}/comments"

mockMvc.perform(get(url))
.andExpect(status().isOk)
mockMvc.perform(get(url)
.header(HttpHeaders.AUTHORIZATION, "Bearer test")
).andExpect(status().isOk)
}

@Test
@DisplayName("실패_게시글을 못 찾을 때_댓글 조회에 실패한다")
fun getCommentFailNoArticle() {
val url = "/community/article/0/comments"

mockMvc.perform(get(url))
.andExpect(status().isBadRequest)
mockMvc.perform(get(url)
.header(HttpHeaders.AUTHORIZATION, "Bearer test")
).andExpect(status().isBadRequest)
.andExpect(jsonPath("$.code").value(ErrorCode.ARTICLE_NOT_FOUND.code))
}

Expand All @@ -206,10 +210,12 @@ class ArticleControllerTest(
val userId = userRepository.findAll().sortedByDescending { it.id }[0].id
val articleId = commentRepository.findAll().filter { null != it.article }[0].id
val url = "/community/article/${articleId}/scrap"
val accessToken = tokenProvider.createAccessToken(id = userId).token

mockMvc.perform(post(url)
.param("userId", userId.toString())
.param("isActive", true.toString())
.header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
).andExpect(status().isOk)
}

Expand All @@ -219,23 +225,27 @@ class ArticleControllerTest(
val userId = userRepository.findAll().sortedByDescending { it.id }[0].id
val articleId = commentRepository.findAll().filter { null != it.article }[0].id
val url = "/community/article/${articleId}/scrap"
val accessToken = tokenProvider.createAccessToken(id = userId).token

mockMvc.perform(post(url)
.param("userId", userId.toString())
.param("isActive", false.toString())
.header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
).andExpect(status().isBadRequest)
}

@Test
@DisplayName("실패_신규 스크랩에서 유저를 못 찾으면_스크랩에 실패한다")
fun newScrapFailNoUser() {
val userId = 0
val userId = 0L
val articleId = commentRepository.findAll().filter { null != it.article }[0].id
val url = "/community/article/${articleId}/scrap"
val accessToken = tokenProvider.createAccessToken(id = userId).token

mockMvc.perform(post(url)
.param("userId", userId.toString())
.param("isActive", false.toString())
.header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
).andExpect(status().isBadRequest)
}

Expand All @@ -245,10 +255,12 @@ class ArticleControllerTest(
val userId = userRepository.findAll()[0].id
val articleId = 0
val url = "/community/article/${articleId}/scrap"
val accessToken = tokenProvider.createAccessToken(id = userId).token

mockMvc.perform(post(url)
.param("userId", userId.toString())
.param("isActive", false.toString())
.header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
).andExpect(status().isBadRequest)
}

Expand All @@ -257,10 +269,11 @@ class ArticleControllerTest(
fun editScrapSuccess() {
val scrap = scrapRepository.findAll()[0]
val url = "/community/article/${scrap.article.id}/scrap"
val accessToken = tokenProvider.createAccessToken(id = scrap.user.id).token

mockMvc.perform(post(url)
.param("userId", scrap.user.id.toString())
.param("isActive", (!scrap.isActive).toString())
.header(HttpHeaders.AUTHORIZATION, "Bearer $accessToken")
).andExpect(status().isOk)
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.example.daitssuapi.domain.main.service
package com.example.daitssuapi.domain.article.service

import com.example.daitssuapi.common.exception.DefaultException
import com.example.daitssuapi.domain.main.dto.request.ArticleCreateRequest
import com.example.daitssuapi.domain.main.dto.request.CommentWriteRequest
import com.example.daitssuapi.domain.main.dto.response.ArticleResponse
import com.example.daitssuapi.domain.main.enums.Topic
import com.example.daitssuapi.domain.main.model.entity.Article
import com.example.daitssuapi.domain.main.model.repository.ArticleRepository
import com.example.daitssuapi.domain.main.model.repository.CommentRepository
import com.example.daitssuapi.domain.main.model.repository.ScrapRepository
import com.example.daitssuapi.domain.main.model.repository.UserRepository
import com.example.daitssuapi.domain.article.dto.request.ArticleCreateRequest
import com.example.daitssuapi.domain.article.dto.request.CommentWriteRequest
import com.example.daitssuapi.domain.article.dto.response.ArticleResponse
import com.example.daitssuapi.domain.article.enums.Topic
import com.example.daitssuapi.domain.article.model.entity.Article
import com.example.daitssuapi.domain.article.model.repository.ArticleRepository
import com.example.daitssuapi.domain.article.model.repository.CommentRepository
import com.example.daitssuapi.domain.article.model.repository.ScrapRepository
import com.example.daitssuapi.domain.user.model.repository.UserRepository
import com.example.daitssuapi.utils.IntegrationTest
import org.assertj.core.api.Assertions.*
import org.junit.jupiter.api.Assertions.assertEquals
Expand All @@ -34,14 +34,13 @@ class ArticleServiceTest(

// when
val articleCreateRequest = ArticleCreateRequest(
userId = user.id,
topic = Topic.CHAT,
title = "테스트 제목",
content = "테스트 내용",
images = emptyList()
)

articleService.createArticle(articleCreateRequest)
articleService.createArticle(articleCreateRequest = articleCreateRequest, userId = user.id)
}

@Test
Expand Down Expand Up @@ -75,11 +74,10 @@ class ArticleServiceTest(
val user = userRepository.findAll()[0]
val request = CommentWriteRequest(
content = "댓글 추가",
userId = user.id,
originalCommentId = null
)

val response = articleService.writeComment(articleId = article.id, request = request)
val response = articleService.writeComment(articleId = article.id, request = request, userId = user.id)

assertAll(
{ assertThat(response.userId).isEqualTo(user.id) },
Expand All @@ -94,11 +92,10 @@ class ArticleServiceTest(
val user = userRepository.findAll()[0]
val request = CommentWriteRequest(
content = "댓글 추가",
userId = user.id,
originalCommentId = null
)

assertThrows<DefaultException> { articleService.writeComment(articleId = 0, request = request) }
assertThrows<DefaultException> { articleService.writeComment(articleId = 0, request = request, userId = user.id) }
}

@Test
Expand All @@ -107,11 +104,10 @@ class ArticleServiceTest(
val article = articleRepository.findAll()[0]
val request = CommentWriteRequest(
content = "댓글 추가",
userId = 0,
originalCommentId = null
)

assertThrows<DefaultException> { articleService.writeComment(articleId = article.id, request = request) }
assertThrows<DefaultException> { articleService.writeComment(articleId = article.id, request = request, userId = 0L) }
}

@Test
Expand All @@ -128,11 +124,10 @@ class ArticleServiceTest(
512Byte 넘기기 위해 대충 반복512Byte 넘기기 위해 대충 반복512Byte 넘기기 위해 대충 반복
512Byte 넘기기 위해 대충 반복512Byte 넘기기 위해 대충 반복512Byte 넘기기 위해 대충 반복
""".trimIndent(),
userId = user.id,
originalCommentId = null
)

assertThrows<DefaultException> { articleService.writeComment(articleId = article.id, request = request) }
assertThrows<DefaultException> { articleService.writeComment(articleId = article.id, request = request, userId = user.id) }
}

@Test
Expand All @@ -142,11 +137,10 @@ class ArticleServiceTest(
val user = userRepository.findAll()[0]
val request = CommentWriteRequest(
content = "대충 댓글 내용",
userId = user.id,
originalCommentId = 0
)

assertThrows<DefaultException> { articleService.writeComment(articleId = article.id, request = request) }
assertThrows<DefaultException> { articleService.writeComment(articleId = article.id, request = request, userId = user.id) }
}

@Test
Expand All @@ -159,11 +153,10 @@ class ArticleServiceTest(
val user = userRepository.findAll()[0]
val request = CommentWriteRequest(
content = "대충 댓글 내용",
userId = user.id,
originalCommentId = originalComment.id
)

assertThrows<DefaultException> { articleService.writeComment(articleId = article.id, request = request) }
assertThrows<DefaultException> { articleService.writeComment(articleId = article.id, request = request, userId = user.id) }
}

@Test
Expand Down
Loading

0 comments on commit d681dd1

Please sign in to comment.