Skip to content

Commit

Permalink
fix: 유틸리티에서 픽스처 및 Mock 인증 삽입 로직 제거 (#149)
Browse files Browse the repository at this point in the history
* fix: 픽스처 삽입 로직 제거

* fix: 시큐리티 mock 인증 삽입 로직 제거

* fix: 테스트 코드 BeforeEach 멤버 생성 로직 추가

---------

Co-authored-by: uwoobeat <desktop3711@gmail.com>
Co-authored-by: ybchar <uiurihappy@sju.ac.kr>
  • Loading branch information
3 people authored Jan 15, 2024
1 parent 94ca0d4 commit 3a8f7b4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 42 deletions.
14 changes: 0 additions & 14 deletions src/main/java/com/depromeet/global/util/MemberUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.depromeet.domain.member.dao.MemberRepository;
import com.depromeet.domain.member.domain.Member;
import com.depromeet.domain.member.domain.Profile;
import com.depromeet.global.error.exception.CustomException;
import com.depromeet.global.error.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,20 +14,7 @@ public class MemberUtil {
private final SecurityUtil securityUtil;
private final MemberRepository memberRepository;

// TODO: 데이터베이스 연동 및 픽스쳐 데이터 삽입 이후 삭제
private void insertMockMemberIfNotExist() {
if (memberRepository.count() != 0) {
return;
}

Member memeber = Member.createNormalMember(new Profile("testNickname", "testImageUrl"));

memberRepository.save(memeber);
}

public Member getCurrentMember() {
insertMockMemberIfNotExist();

return memberRepository
.findById(securityUtil.getCurrentMemberId())
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND));
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/com/depromeet/global/util/SecurityUtil.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
package com.depromeet.global.util;

import com.depromeet.global.security.PrincipalDetails;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
public class SecurityUtil {

private void setMockAuthentication() {
PrincipalDetails principal = new PrincipalDetails(1L, "USER");
Authentication authentication =
new UsernamePasswordAuthenticationToken(
principal, "password", principal.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
}

public Long getCurrentMemberId() {
setMockAuthentication();
PrincipalDetails principal =
(PrincipalDetails)
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
import com.depromeet.domain.missionRecord.dto.response.MissionRecordCreateResponse;
import com.depromeet.global.error.exception.CustomException;
import com.depromeet.global.error.exception.ErrorCode;
import com.depromeet.global.security.PrincipalDetails;
import java.time.LocalDateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
Expand All @@ -44,6 +48,11 @@ class ImageServiceTest {
@BeforeEach
void setUp() {
databaseCleaner.execute();
PrincipalDetails principal = new PrincipalDetails(1L, "USER");
Authentication authentication =
new UsernamePasswordAuthenticationToken(
principal, "password", principal.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.junit.jupiter.api.Assertions.*;

import com.depromeet.DatabaseCleaner;
import com.depromeet.domain.member.dao.MemberRepository;
import com.depromeet.domain.member.domain.Member;
import com.depromeet.domain.mission.application.MissionService;
import com.depromeet.domain.mission.dao.MissionRepository;
import com.depromeet.domain.mission.domain.Mission;
Expand All @@ -15,6 +17,7 @@
import com.depromeet.domain.mission.dto.response.MissionFindAllResponse;
import com.depromeet.domain.mission.dto.response.MissionFindResponse;
import com.depromeet.domain.mission.dto.response.MissionUpdateResponse;
import com.depromeet.global.security.PrincipalDetails;
import com.depromeet.global.util.MemberUtil;
import jakarta.persistence.EntityManager;
import java.time.LocalDateTime;
Expand All @@ -25,6 +28,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -34,14 +40,21 @@ class MissionServiceTest {

@Autowired private MissionService missionService;
@Autowired private MissionRepository missionRepository;
@Autowired private MemberRepository memberRepository;
@Autowired private DatabaseCleaner databaseCleaner;
@Autowired private EntityManager entityManager;
@Autowired private MemberUtil memberUtil;

@BeforeEach
void setUp() {
databaseCleaner.execute();
missionRepository.deleteAll();
PrincipalDetails principal = new PrincipalDetails(1L, "USER");
Authentication authentication =
new UsernamePasswordAuthenticationToken(
principal, "password", principal.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
Member guestMember = Member.createGuestMember("username", "password");
memberRepository.save(guestMember);
}

@Test
Expand Down
29 changes: 13 additions & 16 deletions src/test/java/com/depromeet/global/util/MemberUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import com.depromeet.DatabaseCleaner;
import com.depromeet.domain.member.dao.MemberRepository;
import com.depromeet.domain.member.domain.Member;
import com.depromeet.domain.member.domain.Profile;
import com.depromeet.global.security.PrincipalDetails;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
Expand All @@ -26,24 +29,18 @@ void setUp() {
}

@Test
void 이미_회원이_존재하면_임시_회원을_삽입하지_않는다() {
void 현재_로그인한_회원의_정보를_정상적으로_반환한다() {
// given
Member member = Member.createNormalMember(new Profile("testNickname", "testImageUrl"));
memberRepository.save(member);

PrincipalDetails principal = new PrincipalDetails(1L, "USER");
Authentication authentication =
new UsernamePasswordAuthenticationToken(
principal, "password", principal.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
Member guestMember = Member.createGuestMember("username", "password");
Member savedMember = memberRepository.save(guestMember);
// when
memberUtil.getCurrentMember();

// then
assertEquals(1, memberRepository.count());
}

@Test
void 현재_로그인한_회원ID는_1이다() {
// given & when
Member currentMember = memberUtil.getCurrentMember();

// then
assertEquals(1L, currentMember.getId());
assertEquals(savedMember.getId(), currentMember.getId());
}
}

0 comments on commit 3a8f7b4

Please sign in to comment.