Skip to content

Commit

Permalink
feat Privacy API Controller 작성, BaseIntegrationTest에서 Transactional 옵…
Browse files Browse the repository at this point in the history
…션 제거
  • Loading branch information
rookedsysc committed Mar 31, 2024
1 parent c5a8726 commit b81cf39
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gamemoonchul.infrastructure;
package com.gamemoonchul.infrastructure.web;

import com.gamemoonchul.application.MemberService;
import com.gamemoonchul.common.annotation.MemberSession;
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/gamemoonchul/domain/entity/MemberDummy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,18 @@ public static Member create() {
.role(MemberRole.USER)
.build();
}

public static Member createPrivacyRole() {
return Member.builder()
.provider(OAuth2Provider.GOOGLE)
.email("[email protected]")
.nickname("홍길동")
.identifier("test1")
.name("홍길동")
.picture("https://www.naver.com")
.score(0.0)
.role(MemberRole.PRIVACY_NOT_AGREED)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +17,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@Transactional
class MemberOpenApiControllerTest extends BaseIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b81cf39

Please sign in to comment.