Skip to content

Commit

Permalink
[refactor/InhaBas#179] Member Controller test 재구현
Browse files Browse the repository at this point in the history
  • Loading branch information
whitem4rk committed Dec 14, 2023
1 parent 1530fd0 commit 0b00d7a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface MemberService {

List<ApprovedMemberManagementDto> getApprovedMembersBySearchAndRole(String search);

void updateUnapprovedMembers(List<Integer> memberIdList, String state);
void updateUnapprovedMembers(List<Long> memberIdList, String state);

void updateApprovedMembers(List<Long> memberIdList, Role role);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,9 @@ public List<ApprovedMemberManagementDto> getApprovedMembersBySearchAndRole(Strin

@Override
@Transactional
public void updateUnapprovedMembers(List<Integer> memberIdList, String state) {
public void updateUnapprovedMembers(List<Long> memberIdList, String state) {

List<Long> memberLongList = memberIdList.stream()
.map(Long::valueOf)
.collect(Collectors.toList());

List<Member> members = memberRepository.findAllById(memberLongList);
List<Member> members = memberRepository.findAllById(memberIdList);
boolean allNewMembers = members.stream().allMatch(
member -> DEFAULT_ROLE_AFTER_FINISH_SIGNUP.equals(member.getRole()));

Expand All @@ -132,7 +128,6 @@ public void updateUnapprovedMembers(List<Integer> memberIdList, String state) {
}

if (state.equals(PASS_STATE)) {

for (Member member : members)
member.setRole(DEACTIVATED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@AllArgsConstructor
public class UpdateRequestDto {

private List<Integer> memberIdList;
private List<Long> memberIdList;

@Schema(example = "pass, fail")
private String state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.IbasInformation;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.MemberType;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.SchoolInformation;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.StudentId;
import com.inhabas.api.auth.domain.oauth2.userInfo.GoogleOAuth2UserInfo;
Expand All @@ -12,31 +11,10 @@
import java.util.HashMap;
import java.util.Map;

import static com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.MemberType.*;
import static com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.MemberType.UNDERGRADUATE;
import static com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role.*;

public class MemberTest {

public static Member chiefMember() {
return new Member(
new StudentId("12171707"), "김회장", "010-1111-1111", "[email protected]", ""
, SchoolInformation.ofUnderGraduate("컴퓨터공학과", 2)
, new IbasInformation(CHIEF));
}

public static Member executivesMember() {
return new Member(
new StudentId("12201122"), "박임원", "010-2222-2222", "[email protected]", ""
, SchoolInformation.ofUnderGraduate("컴퓨터공학과", 2)
, new IbasInformation(EXECUTIVES));
}

public static Member secretaryMember() {
return new Member(
new StudentId("12219882"), "이총무", "010-3333-3333", "[email protected]", ""
, SchoolInformation.ofUnderGraduate("컴퓨터공학과", 2)
, new IbasInformation(SECRETARY));
}

public static Member signingUpMember1() {

Expand Down Expand Up @@ -110,10 +88,25 @@ public static Member basicMember2() {
}

public static Member deactivatedMember() {

Map<String, Object> attributes = new HashMap<>() {{
put("provider", "GOOGLE");
put("sub", "1249846925629348");
put("picture", "/static/image.jpg");
put("email", "[email protected]");
put("name", "유동현");
put("locale", "ko");
}};
OAuth2UserInfo user = new GoogleOAuth2UserInfo(attributes);
Member member = new Member(user);
member.setEmail("[email protected]");
member.setName("유동현");

return new Member(
new StudentId("12171707"), "최비활", "010-1111-1111", "[email protected]", ""
new StudentId("12171707"), "김미승인", "010-1111-1111", "[email protected]", ""
, SchoolInformation.ofUnderGraduate("컴퓨터공학과", 2)
, new IbasInformation(DEACTIVATED));

}

public static Member notapprovedMember() {
Expand All @@ -131,11 +124,11 @@ public static Member notapprovedMember() {
member.setEmail("[email protected]");
member.setName("유동현");


return new Member(
new StudentId("12171707"), "김미승인", "010-1111-1111", "[email protected]", ""
, SchoolInformation.ofUnderGraduate("컴퓨터공학과", 2)
, new IbasInformation(NOT_APPROVED));

}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
package com.inhabas.api.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import com.inhabas.api.auth.domain.oauth2.member.domain.service.MemberService;
import com.inhabas.api.auth.domain.oauth2.member.dto.ApprovedMemberManagementDto;
import com.inhabas.api.auth.domain.oauth2.member.dto.ContactDto;
import com.inhabas.api.auth.domain.oauth2.member.dto.NotApprovedMemberManagementDto;
import com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role;
import com.inhabas.api.auth.domain.oauth2.member.dto.*;
import com.inhabas.api.auth.domain.oauth2.member.repository.MemberRepository;
import com.inhabas.api.domain.member.domain.entity.MemberTest;
import com.inhabas.api.domain.signUp.dto.AnswerDto;
import com.inhabas.api.domain.signUp.usecase.AnswerService;
import com.inhabas.testAnnotataion.NoSecureWebMvcTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.util.ArrayList;
import java.util.List;

import static com.inhabas.api.auth.domain.oauth2.member.domain.valueObject.Role.BASIC;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doThrow;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand All @@ -31,13 +39,14 @@ public class MemberControllerTest {

@Autowired
private MockMvc mvc;

@MockBean
private MemberService memberService;

@MockBean
private AnswerService answerService;

@MockBean
private MemberRepository memberRepository;
@Autowired
private ObjectMapper objectMapper;

@DisplayName("(신입)미승인 멤버 정보 목록을 불러온다")
@Test
Expand Down Expand Up @@ -65,39 +74,52 @@ public void getUnapprovedMembers() throws Exception {

}

@DisplayName("(신입)미승인 멤버 -> 비활동 멤버로 변경한다.")
@Test
public void passUnapprovedMembers() throws Exception {

// 회원가입 이후 구현
@DisplayName("(신입)미승인 멤버 -> 비활동 멤버로 변경할때 state가 pass, fail이면 204를 반환하고 아니면 400를 반환한다..")
@ParameterizedTest
@ValueSource(strings = {"pass", "fail", "hacker"})
public void passOrFailUnapprovedMembers(String state) throws Exception {
//given
List<Long> memberIdList = List.of(1L);
List<Member> members = List.of(MemberTest.notapprovedMember());

if (state.equals("pass") || state.equals("fail")) {
//when
given(memberRepository.findAllById(memberIdList)).willReturn(members);
//then
mvc.perform(post("/members/unapproved")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonOf(new UpdateRequestDto(memberIdList, state))))
.andExpect(status().isNoContent());
} else {
//when
doThrow(new IllegalArgumentException()).when(memberService).updateUnapprovedMembers(anyList(), anyString());
//then
mvc.perform(post("/members/unapproved")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonOf(new UpdateRequestDto(memberIdList, state))))
.andExpect(status().isBadRequest());
}
}

@DisplayName("(신입)미승인 멤버 가입 거절 처리한다.")
@DisplayName("특정 신입 멤버 지원서를 조회한다.")
@Test
public void failUnapprovedMembers() throws Exception {
public void getUnapprovedMemberApplication() throws Exception {

// 회원가입 이후 구현
}
// given
List<AnswerDto> dtoList = new ArrayList<>();
AnswerDto dto1 = new AnswerDto(
1L, "안녕하세요. 예 안녕히계세요.");
dtoList.add(dto1);

// @DisplayName("특정 신입 멤버 지원서를 조회한다.")
// @Test
// public void getUnapprovedMemberApplication() throws Exception {
//
// // given
// List<AnswerDto> dtoList = new ArrayList<>();
// AnswerDto dto1 = new AnswerDto(
// 1L, "안녕하세요. 예 안녕히계세요.");
// dtoList.add(dto1);
//
// given(answerService.getAnswers(any())).willReturn(dtoList);
//
// // then
// mvc.perform(get("/members/1/application"))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$.[0].questionNo").value(equalTo(1)))
// .andExpect(jsonPath("$.[0].content").value(equalTo("안녕하세요. 예 안녕히계세요.")));
//
// }
given(answerService.getAnswers(any())).willReturn(dtoList);

// then
mvc.perform(get("/members/1/application"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.[0].questionId").value(equalTo(1)))
.andExpect(jsonPath("$.[0].content").value(equalTo("안녕하세요. 예 안녕히계세요.")));

}

@DisplayName("비활동 이상 모든 멤버 목록을 조회한다.")
@Test
Expand Down Expand Up @@ -125,11 +147,31 @@ public void getApprovedMembers() throws Exception {

}

@DisplayName("비활동 이상 멤버 권한 수정한다.")
@Test
public void updateApprovedMembers() throws Exception {

// 회원가입 이후 구현
@DisplayName("비활동 이상 멤버 권한을 변경할 때 가능한 권한이면 200, 아니면 400을 반환한다.")
@ParameterizedTest
@ValueSource(strings = {"ADMIN", "SIGNING_UP"})
public void updateApprovedMembers(String roleString) throws Exception {
//given
List<Long> memberIdList = List.of(1L);
List<Member> members = List.of(MemberTest.deactivatedMember());

if (roleString.equals("ADMIN")) {
//when
given(memberRepository.findAllById(memberIdList)).willReturn(members);
//then
mvc.perform(post("/members/approved")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonOf(new UpdateRoleRequestDto(memberIdList, Role.ADMIN))))
.andExpect(status().isNoContent());
} else if (roleString.equals("SIGNING_UP")) {
//when
doThrow(new IllegalArgumentException()).when(memberService).updateApprovedMembers(anyList(), any());
//then
mvc.perform(post("/members/approved")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonOf(new UpdateRoleRequestDto(memberIdList, Role.SIGNING_UP))))
.andExpect(status().isBadRequest());
}
}

@DisplayName("회장 연락처 정보를 불러온다")
Expand All @@ -147,4 +189,8 @@ public void getChiefContact() throws Exception {

}

private String jsonOf(Object o) throws JsonProcessingException {
return objectMapper.writeValueAsString(o);
}

}

0 comments on commit 0b00d7a

Please sign in to comment.