Skip to content

Commit

Permalink
#18 feat: 로그아웃 로직 테스트 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JoongHyun-Kim committed Jan 25, 2024
1 parent 0ffd71c commit 2c23949
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/test/java/com/kkobugi/puremarket/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.transaction.annotation.Transactional;

import static com.kkobugi.puremarket.common.constants.Constant.INACTIVE;
import static com.kkobugi.puremarket.common.constants.Constant.LOGOUT;
import static com.kkobugi.puremarket.common.enums.BaseResponseStatus.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
Expand Down Expand Up @@ -107,6 +108,37 @@ public void check_duplicated_loginId() throws BaseException {
assertThrows(BaseException.class, () -> userService.signup(signupRequest2) ,DUPLICATED_LOGIN_ID.getMessage());
}

@Test
@Transactional
@DisplayName("로그아웃")
public void logout() throws Exception {

// given
SignupRequest signupRequest = new SignupRequest("nickname", "id", "pw", "pw", "01012345678");
userService.signup(signupRequest);

// when
User user = userRepository.findByLoginId("id").orElseThrow(() -> new BaseException(INVALID_LOGIN_ID));

// accessToken 값을 넣어주기 위해 mockMVC로 Http Request 보내기
MvcResult loginResponse = mockMvc.perform(MockMvcRequestBuilders.post(RequestURI.user+"/login")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"loginId\":\"id\", \"password\":\"pw\"}"))
.andReturn();

String responseBody = loginResponse.getResponse().getContentAsString();
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(responseBody);
JsonNode resultNode = jsonNode.get("result");
String accessToken = resultNode.get("accessToken").asText();

mockMvc.perform(patch(RequestURI.user+"/logout")
.header("Authorization", "Bearer " + accessToken));

// then
assertEquals(LOGOUT, user.getStatus());
}

@Test
@Transactional
@DisplayName("회원 탈퇴")
Expand Down Expand Up @@ -137,5 +169,4 @@ public void signout() throws Exception {
// then
assertEquals(INACTIVE, user.getStatus());
}

}

0 comments on commit 2c23949

Please sign in to comment.