From 501e379a775af249d71974941817e8b664e16167 Mon Sep 17 00:00:00 2001 From: rookedsysc <67862775+rookedsysc@users.noreply.github.com> Date: Sat, 13 Apr 2024 02:18:33 +0900 Subject: [PATCH] =?UTF-8?q?feat=20Privacy=20API=20Controller=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1=20(#59)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * modify Privacy Agreed Column 추가, Converter, Dto 수정 * feat Member Privacy Not Agreed Role 추가 /privacy/agree API 추가 Spring Security Config, TokenProvider 등 수정 * fix Test 코드 Filter에서 에러 발생해서 403 발생하는 것 확인 * modify Privacy API User 권한도 접근 가능하게 변경 * feat Privacy API Controller 작성, BaseIntegrationTest에서 Transactional 옵션 제거 * modify workflow --warning-mode all 추가 #51 * config Test Junit Dependency 추가 참조 이슈 : https://github.com/spring-io/initializr/issues/1476 * modify Test -i 옵션으로 Test 실패 원인 파악 시도 #51 * modify Privacy Controller 삭제 및 MatchUserService 이동 --- .github/workflows/cd-wordflow.yml | 6 +- build.gradle | 1 + .../application/BoardService.java | 1 - .../{ => application}/MatchUserService.java | 2 +- .../{ => web}/MemberPrivacyController.java | 2 +- .../gamemoonchul/MatchUserServiceTest.java | 1 + .../application/BoardServiceTest.java | 1 - .../application/MatchGameServiceTest.java | 1 - .../domain/entity/MemberDummy.java | 14 +++ .../web/MemberApiControllerTest.java | 2 + .../web/MemberOpenApiControllerTest.java | 2 + .../web/MemberPrivacyControllerTest.java | 114 ++++++++++++++++++ .../web/common/BaseIntegrationTest.java | 3 +- 13 files changed, 141 insertions(+), 9 deletions(-) rename src/main/java/com/gamemoonchul/{ => application}/MatchUserService.java (97%) rename src/main/java/com/gamemoonchul/infrastructure/{ => web}/MemberPrivacyController.java (95%) create mode 100644 src/test/java/com/gamemoonchul/infrastructure/web/MemberPrivacyControllerTest.java diff --git a/.github/workflows/cd-wordflow.yml b/.github/workflows/cd-wordflow.yml index e521c40e..3817c6ce 100644 --- a/.github/workflows/cd-wordflow.yml +++ b/.github/workflows/cd-wordflow.yml @@ -45,9 +45,11 @@ jobs: - name: Build with Gradle run: | chmod +x ./gradlew - ./gradlew clean build --warning-mode all - # 전송할 파일을 담을 디렉토리 생성 + ./gradlew test -i + ./gradlew clean build --warning-mode all + + # 전송할 파일을 담을 디렉토리 생성 - name: Make Directory for deliver run: mkdir deploy diff --git a/build.gradle b/build.gradle index a44881e3..57725be5 100644 --- a/build.gradle +++ b/build.gradle @@ -74,6 +74,7 @@ dependencies { annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } tasks.named('test') { diff --git a/src/main/java/com/gamemoonchul/application/BoardService.java b/src/main/java/com/gamemoonchul/application/BoardService.java index 05fed451..0072620c 100644 --- a/src/main/java/com/gamemoonchul/application/BoardService.java +++ b/src/main/java/com/gamemoonchul/application/BoardService.java @@ -1,6 +1,5 @@ package com.gamemoonchul.application; -import com.gamemoonchul.MatchUserService; import com.gamemoonchul.domain.entity.riot.MatchGame; import com.gamemoonchul.domain.model.vo.riot.MatchRecord; import com.gamemoonchul.infrastructure.adapter.LolSearchAdapter; diff --git a/src/main/java/com/gamemoonchul/MatchUserService.java b/src/main/java/com/gamemoonchul/application/MatchUserService.java similarity index 97% rename from src/main/java/com/gamemoonchul/MatchUserService.java rename to src/main/java/com/gamemoonchul/application/MatchUserService.java index 4a78639a..aa0de57e 100644 --- a/src/main/java/com/gamemoonchul/MatchUserService.java +++ b/src/main/java/com/gamemoonchul/application/MatchUserService.java @@ -1,4 +1,4 @@ -package com.gamemoonchul; +package com.gamemoonchul.application; import com.gamemoonchul.domain.converter.riot.MatchUserConverter; import com.gamemoonchul.domain.entity.riot.MatchGame; diff --git a/src/main/java/com/gamemoonchul/infrastructure/MemberPrivacyController.java b/src/main/java/com/gamemoonchul/infrastructure/web/MemberPrivacyController.java similarity index 95% rename from src/main/java/com/gamemoonchul/infrastructure/MemberPrivacyController.java rename to src/main/java/com/gamemoonchul/infrastructure/web/MemberPrivacyController.java index f63ff5fb..7290f1fe 100644 --- a/src/main/java/com/gamemoonchul/infrastructure/MemberPrivacyController.java +++ b/src/main/java/com/gamemoonchul/infrastructure/web/MemberPrivacyController.java @@ -1,4 +1,4 @@ -package com.gamemoonchul.infrastructure; +package com.gamemoonchul.infrastructure.web; import com.gamemoonchul.application.MemberService; import com.gamemoonchul.common.annotation.MemberSession; diff --git a/src/test/java/com/gamemoonchul/MatchUserServiceTest.java b/src/test/java/com/gamemoonchul/MatchUserServiceTest.java index ed58c64c..2b2b95c9 100644 --- a/src/test/java/com/gamemoonchul/MatchUserServiceTest.java +++ b/src/test/java/com/gamemoonchul/MatchUserServiceTest.java @@ -1,6 +1,7 @@ package com.gamemoonchul; import com.gamemoonchul.application.MatchGameService; +import com.gamemoonchul.application.MatchUserService; import com.gamemoonchul.domain.entity.riot.MatchGame; import com.gamemoonchul.domain.entity.riot.MatchUser; import com.gamemoonchul.domain.model.vo.riot.MatchDummy; diff --git a/src/test/java/com/gamemoonchul/application/BoardServiceTest.java b/src/test/java/com/gamemoonchul/application/BoardServiceTest.java index 0e3a3885..38c47f00 100644 --- a/src/test/java/com/gamemoonchul/application/BoardServiceTest.java +++ b/src/test/java/com/gamemoonchul/application/BoardServiceTest.java @@ -1,6 +1,5 @@ package com.gamemoonchul.application; -import com.gamemoonchul.MatchUserService; import com.gamemoonchul.domain.entity.riot.MatchGame; import com.gamemoonchul.domain.model.vo.riot.MatchDummy; import com.gamemoonchul.domain.model.vo.riot.MatchRecord; diff --git a/src/test/java/com/gamemoonchul/application/MatchGameServiceTest.java b/src/test/java/com/gamemoonchul/application/MatchGameServiceTest.java index 25a172ed..b72e6b43 100644 --- a/src/test/java/com/gamemoonchul/application/MatchGameServiceTest.java +++ b/src/test/java/com/gamemoonchul/application/MatchGameServiceTest.java @@ -1,6 +1,5 @@ package com.gamemoonchul.application; -import com.gamemoonchul.MatchUserService; import com.gamemoonchul.domain.entity.riot.MatchGame; import com.gamemoonchul.domain.entity.riot.MatchUser; import com.gamemoonchul.domain.model.vo.riot.MatchDummy; diff --git a/src/test/java/com/gamemoonchul/domain/entity/MemberDummy.java b/src/test/java/com/gamemoonchul/domain/entity/MemberDummy.java index 27bcb4d6..c1e2985e 100644 --- a/src/test/java/com/gamemoonchul/domain/entity/MemberDummy.java +++ b/src/test/java/com/gamemoonchul/domain/entity/MemberDummy.java @@ -16,4 +16,18 @@ public static Member create() { .role(MemberRole.USER) .build(); } + + public static Member createPrivacyRole() { + return Member.builder() + .provider(OAuth2Provider.GOOGLE) + .email("test1@gmail.com") + .nickname("홍길동") + .identifier("test1") + .name("홍길동") + .picture("https://www.naver.com") + .score(0.0) + .role(MemberRole.PRIVACY_NOT_AGREED) + .build(); + } + } \ No newline at end of file diff --git a/src/test/java/com/gamemoonchul/infrastructure/web/MemberApiControllerTest.java b/src/test/java/com/gamemoonchul/infrastructure/web/MemberApiControllerTest.java index 60682fa7..3ab296bd 100644 --- a/src/test/java/com/gamemoonchul/infrastructure/web/MemberApiControllerTest.java +++ b/src/test/java/com/gamemoonchul/infrastructure/web/MemberApiControllerTest.java @@ -7,6 +7,7 @@ import com.gamemoonchul.domain.status.MemberStatus; import com.gamemoonchul.infrastructure.repository.MemberRepository; import com.gamemoonchul.infrastructure.web.common.BaseIntegrationTest; +import jakarta.transaction.Transactional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -23,6 +24,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +@Transactional class MemberApiControllerTest extends BaseIntegrationTest { @Autowired private TokenHelper tokenHelper; diff --git a/src/test/java/com/gamemoonchul/infrastructure/web/MemberOpenApiControllerTest.java b/src/test/java/com/gamemoonchul/infrastructure/web/MemberOpenApiControllerTest.java index 9d31d2c0..c7a4edcf 100644 --- a/src/test/java/com/gamemoonchul/infrastructure/web/MemberOpenApiControllerTest.java +++ b/src/test/java/com/gamemoonchul/infrastructure/web/MemberOpenApiControllerTest.java @@ -4,6 +4,7 @@ import com.gamemoonchul.config.oauth.user.OAuth2Provider; import com.gamemoonchul.infrastructure.web.common.BaseIntegrationTest; import com.gamemoonchul.infrastructure.web.dto.RenewRequest; +import jakarta.transaction.Transactional; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -16,6 +17,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +@Transactional class MemberOpenApiControllerTest extends BaseIntegrationTest { @Autowired diff --git a/src/test/java/com/gamemoonchul/infrastructure/web/MemberPrivacyControllerTest.java b/src/test/java/com/gamemoonchul/infrastructure/web/MemberPrivacyControllerTest.java new file mode 100644 index 00000000..ac810867 --- /dev/null +++ b/src/test/java/com/gamemoonchul/infrastructure/web/MemberPrivacyControllerTest.java @@ -0,0 +1,114 @@ +package com.gamemoonchul.infrastructure.web; + +import com.gamemoonchul.config.jwt.TokenDto; +import com.gamemoonchul.config.jwt.TokenHelper; +import com.gamemoonchul.config.jwt.TokenInfo; +import com.gamemoonchul.config.jwt.TokenType; +import com.gamemoonchul.domain.entity.Member; +import com.gamemoonchul.domain.entity.MemberDummy; +import com.gamemoonchul.infrastructure.repository.MemberRepository; +import com.gamemoonchul.infrastructure.web.common.BaseIntegrationTest; +import jakarta.transaction.Transactional; +import org.aspectj.lang.annotation.Before; +import org.junit.jupiter.api.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.util.Optional; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@TestPropertySource(locations = "classpath:application.yaml") +class MemberPrivacyControllerTest extends BaseIntegrationTest { + + @Autowired + private MemberRepository memberRepository; + + @Autowired + private TokenHelper tokenHelper; + + Member member; + + TokenDto tokenDto; + + @Test + @Order(1) + void setUp() { + member = MemberDummy.createPrivacyRole(); + memberRepository.save(member); + } + + void getTokenDto() { + member = MemberDummy.createPrivacyRole(); + TokenInfo tokenInfo = TokenInfo.builder() + .email(member.getEmail()) + .provider(member.getProvider().toString()) + .identifier(member.getIdentifier()) + .tokenType(TokenType.ACCESS) + .build(); + tokenDto = tokenHelper.generateToken(tokenInfo); + } + + @Test + @Order(2) + @DisplayName("privacy 동의 안된 경우 테스트") + void notAgreed() throws Exception { + // given + getTokenDto(); + // when + ResultActions resultActions = super.mvc.perform(get("/privacy/is-agreed") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + tokenDto.getAccessToken())); + + // then + resultActions.andExpect(jsonPath("$.data").value(false)); + } + + @Test + @Order(3) + @DisplayName("privacy 동의 api 호출") + void agreePrivcayTest() throws Exception { + // given + getTokenDto(); + // when + ResultActions resultActions = super.mvc.perform(MockMvcRequestBuilders.patch("/privacy/agree") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + tokenDto.getAccessToken())); + + // then + resultActions.andExpect(status().isOk()); + } + + @Test + @Order(4) + @DisplayName("privacy 동의 후 동의가 됐음을 확인") + void agreed() throws Exception { + // given + getTokenDto(); + // when + ResultActions resultActions = super.mvc.perform(get("/privacy/is-agreed") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + tokenDto.getAccessToken())); + + // then + resultActions.andExpect(jsonPath("$.data").value(true)); + } + + @Test + @Order(2) + @DisplayName("토큰 없이 호출하면 401에러 발생") + void notAuthorized() throws Exception { + // given // when + ResultActions resultActions = super.mvc.perform(get("/privacy/is-agreed") + .contentType(MediaType.APPLICATION_JSON)); + + // then + resultActions.andExpect(status().isUnauthorized()); + } +} diff --git a/src/test/java/com/gamemoonchul/infrastructure/web/common/BaseIntegrationTest.java b/src/test/java/com/gamemoonchul/infrastructure/web/common/BaseIntegrationTest.java index 51c13d94..600f6757 100644 --- a/src/test/java/com/gamemoonchul/infrastructure/web/common/BaseIntegrationTest.java +++ b/src/test/java/com/gamemoonchul/infrastructure/web/common/BaseIntegrationTest.java @@ -7,11 +7,10 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.transaction.annotation.Isolation; @SpringBootTest -@Disabled @AutoConfigureMockMvc -@Transactional public class BaseIntegrationTest { @Autowired protected MockMvc mvc;