@ -22,12 +22,14 @@ import org.springframework.boot.actuate.audit.AuditEvent;
import org.springframework.boot.actuate.audit.AuditEventRepository ;
import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository ;
import org.springframework.boot.actuate.audit.listener.AbstractAuditListener ;
import org.springframework.boot.actuate.audit.listener.AuditListener ;
import org.springframework.boot.actuate.security.AbstractAuthenticationAuditListener ;
import org.springframework.boot.actuate.security.AbstractAuthorizationAuditListener ;
import org.springframework.boot.actuate.security.AuthenticationAuditListener ;
import org.springframework.boot.actuate.security.AuthorizationAuditListener ;
import org.springframework.boot.autoconfigure.AutoConfigurations ;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner ;
import org.springframework.context.ApplicationEventPublisher ;
import org.springframework.context.annotation.AnnotationConfigApplicationContext ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.security.access.event.AbstractAuthorizationEvent ;
@ -40,54 +42,71 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Dave Syer
* @author Vedran Pavic
* @author Madhura Bhave
* /
public class AuditAutoConfigurationTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner ( )
. withConfiguration ( AutoConfigurations . of ( AuditAutoConfiguration . class ) ) ;
@Test
public void defaultConfiguration ( ) {
registerAndRefresh ( AuditAutoConfiguration . class ) ;
assertThat ( this . context . getBean ( AuditEventRepository . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( AuthenticationAuditListener . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( AuthorizationAuditListener . class ) ) . isNotNull ( ) ;
public void autoConfigurationIsDisabledByDefault ( ) {
this . contextRunner . run ( ( context ) - > assertThat ( context )
. doesNotHaveBean ( AuditAutoConfiguration . class ) ) ;
}
@Test
public void ownAuditEventRepository ( ) {
registerAndRefresh ( CustomAuditEventRepositoryConfiguration . class ,
AuditAutoConfiguration . class ) ;
assertThat ( this . context . getBean ( AuditEventRepository . class ) )
. isInstanceOf ( TestAuditEventRepository . class ) ;
public void autoConfigurationIsEnabledWhenAuditEventRepositoryBeanPresent ( ) {
this . contextRunner
. withUserConfiguration ( CustomAuditEventRepositoryConfiguration . class )
. run ( ( context ) - > {
assertThat ( context . getBean ( AuditEventRepository . class ) ) . isNotNull ( ) ;
assertThat ( context . getBean ( AuthenticationAuditListener . class ) )
. isNotNull ( ) ;
assertThat ( context . getBean ( AuthorizationAuditListener . class ) )
. isNotNull ( ) ;
} ) ;
}
@Test
public void ownAuthenticationAuditListener ( ) {
registerAndRefresh ( CustomAuthenticationAuditListenerConfiguration . class ,
AuditAutoConfiguration . class ) ;
assertThat ( this . context . getBean ( AbstractAuthenticationAuditListener . class ) )
. isInstanceOf ( TestAuthenticationAuditListener . class ) ;
this . contextRunner
. withUserConfiguration ( CustomAuditEventRepositoryConfiguration . class )
. withUserConfiguration (
CustomAuthenticationAuditListenerConfiguration . class )
. run ( ( context ) - > assertThat (
context . getBean ( AbstractAuthenticationAuditListener . class ) )
. isInstanceOf ( TestAuthenticationAuditListener . class ) ) ;
}
@Test
public void ownAuthorizationAuditListener ( ) {
registerAndRefresh ( CustomAuthorizationAuditListenerConfiguration . class ,
AuditAutoConfiguration . class ) ;
assertThat ( this . context . getBean ( AbstractAuthorizationAuditListener . class ) )
. isInstanceOf ( TestAuthorizationAuditListener . class ) ;
this . contextRunner
. withUserConfiguration ( CustomAuditEventRepositoryConfiguration . class )
. withUserConfiguration (
CustomAuthorizationAuditListenerConfiguration . class )
. run ( ( context ) - > assertThat (
context . getBean ( AbstractAuthorizationAuditListener . class ) )
. isInstanceOf ( TestAuthorizationAuditListener . class ) ) ;
}
@Test
public void ownAuditListener ( ) {
registerAndRefresh ( CustomAuditListenerConfiguration . class ,
AuditAutoConfiguration . class ) ;
assertThat ( this . context . getBean ( AbstractAuditListener . class ) )
. isInstanceOf ( TestAuditListener . class ) ;
this . contextRunner
. withUserConfiguration ( CustomAuditEventRepositoryConfiguration . class )
. withUserConfiguration ( CustomAuditListenerConfiguration . class )
. run ( ( context ) - > assertThat ( context . getBean ( AbstractAuditListener . class ) )
. isInstanceOf ( TestAuditListener . class ) ) ;
}
private void registerAndRefresh ( Class < ? > . . . annotatedClasses ) {
this . context . register ( annotatedClasses ) ;
this . context . refresh ( ) ;
@Test
public void backsOffWhenDisabled ( ) {
this . contextRunner
. withUserConfiguration ( CustomAuditEventRepositoryConfiguration . class )
. withPropertyValues ( "management.auditevents.enabled=false" )
. run ( ( context ) - > assertThat ( context ) . doesNotHaveBean ( AuditListener . class )
. doesNotHaveBean ( AuthenticationAuditListener . class )
. doesNotHaveBean ( AuthorizationAuditListener . class ) ) ;
}
@Configuration ( proxyBeanMethods = false )