Skip to content

Commit

Permalink
[refactor/InhaBas#179] testConfig 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
whitem4rk committed Dec 9, 2023
1 parent f5cefc6 commit bbc44a7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class SignUpDto {
private String phoneNumber;

@NotNull
@Pattern(regexp = "\\d+")
@Schema(example = "12171707")
private String studentId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ private String jsonOf(Object response) throws JsonProcessingException {
.getResponse().getContentAsString(StandardCharsets.UTF_8);

assertThat(response.split("\n")).containsExactlyInAnyOrder(
"[memberId](은)는 must not be null 입력된 값: [null]",
"[studentId](은)는 must not be null 입력된 값: [null]",
"[major](은)는 must not be blank 입력된 값: []",
"[name](은)는 must not be blank 입력된 값: []",
"[phoneNumber](은)는 must match \"\\d{3}-\\d{4}-\\d{4}\" 입력된 값: []",
"[phoneNumber](은)는 must match \"^(010)-\\d{4}-\\d{4}$\" 입력된 값: []",
"[memberType](은)는 must not be null 입력된 값: [null]");
}

Expand All @@ -140,7 +140,7 @@ private String jsonOf(Object response) throws JsonProcessingException {
.name("홍길동만세".repeat(10) + ".") // 50자까지만 가능
.major("금융데이터처리, 블록체인학과.") // 50자가지만 가능
.phoneNumber("8210-1111-1111") // ^(010)-\d{4}-\d{4}$
.studentId("-1")
.studentId("123123123")
.memberType(MemberType.UNDERGRADUATE)
.build();

Expand All @@ -153,10 +153,8 @@ private String jsonOf(Object response) throws JsonProcessingException {
.getResponse().getContentAsString(StandardCharsets.UTF_8);

assertThat(response.split("\n")).containsExactlyInAnyOrder(
"[memberId](은)는 must be greater than 0 입력된 값: [-1]",
"[phoneNumber](은)는 must match \"\\d{3}-\\d{4}-\\d{4}\" 입력된 값: [8210-1111-1111]",
"[name](은)는 length must be between 0 and 25 입력된 값: [홍길동만세홍길동만세홍길동만세홍길동만세홍길동만세.]",
"[major](은)는 length must be between 0 and 15 입력된 값: [금융데이터처리, 블록체인학과.]");
"[phoneNumber](은)는 must match \"^(010)-\\d{4}-\\d{4}$\" 입력된 값: [8210-1111-1111]",
"[name](은)는 length must be between 0 and 50 입력된 값: [홍길동만세홍길동만세홍길동만세홍길동만세홍길동만세홍길동만세홍길동만세홍길동만세홍길동만세홍길동만세.]");

}

Expand Down Expand Up @@ -303,7 +301,10 @@ private String jsonOf(Object response) throws JsonProcessingException {
}};

//when
mvc.perform(put("/signUp").with(csrf()))
mvc.perform(put("/signUp")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(jsonOf(submittedAnswers)))
.andExpect(status().isNoContent())
.andReturn();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@ActiveProfiles({"production"})
@ActiveProfiles({"test"})
@Transactional
@AutoConfigureMockMvc
@SpringBootTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.inhabas.testAnnotataion;

import com.inhabas.api.auth.AuthBeansConfig;
import com.inhabas.api.auth.domain.oauth2.member.security.DefaultRoleHierarchy;
import com.inhabas.api.config.WebSecurityConfig;
import com.inhabas.testConfig.TestConfigurationForSecurity;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -21,7 +23,7 @@
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@ActiveProfiles("default_mvc_test") // for disable cloud config & security filter chain
@ActiveProfiles("test") // for disable cloud config & security filter chain
@WebMvcTest(excludeAutoConfiguration = {OAuth2ClientAutoConfiguration.class}) // disable autoload OAuth2-Client-Components from test properties
@Import({DefaultRoleHierarchy.class, TestConfigurationForSecurity.class})
public @interface DefaultWebMvcTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
package com.inhabas.testConfig;

import com.inhabas.api.auth.domain.token.TokenResolver;
import com.inhabas.api.auth.domain.token.securityFilter.TokenAuthenticationFailureHandler;
import com.inhabas.api.auth.domain.token.securityFilter.UserPrincipalService;
import com.inhabas.api.auth.AuthBeansConfig;
import com.inhabas.api.auth.domain.token.CustomRequestMatcher;
import com.inhabas.api.auth.domain.token.JwtAccessDeniedHandler;
import com.inhabas.api.auth.domain.token.jwtUtils.JwtAuthenticationProvider;
import com.inhabas.api.auth.domain.token.jwtUtils.JwtTokenUtil;
import com.inhabas.api.auth.domain.token.securityFilter.JwtAuthenticationEntryPoint;
import com.inhabas.api.auth.domain.token.securityFilter.JwtAuthenticationFilter;
import com.inhabas.api.web.interceptor.InterceptorConfig;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.web.util.matcher.RequestMatcher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@TestConfiguration
public class TestConfigurationForSecurity {

@MockBean
InterceptorConfig interceptorConfig;

@MockBean
TokenResolver tokenResolver;

JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
@MockBean
JwtAccessDeniedHandler jwtAccessDeniedHandler;
@MockBean
JwtTokenUtil jwtTokenUtil;
@MockBean
JwtAuthenticationProvider jwtAuthenticationProvider;
@MockBean
TokenAuthenticationFailureHandler failureHandler;
AuthBeansConfig authBeansConfig;

@MockBean
UserPrincipalService userPrincipalService;
private AuthenticationManager authenticationManager;

@Bean
public JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
final List<String> skipPaths = new ArrayList<>();
skipPaths.add("/**");
final RequestMatcher requestMatcher = new CustomRequestMatcher(skipPaths);
final JwtAuthenticationFilter filter = new JwtAuthenticationFilter(
requestMatcher,
jwtTokenUtil,
authBeansConfig.tokenResolver()
);
filter.setAuthenticationManager(authenticationManager);
return filter;
}
}

0 comments on commit bbc44a7

Please sign in to comment.