476rick
476rick

Reputation: 2795

RestAssured: How to disable PreAuthorize when

I'm testing an API with RestAssured.
The methods I'm calling are using the next Annotation:

@PreAuthorize("hasAnyRole('ROLE1', 'ROLE2', 'ROLE3')")

My Test class contains an active profile for testing.
For the testing configuration we use a .yaml file.
Is it possible to put something in the .yaml file so we don't need to authorize when running the tests?

Or is there any other way so the PreAuthorize is not active when I'm running tests?

Upvotes: 3

Views: 3354

Answers (1)

Pär Nilsson
Pär Nilsson

Reputation: 2349

You could have a test profile in your code which you then activate when running tests against the code. You can then use the predefined user and password in your tests.

@Configuration
public class TestConfig {

  @EnableWebSecurity
  @Profile("test")
  class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService() {
      InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
      manager.createUser(User.withUsername("user").password("password").roles("ROLE1", "ROLE2", "ROLE3").build());
      return manager;
    }
  }
}

Upvotes: 1

Related Questions