Skip to content

Commit

Permalink
feat: 탈퇴 기능 서비스 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ503 committed Feb 4, 2024
1 parent 9b55084 commit a1926ac
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,13 @@ private User validateAndGetUser(final Long userId, final AuthClaims authClaims)
return userRepository.findById(userId)
.orElseThrow(NotFoundUserException::new);
}

public void withdraw(final Long userId, final String refreshToken) {
final AuthClaims authClaims = tokenProvider.parseToken(TokenType.REFRESH, refreshToken);
final User user = validateAndGetUser(userId, authClaims);

user.delete();
blackListTokenService.register(refreshToken);
deviceTokenService.deactivateAllByUserId(user.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public void deactivate(final Long userId, final String token) {
final Optional<DeviceToken> deviceToken = deviceTokenRepository.findByUserIdAndToken(userId, token);
deviceToken.ifPresent(DeviceToken::deactivate);
}

public void deactivateAllByUserId(final Long userId) {
final List<DeviceToken> deviceTokens = deviceTokenRepository.findAllByUserIdAndActiveIsTrue(userId);
deviceTokens.forEach(DeviceToken::deactivate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.SpyBean;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.mockito.BDDMockito.willReturn;
Expand Down Expand Up @@ -190,4 +192,27 @@ class AuthenticationServiceTest extends AuthenticationServiceTestFixture {
assertThatThrownBy(() -> authenticationService.logout(기존_사용자.getId(), 유효하지_않은_리프레시_토큰을_갖는_로그아웃_dto))
.isInstanceOf(InvalidTokenException.class);
}

@Test
void 로그아웃시_디바이스_토큰과_액세스_토큰을_비활성화하고_사용자의_삭제_여부를_참으로_변경한다() {
// when
authenticationService.withdraw(기존_사용자.getId(), 유효한_refresh_token);

// then
final User user = userRepository.findById(기존_사용자.getId()).get();
final BlackListToken blackListToken = blackListTokenRepository.findByToken(유효한_refresh_token).get();
final List<DeviceToken> deviceTokens = deviceTokenRepository.findAllByUserIdAndActiveIsTrue(기존_사용자.getId());
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(user.isDeleted()).isTrue();
softAssertions.assertThat(blackListToken.getToken()).isEqualTo(유효한_refresh_token);
softAssertions.assertThat(deviceTokens).isEmpty();
});
}

@Test
void 탈퇴시_리프레시_토큰이_유효하지_않다면_예외를_발생시킨다() {
// when & then
assertThatThrownBy(() -> authenticationService.withdraw(기존_사용자.getId(), 유효하지_않는_refresh_token))
.isInstanceOf(InvalidTokenException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@IsolateDatabase
Expand Down Expand Up @@ -70,4 +72,14 @@ class DeviceTokenServiceTest extends DeviceTokenServiceTestFixture {
final DeviceToken actual = deviceTokenRepository.findByUserIdAndToken(사용자_아이디, 디바이스_토큰1.getToken()).get();
assertThat(actual.isActive()).isFalse();
}

@Test
void 사용자의_모든_디바이스_토큰을_비활성화한다() {
// when
deviceTokenService.deactivateAllByUserId(사용자_아이디);

// then
final List<DeviceToken> deviceTokens = deviceTokenRepository.findAllByUserIdAndActiveIsTrue(사용자_아이디);
assertThat(deviceTokens).isEmpty();
}
}

0 comments on commit a1926ac

Please sign in to comment.