From f09844564e6ef4cb42a27f21747b54158f7d4aa0 Mon Sep 17 00:00:00 2001 From: HeoSeokMun Date: Sun, 17 Dec 2023 15:09:46 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EC=A4=91=EB=B3=B5=EB=90=9C=20code=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yourssu/ssudateserver/common/BaseTest.kt | 10 +++ .../controller/RegisterCodeTest.kt | 81 ++++++++++++++++--- 2 files changed, 81 insertions(+), 10 deletions(-) diff --git a/src/test/kotlin/com/yourssu/ssudateserver/common/BaseTest.kt b/src/test/kotlin/com/yourssu/ssudateserver/common/BaseTest.kt index 913d8e7..1d6d4c0 100644 --- a/src/test/kotlin/com/yourssu/ssudateserver/common/BaseTest.kt +++ b/src/test/kotlin/com/yourssu/ssudateserver/common/BaseTest.kt @@ -1,6 +1,7 @@ package com.yourssu.ssudateserver.common import com.fasterxml.jackson.databind.ObjectMapper +import com.yourssu.ssudateserver.entity.Code import com.yourssu.ssudateserver.entity.Follow import com.yourssu.ssudateserver.entity.User import com.yourssu.ssudateserver.enums.Animals @@ -9,6 +10,7 @@ import com.yourssu.ssudateserver.enums.Gender import com.yourssu.ssudateserver.enums.MBTI import com.yourssu.ssudateserver.enums.MaleAnimals import com.yourssu.ssudateserver.enums.RoleType +import com.yourssu.ssudateserver.repository.CodeRepository import com.yourssu.ssudateserver.repository.FollowRepository import com.yourssu.ssudateserver.repository.UserRepository import com.yourssu.ssudateserver.service.RefreshTokenService @@ -36,6 +38,9 @@ class BaseTest { @Autowired protected lateinit var followRepository: FollowRepository + @Autowired + protected lateinit var codeRepository: CodeRepository + @Autowired lateinit var tokenService: RefreshTokenService @@ -88,6 +93,10 @@ class BaseTest { ) tokenService.saveTokenInfo(oauthName = "oauthName1", "refreshToken") + + codeRepository.save( + Code(fromCode = "code3", toCode = "code1", createdAt = now()) + ) } @BeforeEach @@ -99,5 +108,6 @@ class BaseTest { fun setDown() { userRepository.deleteAll() followRepository.deleteAll() + codeRepository.deleteAll() } } diff --git a/src/test/kotlin/com/yourssu/ssudateserver/controller/RegisterCodeTest.kt b/src/test/kotlin/com/yourssu/ssudateserver/controller/RegisterCodeTest.kt index 167fcdc..3ec11dc 100644 --- a/src/test/kotlin/com/yourssu/ssudateserver/controller/RegisterCodeTest.kt +++ b/src/test/kotlin/com/yourssu/ssudateserver/controller/RegisterCodeTest.kt @@ -3,10 +3,8 @@ package com.yourssu.ssudateserver.controller import com.yourssu.ssudateserver.common.BaseTest import com.yourssu.ssudateserver.dto.request.RegisterCodeRequestDto import com.yourssu.ssudateserver.fixture.PrincipalFixture.Companion.setPrincipal -import com.yourssu.ssudateserver.repository.CodeRepository import org.assertj.core.api.Assertions import org.junit.jupiter.api.Test -import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.MediaType import org.springframework.test.context.ActiveProfiles import org.springframework.test.web.servlet.post @@ -14,9 +12,6 @@ import org.springframework.test.web.servlet.post @ActiveProfiles("test") class RegisterCodeTest : BaseTest() { - @Autowired - lateinit var codeRepository: CodeRepository - @Test fun registerCodeTest() { setPrincipal("oauthName1") @@ -32,11 +27,10 @@ class RegisterCodeTest : BaseTest() { content = objectMapper.writeValueAsString(registerCodeRequestDto) } - test - .andExpect { - status { isOk() } - jsonPath("ticket") { value(fromUser.ticket + 1) } - } + test.andExpect { + status { isOk() } + jsonPath("ticket") { value(fromUser.ticket + 1) } + } val code = codeRepository.findByFromCodeAndToCode(fromCode = fromUser.code, toCode = toUser.code) @@ -44,4 +38,71 @@ class RegisterCodeTest : BaseTest() { Assertions.assertThat(code).isNotNull Assertions.assertThat(userRepository.findByOauthName("oauthName2")!!.ticket).isEqualTo(previous + 1) } + + @Test + fun registerCodeFailMyCodeTest() { + setPrincipal("oauthName1") + + val registerCodeRequestDto = RegisterCodeRequestDto("code1") + + val test = mockMvc.post("/register/code") { + contentType = MediaType.APPLICATION_JSON + content = objectMapper.writeValueAsString(registerCodeRequestDto) + } + + test.andExpect { + status { isBadRequest() } + jsonPath("message") { value("내 code는 등록할 수 없습니다.") } + } + test.andDo { + print() + } + } + + @Test + fun registerCodeFailUsedCodeTest() { + setPrincipal("oauthName1") + + val registerCodeRequestDto = RegisterCodeRequestDto("code2") + + mockMvc.post("/register/code") { + contentType = MediaType.APPLICATION_JSON + content = objectMapper.writeValueAsString(registerCodeRequestDto) + } + + val test = mockMvc.post("/register/code") { + contentType = MediaType.APPLICATION_JSON + content = objectMapper.writeValueAsString(registerCodeRequestDto) + } + + test.andExpect { + status { isBadRequest() } + jsonPath("message") { value("이미 등록한 친구 code입니다.") } + } + + test.andDo { + print() + } + } + + @Test + fun registerCodeFailFriendUseMyCodeTest() { + setPrincipal("oauthName1") + + val registerCodeRequestDto = RegisterCodeRequestDto("code3") + + val test = mockMvc.post("/register/code") { + contentType = MediaType.APPLICATION_JSON + content = objectMapper.writeValueAsString(registerCodeRequestDto) + } + + test.andExpect { + status { isBadRequest() } + jsonPath("message") { value("친구가 이미 당신의 code를 등록했습니다.") } + } + + test.andDo { + print() + } + } }