Skip to content

Commit

Permalink
test: 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
GGHDMS committed Dec 16, 2023
1 parent 84c2ae8 commit 014a3b2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class BaseTest {
mbti = mbti,
nickName = "testNick${it + 1}",
oauthName = "oauthName${it + 1}",
introduction = "Introduction${it + 1}",
introduce = "Introduction${it + 1}",
contact = "Contact${it + 1}",
weight = it % 2,
createdAt = currentDateTime.plusSeconds(it.toLong()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.yourssu.ssudateserver.controller
import com.yourssu.ssudateserver.common.BaseTest
import com.yourssu.ssudateserver.dto.request.ContactRequestDto
import com.yourssu.ssudateserver.fixture.PrincipalFixture.Companion.setPrincipal
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.http.MediaType
import org.springframework.test.context.ActiveProfiles
Expand All @@ -16,23 +16,49 @@ class ContactTest : BaseTest() {
fun contactTest() {
setPrincipal()
val requestDto = ContactRequestDto(
nickName = "testNick1",
nickName = "testNick2",
)

val user = userRepository.findByNickName("testNick2")!!
val previousWeight = user.weight
println(previousWeight)

val test = mockMvc.post("/contact") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(requestDto)
}
test.andExpect {
status { isOk() }
jsonPath("nickName") { value("testNick1") }
jsonPath("contact") { value("Contact1") }
jsonPath("nickName") { value("testNick2") }
jsonPath("contact") { value("Contact2") }
}
test.andDo {
print()
}

Assertions.assertThat(userRepository.findByNickName("testNick1")!!.weight).isEqualTo(1)
assertThat(userRepository.findByNickName("testNick2")!!.weight).isEqualTo(previousWeight + 1)
}

@Test
fun contactTestFailSelfContact() {
setPrincipal()

val requestDto = ContactRequestDto(
nickName = "testNick1",
)

val user = userRepository.findByNickName("testNick1")!!

val test = mockMvc.post("/contact") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(requestDto)
}
test.andExpect {
jsonPath("message") { value("본인의 nickName으로 Contact할 수 없습니다.") }
}
test.andDo {
print()
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import com.yourssu.ssudateserver.dto.request.RefreshTokenRequestDto
import com.yourssu.ssudateserver.fixture.PrincipalFixture.Companion.setPrincipal
import org.junit.jupiter.api.Test
import org.springframework.http.MediaType
import org.springframework.security.test.context.support.WithMockUser
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.web.servlet.post

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

@Test
@WithMockUser
fun refreshTokenTest() {
setPrincipal()

Expand All @@ -31,8 +29,8 @@ class RefreshTokenTest : BaseTest() {
jsonPath("refreshToken") { exists() }
}
}

@Test
@WithMockUser
fun refreshTokenNotFoundTest() {
setPrincipal("oauthName2")

Expand All @@ -51,7 +49,6 @@ class RefreshTokenTest : BaseTest() {
}

@Test
@WithMockUser
fun invalidRefreshTokenTest() {
setPrincipal()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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")

val registerCodeRequestDto = RegisterCodeRequestDto("code2")

val fromUser = userRepository.findByOauthName("oauthName1")!!
val toUser = userRepository.findByOauthName("oauthName2")!!
val previous = toUser.ticket

val test = mockMvc.post("/register/code") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(registerCodeRequestDto)
}

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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,11 @@ class SearchTest : BaseTest() {
jsonPath("animals") { exists() }
jsonPath("mbti") { exists() }
jsonPath("nickName") { exists() }
jsonPath("introduction") { exists() }
jsonPath("introduce") { exists() }
jsonPath("contact") { exists() }
jsonPath("weight") { exists() }
jsonPath("ticket") { exists() }
jsonPath("code") { exists() }

}
test.andDo {
print()
Expand Down

0 comments on commit 014a3b2

Please sign in to comment.