Skip to content

Commit

Permalink
refactor : 테스트코드 수정 #45
Browse files Browse the repository at this point in the history
  • Loading branch information
strangehoon committed Aug 7, 2024
1 parent 56b76aa commit ae6fde5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sendback.domain.auth.fixture;

import com.sendback.domain.auth.dto.Token;

public class AuthFixture {

public static final Token NEW_TOKEN = new Token("new_access_token", "new_refresh_token");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.sendback.domain.auth.service;

import com.sendback.domain.auth.dto.Token;
import com.sendback.global.config.jwt.JwtProvider;
import com.sendback.global.config.redis.RedisService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.sendback.domain.auth.fixture.AuthFixture.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class AuthServiceTest {

@InjectMocks
AuthService authService;
@Mock
RedisService redisService;
@Mock
JwtProvider jwtProvider;

@Test
@DisplayName("토큰 재발급시 access token, refresh token을 재발급한다.")
void reissueToken() {
// given
String refresh_token = "valid_refresh_token";
Long user_id = 123L;
given(jwtProvider.parseRefreshToken(refresh_token)).willReturn(user_id);
given(jwtProvider.issueToken(user_id)).willReturn(NEW_TOKEN);

// when
Token result = authService.reissueToken(refresh_token);

// then
assertNotNull(result);
assertEquals(NEW_TOKEN.accessToken(), result.accessToken());
assertEquals(NEW_TOKEN.refreshToken(), result.refreshToken());
}

@Test
@DisplayName("로그아웃을 진행하면 redis에서 해당 유저의 refresh token을 삭제한다.")
void logoutSocial() {
// given
Long user_id = 123L;
doNothing().when(redisService).delete(user_id);

// when
authService.logoutSocial(user_id);

// then
verify(redisService, times(1)).delete(user_id);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.sendback.domain.comment.persister;
package com.sendback.domain.comment.fixture;

import com.sendback.domain.comment.entity.Comment;
import com.sendback.domain.project.entity.Project;
import com.sendback.domain.user.entity.User;

import static com.sendback.domain.project.fixture.ProjectFixture.createDummyProject;
import static com.sendback.domain.user.fixture.UserFixture.createDummyUser;

public class CommentFixture {

public static Comment createDummyComment(User user, Project project){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;
import java.util.Optional;
import static com.sendback.domain.comment.exception.CommentExceptionType.NOT_COMMENT_AUTHOR;
import static com.sendback.domain.comment.persister.CommentFixture.createDummyComment;
import static com.sendback.domain.comment.fixture.CommentFixture.createDummyComment;
import static com.sendback.domain.project.fixture.ProjectFixture.createDummyProject;
import static com.sendback.domain.user.fixture.UserFixture.*;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.sendback.global.ControllerTest;
import com.sendback.global.WithMockCustomUser;
import com.sendback.global.common.CustomPage;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -22,7 +21,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
Expand All @@ -47,13 +45,12 @@ class signUp {
@Test
@DisplayName("회원 가입을 성공하면 200 상태코드와 함께 access token, refresh token을 반환한다.")
@WithMockCustomUser
@Disabled
void signUpKakao_success() throws Exception {
// given
String accessToken = "valid accessToken";
String refreshToken = "valid refreshToken";
String accessToken = "valid_accessToken";
String refreshToken = "valid_refreshToken";
SignUpRequestDto signUpRequestDto = new SignUpRequestDto("user", "2000.01.01",
"남자", "백엔드", Arrays.asList("환경", "게임"), "valid signToken");
"남자", "백엔드", Arrays.asList("환경", "게임"), "valid_signToken");
given(userService.signUpUser(signUpRequestDto)).willReturn(new Token(accessToken, refreshToken));

String content = objectMapper.writeValueAsString(signUpRequestDto);
Expand All @@ -67,7 +64,7 @@ void signUpKakao_success() throws Exception {
.andExpect(jsonPath("$.code").value("200"))
.andExpect(jsonPath("$.message").value("성공"))
.andExpect(jsonPath("$.data.accessToken").value(accessToken))
.andExpect(header().string("Set-Cookie", containsString("refreshToken="+refreshToken)))
.andExpect(header().exists(HttpHeaders.SET_COOKIE))
.andDo(print());

// then
Expand Down Expand Up @@ -228,12 +225,11 @@ class updateUserInfo {
@Test
@DisplayName("내 정보를 수정하면 200 상태코드와 함께 수정된 정보들도 반환한다.")
@WithMockCustomUser
@Disabled
void updateUserInfo_success() throws Exception {

// given
UpdateUserInfoRequestDto updateUserInfoRequestDto = new UpdateUserInfoRequestDto("테스트 사용자", "2000.01.01", "백엔드", Arrays.asList("환경", "게임"));
UpdateUserInfoResponseDto updateUserInfoResponseDto = new UpdateUserInfoResponseDto("테스트 사용자", "2000.01.01", "백엔드", Arrays.asList("환경", "게임"));
UpdateUserInfoRequestDto updateUserInfoRequestDto = new UpdateUserInfoRequestDto("테스트사용자", "2000.01.01", "백엔드", Arrays.asList("환경", "게임"));
UpdateUserInfoResponseDto updateUserInfoResponseDto = new UpdateUserInfoResponseDto("테스트사용자", "2000.01.01", "백엔드", Arrays.asList("환경", "게임"));

given(userService.updateUserInfo(anyLong(), any(UpdateUserInfoRequestDto.class))).willReturn(updateUserInfoResponseDto);
String content = objectMapper.writeValueAsString(updateUserInfoRequestDto);
Expand Down

0 comments on commit ae6fde5

Please sign in to comment.