Improve SpringSecurityTests

Sometimes TestSpanHandler does not return the spans in time so these tests could skip the verification steps and not fail even if they should.
These changes ensure that the tests will fail in these cases.
This commit is contained in:
Jonatan Ivanov
2022-05-19 20:22:56 -07:00
parent bc996c634d
commit f8e666d817

View File

@@ -16,11 +16,13 @@
package org.springframework.cloud.sleuth.instrument.security;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -69,8 +71,10 @@ public abstract class SpringSecurityTests {
then(entity.getStatusCode().is2xxSuccessful()).isTrue();
then(entity.getBody()).isEqualTo("authenticated");
Awaitility.await().atMost(Duration.ofSeconds(1)).untilAsserted(() -> then(testSpanHandler).isNotEmpty());
List<Map.Entry<Long, String>> authEvents = getAuthEvents(testSpanHandler.reportedSpans());
then(authEvents).isNotEmpty();
for (int i = 0; i < authEvents.size(); i += 2) {
String setEvent = authEvents.get(i).getValue();
then(setEvent).isEqualTo("Authentication set UsernamePasswordAuthenticationToken[USER]");
@@ -85,8 +89,10 @@ public abstract class SpringSecurityTests {
then(entity.getStatusCode().is2xxSuccessful()).isTrue();
then(entity.getBody()).contains("html", "form");
Awaitility.await().atMost(Duration.ofSeconds(1)).untilAsserted(() -> then(testSpanHandler).isNotEmpty());
List<Map.Entry<Long, String>> authEvents = getAuthEvents(testSpanHandler.reportedSpans());
then(authEvents).isNotEmpty();
for (int i = 0; i < authEvents.size(); i += 2) {
String setEvent = authEvents.get(i).getValue();
then(setEvent).isEqualTo("Authentication set AnonymousAuthenticationToken[ROLE_ANONYMOUS]");