Skip to content

Commit

Permalink
MOSIP-32842 Fixed test case failure
Browse files Browse the repository at this point in the history
Signed-off-by: kameshsr <[email protected]>
  • Loading branch information
kameshsr committed May 21, 2024
1 parent 0ed3c0b commit ebf2232
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -14,14 +16,16 @@
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.firewall.DefaultHttpFirewall;
import org.springframework.security.web.firewall.HttpFirewall;

import org.springframework.core.annotation.Order;
import jakarta.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@EnableMethodSecurity(prePostEnabled = true// , securedEnabled = true, jsr250Enabled = true
)
@Order(2)
public class TestSecurityConfig {

@Bean
Expand All @@ -35,6 +39,22 @@ protected SecurityFilterChain configureSecurityFilterChain(final HttpSecurity ht
return httpSecurity.build();
}

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().requestMatchers(allowedEndPoints()).and().httpFirewall(defaultHttpFirewall());
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf(csrf -> csrf.disable())
.exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedEntryPoint()))
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests((authz) -> authz.anyRequest().authenticated())
.userDetailsService(userDetailsService());

return http.build();
}

private String[] allowedEndPoints() {
return new String[]{"*", "/swagger-ui.html"};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

import io.mosip.pms.common.helper.FilterHelper;
import io.mosip.pms.common.helper.SearchHelper;
import io.mosip.pms.common.helper.WebSubPublisher;
import io.mosip.pms.common.util.PageUtils;
import io.mosip.pms.common.util.RestUtil;
import io.mosip.pms.common.validator.FilterColumnValidator;

/**
* @author Nagarjuna
*
*/
@Import(value = {WebSubPublisher.class,RestUtil.class,SearchHelper.class,FilterHelper.class,PageUtils.class,FilterColumnValidator.class,RestUtil.class})
@SpringBootApplication(scanBasePackages = { "io.mosip.pms.policy.*","io.mosip.pms.common.*"},
exclude={DataSourceAutoConfiguration.class})
@Import(value = { WebSubPublisher.class, SearchHelper.class, FilterHelper.class, PageUtils.class,
FilterColumnValidator.class })
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@ComponentScan(basePackages = { "io.mosip.pms.policy.*", "io.mosip.pms.common.*",
"io.mosip.kernel.websub.api.config", "io.mosip.kernel.templatemanager" })
public class PolicyServiceTest {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.http.ssl.TrustStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

Expand All @@ -20,6 +21,7 @@
public class TestConfig {

@Bean
@Primary
public RestTemplate restTemplateConfig()
throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public HttpFirewall defaultHttpFirewall() {

@Bean
protected SecurityFilterChain configure(final HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf(csrf -> csrf.disable());
httpSecurity.authorizeHttpRequests(cfg -> cfg.anyRequest().permitAll());
return httpSecurity.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
Expand Down Expand Up @@ -69,6 +70,7 @@
@SpringBootTest(classes = PolicyServiceTest.class)
@AutoConfigureMockMvc
@EnableWebMvc
@TestPropertySource("classpath:application.properties")
public class PolicyManagementControllerTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageImpl;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;

Expand Down Expand Up @@ -68,6 +69,7 @@

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("classpath:application.properties")
public class PolicyServiceTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ javax.persistence.jdbc.driver=org.h2.Driver
javax.persistence.jdbc.url = jdbc:h2:mem:mosip_pms;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM 'classpath:pmpTestschema.sql'
javax.persistence.jdbc.password = sa
javax.persistence.jdbc.user = postgres
hibernate.dialect=org.hibernate.dialect.H2Dialect

hibernate.jdbc.lob.non_contextual_creation=true
hibernate.hbm2ddl.auto=update
Expand Down

0 comments on commit ebf2232

Please sign in to comment.