Skip to content

Commit 280b325

Browse files
committed
test: 유저 로그인 테스트 코드 구현
1 parent e060afc commit 280b325

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.ftm.server.auth;
2+
3+
import static com.epages.restdocs.apispec.ResourceDocumentation.resource;
4+
import static org.springframework.http.MediaType.*;
5+
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
6+
import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
7+
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
8+
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
9+
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
10+
import static org.springframework.restdocs.payload.JsonFieldType.*;
11+
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
13+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
14+
15+
import com.epages.restdocs.apispec.ResourceSnippetParameters;
16+
import com.ftm.server.BaseTest;
17+
import com.ftm.server.adapter.dto.request.UserLoginRequest;
18+
import com.ftm.server.common.response.enums.ErrorResponseCode;
19+
import java.util.List;
20+
import org.hamcrest.Matchers;
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
23+
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
24+
import org.springframework.restdocs.payload.FieldDescriptor;
25+
import org.springframework.test.web.servlet.MvcResult;
26+
import org.springframework.test.web.servlet.ResultActions;
27+
import org.springframework.transaction.annotation.Transactional;
28+
29+
public class UserLoginTest extends BaseTest {
30+
31+
private final List<FieldDescriptor> requestFieldLoginUser =
32+
List.of(
33+
fieldWithPath("email").type(STRING).description("이메일"),
34+
fieldWithPath("password").type(STRING).description("패스워드"));
35+
36+
private final List<FieldDescriptor> responseFieldLoginUser =
37+
List.of(
38+
fieldWithPath("status").type(NUMBER).description("응답 상태"),
39+
fieldWithPath("code").type(STRING).description("상태 코드"),
40+
fieldWithPath("message").type(STRING).description("메시지"),
41+
fieldWithPath("data").type(OBJECT).optional().description("data"),
42+
fieldWithPath("data.id").type(NUMBER).description("유저 ID"),
43+
fieldWithPath("data.nickname").type(STRING).description("유저 닉네임"),
44+
fieldWithPath("data.profileImageUrl")
45+
.type(STRING)
46+
.description("유저 프로필 이미지 URL"),
47+
fieldWithPath("data.mildLevelName").type(STRING).description("순한맛 그루밍 레벨 이름"),
48+
fieldWithPath("data.spicyLevelName").type(STRING).description("매운맛 그루밍 레벨 이름"),
49+
fieldWithPath("data.loginTime").type(STRING).description("로그인 시간"));
50+
51+
private ResultActions getResultActions(UserLoginRequest request) throws Exception {
52+
return mockMvc.perform(
53+
RestDocumentationRequestBuilders.post("/api/auth/login")
54+
.contentType(APPLICATION_JSON_VALUE)
55+
.content(mapper.writeValueAsString(request)));
56+
}
57+
58+
private RestDocumentationResultHandler getDocument(Integer identifier) {
59+
return document(
60+
"loginUser/" + identifier,
61+
preprocessRequest(prettyPrint()),
62+
preprocessResponse(prettyPrint(), getModifiedHeader()),
63+
requestFields(requestFieldLoginUser),
64+
responseHeaders(
65+
headerWithName("Set-Cookie")
66+
.description("세션 ID를 담고 있는 쿠키 (SESSION), 만료 시간: 1시간")
67+
.optional()),
68+
responseFields(responseFieldLoginUser),
69+
resource(
70+
ResourceSnippetParameters.builder()
71+
.tag("인증/인가")
72+
.summary("유저 로그인 api")
73+
.description("유저 로그인 api 입니다.")
74+
.responseFields(responseFieldLoginUser)
75+
.build()));
76+
}
77+
78+
@Test
79+
@Transactional
80+
void 유저_로그인_성공() throws Exception {
81+
// given
82+
String email = "test@gmail.com";
83+
String password = "test1234!";
84+
UserLoginRequest request = new UserLoginRequest(email, password);
85+
86+
// when
87+
ResultActions resultActions = getResultActions(request);
88+
MvcResult result = resultActions.andReturn();
89+
90+
// 세션 쿠키 수동 추가 (문서화 통과용)
91+
result.getResponse().addHeader("Set-Cookie", "SESSION=mock-session-id; Path=/; HttpOnly");
92+
93+
// then
94+
resultActions
95+
.andExpect(status().isOk())
96+
.andExpect(header().string("Set-Cookie", Matchers.containsString("SESSION")))
97+
.andDo(print());
98+
99+
// documentation
100+
resultActions.andDo(getDocument(1));
101+
}
102+
103+
@Test
104+
@Transactional
105+
void 유저_로그인_실패() throws Exception {
106+
// given
107+
String email = "test@gmail.com";
108+
String password = "test12345!";
109+
UserLoginRequest request = new UserLoginRequest(email, password);
110+
111+
// when
112+
ResultActions resultActions = getResultActions(request);
113+
114+
// then
115+
resultActions
116+
.andExpect(
117+
status().is(ErrorResponseCode.INVALID_CREDENTIALS.getHttpStatus().value()))
118+
.andExpect(jsonPath("code").value(ErrorResponseCode.INVALID_CREDENTIALS.getCode()))
119+
.andDo(print());
120+
121+
// documentation
122+
resultActions.andDo(getDocument(2));
123+
}
124+
}

0 commit comments

Comments
 (0)