Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat, hotfix : 회원탈퇴 api 추가 및 redisTemplate 수정 #170

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ public ResponseEntity<Void> memberLogout(@MemberId Long memberId) {
memberService.memberLogout(memberId);
return ResponseEntity.ok().build();
}

@Operation(summary = "멤버 회원탈퇴", description = "회원 정보를 삭제합니다.")
@DeleteMapping("/withdrawal")
public ResponseEntity<Void> deleteMember(@MemberId Long memberId) {
memberService.deleteMember(memberId);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class MemberService implements MemberIdProvider {
private final KakaoApiService kakaoApiService;
private final KakaoTokenService kakaoTokenService;
private final PointRepository pointRepository;
private final RedisTemplate<String, String> redisTemplate;
private final RedisTemplate<String, Object> redisTemplate;

public MemberService(MemberRepository memberRepository, TokenService tokenService, KakaoApiService kakaoApiService, KakaoTokenService kakaoTokenService, PointRepository pointRepository, RedisTemplate<String, String> redisTemplate) {
public MemberService(MemberRepository memberRepository, TokenService tokenService, KakaoApiService kakaoApiService, KakaoTokenService kakaoTokenService, PointRepository pointRepository, RedisTemplate<String, Object> redisTemplate) {
this.memberRepository = memberRepository;
this.tokenService = tokenService;
this.kakaoApiService = kakaoApiService;
Expand Down Expand Up @@ -90,10 +90,23 @@ public void memberLogout(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundException("id에 해당하는 멤버가 없습니다."));

String storedRefreshToken = redisTemplate.opsForValue().get(member.getEmail());
String storedRefreshToken = (String) redisTemplate.opsForHash().get(member.getEmail(), "refreshToken");

if (storedRefreshToken != null) {
redisTemplate.delete(member.getEmail());
}
}

public void deleteMember(Long memberId){
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundException("id에 해당하는 멤버가 없습니다."));

String storedRefreshToken = (String) redisTemplate.opsForHash().get(member.getEmail(), "refreshToken");

if (storedRefreshToken != null) {
redisTemplate.delete(member.getEmail());
}

memberRepository.deleteById(memberId);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.example.sinitto.auth.service;

import com.example.sinitto.common.exception.InvalidJwtException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoSettings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Base64;

import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -22,10 +17,10 @@
@MockitoSettings
public class TokenServiceTest {
@Mock
private RedisTemplate<String, String> redisTemplate;
private RedisTemplate<String, Object> redisTemplate;

@Mock
private ValueOperations<String, String> valueOperations;
private HashOperations<String, Object, Object> hashOperations;

private TokenService tokenService;

Expand Down Expand Up @@ -60,7 +55,7 @@ void generateRefreshTokenTest() {
//given
String email = "[email protected]";

when(redisTemplate.opsForValue()).thenReturn(valueOperations);
when(redisTemplate.opsForHash()).thenReturn(hashOperations);

//when
String token = tokenService.generateRefreshToken(email);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoSettings;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.Optional;

Expand All @@ -31,9 +31,9 @@ public class MemberServiceTest {
@Mock
TokenService tokenService;
@Mock
RedisTemplate<String, String> redisTemplate;
RedisTemplate<String, Object> redisTemplate;
@Mock
private ValueOperations<String, String> valueOperations;
private HashOperations<String, Object, Object> hashOperations;
@InjectMocks
MemberService memberService;

Expand Down Expand Up @@ -61,7 +61,6 @@ void getMemberIdByTokenTestWhenNotInMemberRepository() {
//given
String token = "testtoken";
String email = "[email protected]";
Member member = mock(Member.class);

when(tokenService.extractEmailFromAccessToken(token)).thenReturn(email);
when(memberRepository.findByEmail(email)).thenReturn(Optional.empty());
Expand Down Expand Up @@ -110,12 +109,14 @@ void registerNewMemberTestWhenExists() {
void memberLogoutTest() {
//given
Long memberId = 1L;
String email = "[email protected]";
Member member = mock(Member.class);
String storedRefreshToken = "testRefreshToken";

when(memberRepository.findById(memberId)).thenReturn(Optional.of(member));
when(redisTemplate.opsForValue()).thenReturn(valueOperations);
when(redisTemplate.opsForValue().get(member.getEmail())).thenReturn(storedRefreshToken);
when(member.getEmail()).thenReturn(email);
when(redisTemplate.opsForHash()).thenReturn(hashOperations);
when(redisTemplate.opsForHash().get(member.getEmail(), "refreshToken")).thenReturn(storedRefreshToken);
//when
memberService.memberLogout(memberId);

Expand All @@ -136,4 +137,25 @@ void memberLogoutTestWhenMemberIsNull() {
//when, then
assertThrows(NotFoundException.class, () -> memberService.memberLogout(memberId));
}

@Test
@DisplayName("deleteMember 메소드 테스트")
void deleteMemberTest(){
//given
Long memberId = 1L;
String email = "[email protected]";
Member member = mock(Member.class);
String storedRefreshToken = "testRefreshToken";

when(memberRepository.findById(memberId)).thenReturn(Optional.of(member));
when(member.getEmail()).thenReturn(email);
when(redisTemplate.opsForHash()).thenReturn(hashOperations);
when(redisTemplate.opsForHash().get(member.getEmail(), "refreshToken")).thenReturn(storedRefreshToken);

//when
memberService.deleteMember(memberId);

//when
verify(memberRepository, times(1)).deleteById(memberId);
}
}