-
Notifications
You must be signed in to change notification settings - Fork 870
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add spring-security-config-6.0 java agent tests
Switch HttpSecurity instrumentation to performBuild(), since build() is on a superclass
- Loading branch information
Showing
4 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
.../instrumentation/spring/security/config/v6_0/servlet/HttpSecurityInstrumentationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.spring.security.config.v6_0.servlet; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.instrumentation.spring.security.config.v6_0.servlet.EnduserAttributesCapturingServletFilter; | ||
import java.util.Collections; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.ObjectPostProcessor; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.web.DefaultSecurityFilterChain; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@ExtendWith(SpringExtension.class) | ||
class HttpSecurityInstrumentationTest { | ||
|
||
@Configuration | ||
static class TestConfiguration {} | ||
|
||
@Mock ObjectPostProcessor<Object> objectPostProcessor; | ||
|
||
/** | ||
* Ensures that {@link HttpSecurityInstrumentation} registers a {@link | ||
* EnduserAttributesCapturingServletFilter} in the filter chain. | ||
* | ||
* <p>Usage of the filter is covered in other unit tests. | ||
*/ | ||
@Test | ||
void ensureFilterRegistered(@Autowired ApplicationContext applicationContext) throws Exception { | ||
|
||
AuthenticationManagerBuilder authenticationBuilder = | ||
new AuthenticationManagerBuilder(objectPostProcessor); | ||
|
||
HttpSecurity httpSecurity = | ||
new HttpSecurity( | ||
objectPostProcessor, | ||
authenticationBuilder, | ||
Collections.singletonMap(ApplicationContext.class, applicationContext)); | ||
|
||
DefaultSecurityFilterChain filterChain = httpSecurity.build(); | ||
|
||
assertThat(filterChain.getFilters()) | ||
.filteredOn( | ||
item -> | ||
item.getClass() | ||
.getName() | ||
.endsWith(EnduserAttributesCapturingServletFilter.class.getSimpleName())) | ||
.hasSize(1); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...umentation/spring/security/config/v6_0/webflux/ServerHttpSecurityInstrumentationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.spring.security.config.v6_0.webflux; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.instrumentation.spring.security.config.v6_0.webflux.EnduserAttributesCapturingWebFilter; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.security.config.web.server.ServerHttpSecurity; | ||
import org.springframework.security.web.server.SecurityWebFilterChain; | ||
|
||
class ServerHttpSecurityInstrumentationTest { | ||
|
||
/** | ||
* Ensures that {@link ServerHttpSecurityInstrumentation} registers a {@link | ||
* EnduserAttributesCapturingWebFilter} in the filter chain. | ||
* | ||
* <p>Usage of the filter is covered in other unit tests. | ||
*/ | ||
@Test | ||
void ensureFilterRegistered() { | ||
|
||
ServerHttpSecurity serverHttpSecurity = ServerHttpSecurity.http(); | ||
|
||
SecurityWebFilterChain securityWebFilterChain = serverHttpSecurity.build(); | ||
|
||
assertThat(securityWebFilterChain.getWebFilters().collectList().block()) | ||
.filteredOn( | ||
item -> | ||
item.getClass() | ||
.getName() | ||
.endsWith(EnduserAttributesCapturingWebFilter.class.getSimpleName())) | ||
.hasSize(1); | ||
} | ||
} |