Skip to content

Commit

Permalink
test: 중복된 code 등록 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
GGHDMS committed Dec 17, 2023
1 parent 014a3b2 commit f098445
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/test/kotlin/com/yourssu/ssudateserver/common/BaseTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -36,6 +38,9 @@ class BaseTest {
@Autowired
protected lateinit var followRepository: FollowRepository

@Autowired
protected lateinit var codeRepository: CodeRepository

@Autowired
lateinit var tokenService: RefreshTokenService

Expand Down Expand Up @@ -88,6 +93,10 @@ class BaseTest {
)

tokenService.saveTokenInfo(oauthName = "oauthName1", "refreshToken")

codeRepository.save(
Code(fromCode = "code3", toCode = "code1", createdAt = now())
)
}

@BeforeEach
Expand All @@ -99,5 +108,6 @@ class BaseTest {
fun setDown() {
userRepository.deleteAll()
followRepository.deleteAll()
codeRepository.deleteAll()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ 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

@ActiveProfiles("test")
class RegisterCodeTest : BaseTest() {

@Autowired
lateinit var codeRepository: CodeRepository

@Test
fun registerCodeTest() {
setPrincipal("oauthName1")
Expand All @@ -32,16 +27,82 @@ 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)

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()
}
}
}

0 comments on commit f098445

Please sign in to comment.