Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿฆพ Refactor/#55 ์œ ์ € ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ˆ˜์ • #68

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willDoNothing;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
Expand Down Expand Up @@ -44,6 +43,7 @@
import coffeemeet.server.user.service.dto.MyProfileDto.Response;
import coffeemeet.server.user.service.dto.UserProfileDto;
import com.epages.restdocs.apispec.Schema;
import java.time.format.DateTimeFormatter;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand All @@ -61,11 +61,13 @@ class UserControllerTest extends ControllerTestConfig {
@Test
@DisplayName("ํšŒ์›๊ฐ€์ž…์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.")
void signupTest() throws Exception {
// given
SignupHttpDto.Request request = SignupDtoFixture.signupDto();
AuthTokens authTokens = new AuthTokens("accessToken", "refreshToken");

given(userService.signup(any(), any(), any(), any())).willReturn(authTokens);

// when, then
mockMvc.perform(post("/api/v1/users/sign-up")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
Expand Down Expand Up @@ -94,10 +96,12 @@ void signupTest() throws Exception {
@Test
@DisplayName("๋กœ๊ทธ์ธ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.")
void loginTest() throws Exception {
// given
AuthTokens authTokens = new AuthTokens("accessToken", "refreshToken");

given(userService.login(any(), any())).willReturn(authTokens);

// when, then
mockMvc.perform(get("/api/v1/users/login/{oAuthProvider}", OAuthProvider.KAKAO)
.contentType(MediaType.APPLICATION_JSON)
.param("authCode", "authCode"))
Expand Down Expand Up @@ -125,11 +129,13 @@ void loginTest() throws Exception {
@Test
@DisplayName("์‚ฌ์šฉ์ž ํ”„๋กœํ•„์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.")
void findUserProfileTest() throws Exception {
Long userId = 1L;
// given
long userId = 1L;
UserProfileDto.Response response = UserProfileDtoFixture.userProfileDtoResponse();

given(userService.findUserProfile(userId)).willReturn(response);

// when, then
mockMvc.perform(get("/api/v1/users/{id}", userId)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -159,15 +165,16 @@ void findUserProfileTest() throws Exception {
@Test
@DisplayName("๋งˆ์ดํŽ˜์ด์ง€๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.")
void findMyProfileTest() throws Exception {
// given
Long userId = 1L;
RefreshToken refreshToken = RefreshTokenFixture.refreshToken();

given(refreshTokenQuery.getRefreshToken(anyLong())).willReturn(refreshToken);
Response response = MyProfileDtoFixture.myProfileDtoResponse();

when(jwtTokenProvider.extractUserId(TOKEN)).thenReturn(userId);
when(userService.findMyProfile(anyLong())).thenReturn(response);
given(refreshTokenQuery.getRefreshToken(anyLong())).willReturn(refreshToken);
given(jwtTokenProvider.extractUserId(TOKEN)).willReturn(userId);
given(userService.findMyProfile(anyLong())).willReturn(response);

// when, then
mockMvc.perform(get("/api/v1/users/me")
.header("Authorization", TOKEN)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -193,12 +200,25 @@ void findMyProfileTest() throws Exception {
)
)
)
.andExpect(status().isOk());
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value(response.name()))
.andExpect(jsonPath("$.nickname").value(response.nickname()))
.andExpect(jsonPath("$.email").value(response.email()))
.andExpect(jsonPath("$.profileImageUrl").value(response.profileImageUrl()))
.andExpect(jsonPath("$.birthYear").value(response.birthYear()))
.andExpect(jsonPath("$.birthDay").value(response.birthDay()))
.andExpect(jsonPath("$.reportedCount").value(response.reportedCount()))
.andExpect(
jsonPath("$.sanctionPeriod").value(response.sanctionPeriod()
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS"))))
.andExpect(jsonPath("$.department").value(String.valueOf(response.department())))
.andExpect(jsonPath("$.interests[0]").value(response.interests().get(0).name()));
}

@Test
@DisplayName("๋ณธ์ธ ํ”„๋กœํ•„ ์‚ฌ์ง„์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.")
void updateProfileImageTest() throws Exception {
// given
Long userId = 1L;
RefreshToken refreshToken = RefreshTokenFixture.refreshToken();

Expand All @@ -210,6 +230,7 @@ void updateProfileImageTest() throws Exception {
"image/png",
"testImage".getBytes());

// when, given
mockMvc.perform((multipart("/api/v1/users/me/profile-image")
.file("profileImage", file.getBytes())
.header("Authorization", TOKEN)
Expand All @@ -232,6 +253,7 @@ void updateProfileImageTest() throws Exception {
@Test
@DisplayName("๋ณธ์ธ ํ”„๋กœํ•„ ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.")
void updateProfileInfoTest() throws Exception {
// given
Long userId = 1L;
Request request = UpdateProfileDtoFixture.updateProfileDtoRequest();
RefreshToken refreshToken = RefreshTokenFixture.refreshToken();
Expand All @@ -241,6 +263,7 @@ void updateProfileInfoTest() throws Exception {
willDoNothing().given(
userService).updateProfileInfo(any(), any(), any());

// when, given
mockMvc.perform(patch("/api/v1/users/me")
.header("Authorization", TOKEN)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -265,12 +288,14 @@ void updateProfileInfoTest() throws Exception {
@Test
@DisplayName("๋‹‰๋„ค์ž„ ์ค‘๋ณต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.")
void checkNicknameDuplicationTest() throws Exception {
// given
User user = user();
String nickname = user.getProfile().getNickname();
RefreshToken refreshToken = RefreshTokenFixture.refreshToken();

given(refreshTokenQuery.getRefreshToken(anyLong())).willReturn(refreshToken);

// when, given
mockMvc.perform(get("/api/v1/users/duplicate")
.param("nickname", nickname)
)
Expand Down