Skip to content
This repository was archived by the owner on Jul 7, 2025. It is now read-only.

Commit 0544848

Browse files
committed
test: 회원가입, 소셜로그인 컨트롤러 테스트 코드 추가
1 parent e5e0db7 commit 0544848

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.asap.app.auth.controller
2+
3+
import com.asap.app.auth.dto.SocialLoginRequest
4+
import com.fasterxml.jackson.databind.ObjectMapper
5+
import org.junit.jupiter.api.Test
6+
import org.springframework.beans.factory.annotation.Autowired
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
8+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
9+
import org.springframework.http.MediaType
10+
import org.springframework.test.web.servlet.MockMvc
11+
import org.springframework.test.web.servlet.post
12+
13+
@WebMvcTest(AuthController::class)
14+
@AutoConfigureMockMvc
15+
class AuthControllerTest {
16+
17+
@Autowired
18+
private lateinit var mockMvc: MockMvc
19+
20+
private val objectMapper: ObjectMapper = ObjectMapper()
21+
22+
23+
@Test
24+
fun socialLoginSuccessTest(){
25+
// given
26+
val request = SocialLoginRequest("registered")
27+
// when
28+
val response = mockMvc.post("/api/v1/auth/login/{provider}", "kakao") {
29+
contentType = MediaType.APPLICATION_JSON
30+
content = objectMapper.writeValueAsString(request)
31+
}
32+
33+
// then
34+
response.andExpect {
35+
status { isOk() }
36+
jsonPath("$.accessToken") {
37+
exists()
38+
isString()
39+
isNotEmpty()
40+
}
41+
jsonPath("$.refreshToken") {
42+
exists()
43+
isString()
44+
isNotEmpty()
45+
}
46+
}
47+
}
48+
49+
@Test
50+
fun socialLoginNonRegisteredTest(){
51+
// given
52+
val request = SocialLoginRequest("nonRegistered")
53+
// when
54+
val response = mockMvc.post("/api/v1/auth/login/{provider}", "kakao") {
55+
contentType = MediaType.APPLICATION_JSON
56+
content = objectMapper.writeValueAsString(request)
57+
}
58+
59+
// then
60+
response.andExpect {
61+
status { isUnauthorized() }
62+
jsonPath("$.registerToken") {
63+
exists()
64+
isString()
65+
isNotEmpty()
66+
}
67+
}
68+
}
69+
70+
71+
@Test
72+
fun socialLoginBadRequestTest(){
73+
// given
74+
val request = SocialLoginRequest("invalid")
75+
// when
76+
val response = mockMvc.post("/api/v1/auth/login/{provider}", "kakao") {
77+
contentType = MediaType.APPLICATION_JSON
78+
content = objectMapper.writeValueAsString(request)
79+
}
80+
81+
// then
82+
response.andExpect {
83+
status { isBadRequest() }
84+
}
85+
}
86+
87+
88+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.asap.app.user.controller
2+
3+
import com.asap.app.user.dto.RegisterUserRequest
4+
import com.fasterxml.jackson.databind.ObjectMapper
5+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
6+
import org.junit.jupiter.api.Test
7+
import org.springframework.beans.factory.annotation.Autowired
8+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
9+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
10+
import org.springframework.http.MediaType
11+
import org.springframework.test.web.servlet.MockMvc
12+
import org.springframework.test.web.servlet.post
13+
import java.time.LocalDate
14+
15+
@WebMvcTest(UserController::class)
16+
@AutoConfigureMockMvc
17+
class UserControllerTest {
18+
19+
@Autowired
20+
private lateinit var mockMvc: MockMvc
21+
22+
private val objectMapper: ObjectMapper = ObjectMapper().registerModules(JavaTimeModule())
23+
24+
@Test
25+
fun registerUserTest(){
26+
// given
27+
val request = RegisterUserRequest("register", true, true, true, LocalDate.now())
28+
// when
29+
val response = mockMvc.post("/api/v1/users") {
30+
contentType = MediaType.APPLICATION_JSON
31+
content = objectMapper.writeValueAsString(request)
32+
}
33+
// then
34+
response.andExpect {
35+
status { isOk() }
36+
jsonPath("$.accessToken") {
37+
exists()
38+
isString()
39+
isNotEmpty()
40+
}
41+
jsonPath("$.refreshToken") {
42+
exists()
43+
isString()
44+
isNotEmpty()
45+
}
46+
}
47+
}
48+
49+
@Test
50+
fun registerUserNotExistsRegisterTokenTest(){
51+
// given
52+
val request = RegisterUserRequest("nonExistsToken", false, true, true, LocalDate.now())
53+
// when
54+
val response = mockMvc.post("/api/v1/users") {
55+
contentType = MediaType.APPLICATION_JSON
56+
content = objectMapper.writeValueAsString(request)
57+
}
58+
// then
59+
response.andExpect {
60+
status { isBadRequest() }
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)