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