Skip to content

Commit

Permalink
refactor: RefreshToken을 Redis Entity로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
jjongwa committed Nov 13, 2023
1 parent 4076c33 commit 83c107c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public void deleteMember(final MemberDeleteEvent event) {

dayLogRepository.deleteByIds(dayLogIds);
tripRepository.deleteByMemberId(event.getMemberId());
refreshTokenRepository.deleteByMemberId(event.getMemberId());
}

@Async
Expand Down
19 changes: 5 additions & 14 deletions backend/src/main/java/hanglog/login/domain/RefreshToken.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
package hanglog.login.domain;

import static lombok.AccessLevel.PROTECTED;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;

@Entity
@Getter
@NoArgsConstructor(access = PROTECTED)
@AllArgsConstructor
@RedisHash(value = "refreshToken", timeToLive = 604800)
public class RefreshToken {

@Id
private String token;

@Column(nullable = false)
private Long memberId;

public RefreshToken(final String token, final Long memberId) {
this.token = token;
this.memberId = memberId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import hanglog.login.domain.RefreshToken;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

public interface RefreshTokenRepository extends CrudRepository<RefreshToken, String> {

Optional<RefreshToken> findByToken(final String token);

boolean existsByToken(final String token);

void deleteByMemberId(@Param("memberId") final Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,66 +1,40 @@
package hanglog.integration.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import hanglog.community.domain.repository.PublishedTripRepository;
import hanglog.login.domain.OauthProviders;
import hanglog.login.domain.repository.RefreshTokenRepository;
import hanglog.login.infrastructure.BearerAuthorizationExtractor;
import hanglog.login.infrastructure.JwtProvider;
import hanglog.login.service.LoginService;
import hanglog.member.domain.Member;
import hanglog.member.domain.repository.MemberRepository;
import hanglog.trip.domain.repository.SharedTripRepository;
import hanglog.trip.domain.repository.TripRepository;
import hanglog.trip.infrastructure.CustomTripRepositoryImpl;
import jakarta.persistence.EntityManager;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Import;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;

@Import({
LoginService.class,
OauthProviders.class,
JwtProvider.class,
BearerAuthorizationExtractor.class,
CustomTripRepositoryImpl.class
})
class LoginServiceIntegrationTest extends ServiceIntegrationTest {
@SpringBootTest
@Sql(value = {"classpath:data/truncate.sql"})
class LoginServiceIntegrationTest {

@Autowired
private OauthProviders oauthProviders;
@Autowired
private RefreshTokenRepository refreshTokenRepository;
@Autowired
private TripRepository tripRepository;
@Autowired
private JwtProvider jwtProvider;
@Autowired
private BearerAuthorizationExtractor bearerExtractor;
@Autowired
private LoginService loginService;
@Autowired
private PublishedTripRepository publishedTripRepository;
@Autowired
private MemberRepository memberRepository;
@Autowired
private SharedTripRepository sharedTripRepository;
@Autowired
private ApplicationEventPublisher publisher;
@Autowired
private EntityManager entityManager;

@DisplayName("멤버를 삭제한다.")
@Test
void deleteAccount() {
// when & then
// given
final Member member = memberRepository.save(new Member(
"socialLoginId",
"name",
"https://hanglog.com/img/imageName.png"
));
assertThat(memberRepository.findById(member.getId())).isPresent();
assertDoesNotThrow(() -> loginService.deleteAccount(member.getId()));
entityManager.flush();
entityManager.clear();

// when
loginService.deleteAccount(member.getId());

// then
assertThat(memberRepository.findById(member.getId())).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ void deleteMember() {
doNothing().when(itemRepository).deleteByIds(anyList());
doNothing().when(dayLogRepository).deleteByIds(anyList());
doNothing().when(tripRepository).deleteByMemberId(anyLong());
doNothing().when(refreshTokenRepository).deleteByMemberId(anyLong());

// when
listener.deleteMember(event);
Expand All @@ -83,7 +82,6 @@ void deleteMember() {
verify(itemRepository, times(1)).deleteByIds(anyList());
verify(dayLogRepository, times(1)).deleteByIds(anyList());
verify(tripRepository, times(1)).deleteByMemberId(anyLong());
verify(refreshTokenRepository, times(1)).deleteByMemberId(anyLong());
}

@DisplayName("deleteTrip 메서드에서 올바르게 레포지토리의 메서드를 호출한다.")
Expand Down
1 change: 0 additions & 1 deletion backend/src/test/resources/data/truncate.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ TRUNCATE TABLE currency RESTART IDENTITY;
TRUNCATE TABLE city RESTART IDENTITY;
TRUNCATE TABLE category RESTART IDENTITY;
TRUNCATE TABLE member RESTART IDENTITY;
TRUNCATE TABLE refresh_token RESTART IDENTITY;
SET referential_integrity TRUE;

0 comments on commit 83c107c

Please sign in to comment.