|
| 1 | +package apptive.team5.user.controller; |
| 2 | + |
| 3 | +import apptive.team5.global.exception.ExceptionCode; |
| 4 | +import apptive.team5.jwt.TokenType; |
| 5 | +import apptive.team5.jwt.component.JWTUtil; |
| 6 | +import apptive.team5.user.domain.UserEntity; |
| 7 | +import apptive.team5.user.dto.UserResponse; |
| 8 | +import apptive.team5.user.repository.UserRepository; |
| 9 | +import apptive.team5.util.TestUtil; |
| 10 | +import apptive.team5.util.mockuser.WithCustomMockUser; |
| 11 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | +import org.assertj.core.api.SoftAssertions; |
| 13 | +import org.junit.jupiter.api.DisplayName; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 17 | +import org.springframework.boot.test.context.SpringBootTest; |
| 18 | +import org.springframework.http.HttpStatus; |
| 19 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 20 | +import org.springframework.test.web.servlet.MockMvc; |
| 21 | +import org.springframework.test.web.servlet.assertj.MockMvcTester; |
| 22 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 23 | +import org.springframework.transaction.annotation.Transactional; |
| 24 | + |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import static org.assertj.core.api.SoftAssertions.*; |
| 28 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 29 | + |
| 30 | + |
| 31 | +@SpringBootTest |
| 32 | +@AutoConfigureMockMvc |
| 33 | +@Transactional |
| 34 | +class UserControllerTest { |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private MockMvc mockMvc; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private UserRepository userRepository; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private JWTUtil jwtUtil; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private ObjectMapper objectMapper; |
| 47 | + |
| 48 | + |
| 49 | + @DisplayName("회원 정보 조회 성공") |
| 50 | + @Test |
| 51 | + @WithCustomMockUser(identifier = TestUtil.userIdentifier) |
| 52 | + void getMyInfoSuccess() throws Exception { |
| 53 | + |
| 54 | + UserEntity user = TestUtil.makeUserEntity(); |
| 55 | + userRepository.save(user); |
| 56 | + |
| 57 | + String response = mockMvc.perform(get("/api/users/my")) |
| 58 | + .andReturn().getResponse().getContentAsString(); |
| 59 | + |
| 60 | + UserResponse userResponse = objectMapper.readValue(response, UserResponse.class); |
| 61 | + |
| 62 | + assertSoftly(softly -> { |
| 63 | + softly.assertThat(userResponse.email()).isEqualTo(user.getEmail()); |
| 64 | + softly.assertThat(userResponse.identifier()).isEqualTo(user.getIdentifier()); |
| 65 | + softly.assertThat(userResponse.socialType()).isEqualTo(user.getSocialType()); |
| 66 | + softly.assertThat(userResponse.userRoleType()).isEqualTo(user.getRoleType()); |
| 67 | + softly.assertThat(userResponse.profileImageUrl()).isEqualTo(user.getProfileImageUrl()); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + @DisplayName("회원 정보 조회 실패 - 존재하지 않는 회원") |
| 72 | + @Test |
| 73 | + @WithCustomMockUser |
| 74 | + void getMyInfoFail() throws Exception { |
| 75 | + |
| 76 | + |
| 77 | + MockHttpServletResponse response = mockMvc.perform(get("/api/users/my")) |
| 78 | + .andReturn().getResponse(); |
| 79 | + |
| 80 | + String content = response.getContentAsString(); |
| 81 | + |
| 82 | + |
| 83 | + assertSoftly(softly -> { |
| 84 | + softly.assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value()); |
| 85 | + softly.assertThat(content.contains(ExceptionCode.NOT_FOUND_USER.getDescription())); |
| 86 | + }); |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | +} |
0 commit comments