-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserControllerTest.java
More file actions
234 lines (215 loc) · 13.3 KB
/
UserControllerTest.java
File metadata and controls
234 lines (215 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package dev.steady.user.controller;
import com.epages.restdocs.apispec.Schema;
import dev.steady.auth.domain.Platform;
import dev.steady.global.auth.Authentication;
import dev.steady.global.config.ControllerTestConfig;
import dev.steady.user.dto.response.UserNicknameExistResponse;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document;
import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.resourceDetails;
import static dev.steady.auth.domain.Platform.KAKAO;
import static dev.steady.auth.fixture.OAuthFixture.createAuthCodeRequestUrl;
import static dev.steady.global.auth.AuthFixture.createUserInfo;
import static dev.steady.user.fixture.UserFixtures.createUserCreateRequest;
import static dev.steady.user.fixture.UserFixtures.createUserMyDetailResponse;
import static dev.steady.user.fixture.UserFixtures.createUserOtherDetailResponse;
import static dev.steady.user.fixture.UserFixtures.createUserUpdateRequest;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willDoNothing;
import static org.mockito.Mockito.when;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.payload.JsonFieldType.ARRAY;
import static org.springframework.restdocs.payload.JsonFieldType.BOOLEAN;
import static org.springframework.restdocs.payload.JsonFieldType.NUMBER;
import static org.springframework.restdocs.payload.JsonFieldType.STRING;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseBody;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.queryParameters;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
class UserControllerTest extends ControllerTestConfig {
@Test
@DisplayName("Account가 존재하면 User를 등록하고 OAuth 인가코드 요청 URL을 반환할 수 있다.")
void createUser() throws Exception {
// given
var request = createUserCreateRequest();
var authCodeRequestUrl = createAuthCodeRequestUrl();
given(userService.createUser(request)).willReturn(1L);
given(accountService.registerUser(request.accountId(), 1L)).willReturn(KAKAO);
// when
when(oAuthService.getAuthCodeRequestUrlProvider(KAKAO)).thenReturn(authCodeRequestUrl);
// then
mockMvc.perform(post("/api/v1/user/profile")
.contentType(APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request))
)
.andExpect(status().isCreated())
.andDo(document("user-v1-create",
resourceDetails().tag("사용자").description("유저 프로필 생성")
.requestSchema(Schema.schema("UserCreateResponse")),
requestFields(
fieldWithPath("accountId").type(NUMBER).description("계정 id"),
fieldWithPath("nickname").type(STRING).description("사용자 닉네임"),
fieldWithPath("positionId").type(NUMBER).description("사용자 포지션"),
fieldWithPath("stacksId").type(ARRAY).description("사용자 기술 스택")
)
))
.andExpect(header().string("Location", authCodeRequestUrl.toString()));
}
@Test
@DisplayName("이미 존재하는 nickname에 대해서 true를 반환할 수 있다.")
void existsByNickname() throws Exception {
// given
var nickname = "닉네임";
var response = new UserNicknameExistResponse(true);
given(userService.existsByNickname(nickname)).willReturn(response);
// when, then
mockMvc.perform(get("/api/v1/user/profile/exist")
.queryParam("nickname", nickname))
.andDo(document("user-v1-check-nickname", resourceDetails().tag("사용자")
.description("유저 닉네임 중복 검사")
.responseSchema(Schema.schema("UserCheckNicknameResponse")),
queryParameters(
parameterWithName("nickname").description("닉네임")
),
responseBody()
))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.content().string(objectMapper.writeValueAsString(response)));
}
@ParameterizedTest
@EnumSource(Platform.class)
@DisplayName("내 프로필을 조회할 수 있다.")
void getMyUserDetail(Platform platform) throws Exception {
// given
var userId = 1L;
var userInfo = createUserInfo(userId);
var auth = new Authentication(userId);
var response = createUserMyDetailResponse(platform);
given(jwtResolver.getAuthentication(TOKEN)).willReturn(auth);
given(userService.getMyUserDetail(userInfo)).willReturn(response);
// when, then
mockMvc.perform(get("/api/v1/user/profile")
.header(AUTHORIZATION, TOKEN)
)
.andDo(document("user-v1-get-myProfile",
resourceDetails().tag("사용자").description("내 프로필 조회")
.responseSchema(Schema.schema("UserMyDetailResponse")),
requestHeaders(
headerWithName(AUTHORIZATION).description("토큰")
),
responseFields(
fieldWithPath("platform").type(STRING).description("소셜 계정 플랫폼"),
fieldWithPath("userId").type(NUMBER).description("사용자 식별자"),
fieldWithPath("profileImage").type(STRING).description("사용자 프로필 이미지 URL"),
fieldWithPath("nickname").type(STRING).description("사용자 닉네임"),
fieldWithPath("bio").type(STRING).description("사용자 한 줄 소개"),
fieldWithPath("position.id").type(NUMBER).description("관심 포지션 식별자"),
fieldWithPath("position.name").type(STRING).description("관심 포지션 이름"),
fieldWithPath("stacks[].id").type(NUMBER).description("관심 스택 식별자"),
fieldWithPath("stacks[].name").type(STRING).description("관심 스택 이름"),
fieldWithPath("stacks[].imageUrl").type(STRING).description("관심 스택 이미지 URL")
)
)).andExpect(status().isOk())
.andExpect(content().string(objectMapper.writeValueAsString(response)));
}
@Test
@DisplayName("타인의 프로필을 조회할 수 있다.")
void getOtherUserDetail() throws Exception {
// given
var userId = 2L;
var response = createUserOtherDetailResponse();
given(userService.getOtherUserDetail(userId)).willReturn(response);
// when, then
mockMvc.perform(get("/api/v1/user/{userId}/profile", userId)
.contentType(APPLICATION_JSON)
)
.andDo(document("user-v1-get-otherProfile",
resourceDetails().tag("사용자").description("타인의 프로필 조회")
.responseSchema(Schema.schema("UserOtherDetailResponse")),
responseFields(
fieldWithPath("user.userId").type(NUMBER).description("사용자 식별자"),
fieldWithPath("user.profileImage").type(STRING).description("사용자 프로필 이미지 URL"),
fieldWithPath("user.nickname").type(STRING).description("사용자 닉네임"),
fieldWithPath("user.bio").type(STRING).description("사용자 한 줄 소개"),
fieldWithPath("user.position.id").type(NUMBER).description("관심 포지션 식별자"),
fieldWithPath("user.position.name").type(STRING).description("관심 포지션 이름"),
fieldWithPath("user.stacks[].id").type(NUMBER).description("관심 스택 식별자"),
fieldWithPath("user.stacks[].name").type(STRING).description("관심 스택 이름"),
fieldWithPath("user.stacks[].imageUrl").type(STRING).description("관심 스택 이미지 URL"),
fieldWithPath("userCards[].cardId").type(NUMBER).description("카드 식별자"),
fieldWithPath("userCards[].imageUrl").type(STRING).description("카드 이미지 URL"),
fieldWithPath("userCards[].count").type(NUMBER).description("사용자가 받은 카드 개수"),
fieldWithPath("reviews").type(ARRAY).description("사용자가 받은 리뷰 코멘트"),
fieldWithPath("isDeleted").type(BOOLEAN).description("탈퇴 유저 여부")
)
)).andExpect(status().isOk())
.andExpect(content().string(objectMapper.writeValueAsString(response)));
}
@Test
@DisplayName("내 프로필 정보를 수정할 수 있다.")
void updateUserDetail() throws Exception {
// given
var userId = 1L;
var userInfo = createUserInfo(userId);
var auth = new Authentication(userId);
given(jwtResolver.getAuthentication(TOKEN)).willReturn(auth);
var request = createUserUpdateRequest();
willDoNothing().given(userService).updateUser(request, userInfo);
// when, then
mockMvc.perform(patch("/api/v1/user/profile")
.contentType(APPLICATION_JSON)
.header(AUTHORIZATION, TOKEN)
.content(objectMapper.writeValueAsString(request))
)
.andDo(document("user-v1-update",
resourceDetails().tag("사용자").description("유저 프로필 수정")
.requestSchema(Schema.schema("UserUpdateRequest")),
requestHeaders(
headerWithName(AUTHORIZATION).description("토큰")
),
requestFields(
fieldWithPath("profileImage").type(STRING).description("사용자 프로필 이미지 URL"),
fieldWithPath("nickname").type(STRING).description("사용자 닉네임"),
fieldWithPath("bio").type(STRING).description("사용자 한 줄 소개"),
fieldWithPath("positionId").type(NUMBER).description("사용자 포지션"),
fieldWithPath("stacksId").type(ARRAY).description("사용자 기술 스택")
)
))
.andExpect(status().isOk());
}
@Test
@DisplayName("회원탈퇴를 진행할 수 있다.")
void withdrawTest() throws Exception {
// given
var userId = 1L;
var userInfo = createUserInfo(userId);
var authentication = new Authentication(userId);
given(jwtResolver.getAuthentication(TOKEN)).willReturn(authentication);
willDoNothing().given(userService).withdrawUser(userInfo);
// when, then
mockMvc.perform(delete("/api/v1/user")
.header(AUTHORIZATION, TOKEN))
.andDo(document("user-v1-withdraw", resourceDetails().tag("사용자")
.description("유저 탈퇴"),
requestHeaders(
headerWithName(AUTHORIZATION).description("토큰")
)
)).andExpect(status().isNoContent());
}
}