Skip to content

Commit

Permalink
Closes kadai-io#555 - Partially fix collision between @WithAccessId
Browse files Browse the repository at this point in the history
… and `@TestTemplate`
  • Loading branch information
bruderj15 committed Feb 17, 2025
1 parent ca64fe0 commit 7923518
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,16 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
List<WithAccessId> accessIds =
AnnotationSupport.findRepeatableAnnotations(context.getElement(), WithAccessId.class);
Store store = getMethodLevelStore(context);
return accessIds.stream()
.map(
a -> {
store.put(ACCESS_IDS_STORE_KEY, a);
return new JaasExtensionInvocationContext(a);
});

// Partial and only temporary workaround for kadai-io/kadai/#555
if (accessIds.size() > 1) {
return accessIds.stream()
.peek(a -> store.put(ACCESS_IDS_STORE_KEY, a))
.map(JaasExtensionInvocationContext::new);
} else {
accessIds.forEach(a -> store.put(ACCESS_IDS_STORE_KEY, a));
return Stream.empty();
}
}

private ExtensionContext getParentMethodExtensionContent(ExtensionContext extensionContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.kadai.common.internal.security.CurrentUserContextImpl;
import io.kadai.common.test.security.JaasExtensionTestExtensions.ShouldThrowJunitException;
import io.kadai.common.test.security.JaasExtensionTestExtensions.ShouldThrowParameterResolutionException;
import java.time.DayOfWeek;
import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;
Expand All @@ -34,6 +35,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DynamicContainer;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Nested;
Expand All @@ -43,6 +45,8 @@
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

@ExtendWith(JaasExtension.class)
class JaasExtensionTest {
Expand Down Expand Up @@ -217,8 +221,19 @@ List<DynamicTest> should_SetJaasSubject_When_AnnotationExists_On_TestFactory() {

// region JaasExtension#interceptTestTemplateMethod

@ParameterizedTest
@EnumSource(DayOfWeek.class)
@WithAccessId(user = "testtemplate")
void should_SetSaasSubject_When_SingleAnnotationExistsOnParameterizedTest(DayOfWeek dayOfWeek) {
assertThat(dayOfWeek).isNotNull();
assertThat(CURRENT_USER_CONTEXT.getUserid()).isEqualTo("testtemplate");
}

@WithAccessId(user = "testtemplate")
@TestTemplate
@Disabled("Disabled because of kadai-io/kadai/#555 and it's temporary partial fix which now "
+ "does not provide a TestTemplateInvocationContext anymore when exactly one @WithAccessId "
+ "is provided, while still setting the CURRENT_USER_CONTEXT.")
void should_SetJaasSubject_When_AnnotationExists_On_TestTemplate() {
assertThat(CURRENT_USER_CONTEXT.getUserid()).isEqualTo("testtemplate");
}
Expand All @@ -234,6 +249,9 @@ void should_SetMultipleJaasSubjects_When_MultipleAnnotationsExist_On_TestTemplat

@WithAccessId(user = "testtemplate1", groups = "abc", permissions = "perm")
@TestTemplate
@Disabled("Disabled because of kadai-io/kadai/#555 and it's temporary partial fix which now "
+ "does not provide a TestTemplateInvocationContext anymore when exactly one @WithAccessId "
+ "is provided, while still setting the CURRENT_USER_CONTEXT.")
void should_InjectCorrectAccessId_When_AnnotationExists_On_TestTemplate(WithAccessId accessId) {
assertThat(accessId.user()).isEqualTo("testtemplate1");
assertThat(accessId.groups()).containsExactly("abc");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,16 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
List<WithAccessId> accessIds =
AnnotationSupport.findRepeatableAnnotations(context.getElement(), WithAccessId.class);
Store store = getMethodLevelStore(context);
return accessIds.stream()
.peek(a -> store.put(ACCESS_IDS_STORE_KEY, a))
.map(JaasExtensionInvocationContext::new);

// Partial and only temporary workaround for kadai-io/kadai/#555
if (accessIds.size() > 1) {
return accessIds.stream()
.peek(a -> store.put(ACCESS_IDS_STORE_KEY, a))
.map(JaasExtensionInvocationContext::new);
} else {
accessIds.forEach(a -> store.put(ACCESS_IDS_STORE_KEY, a));
return Stream.empty();
}
}

private ExtensionContext getParentMethodExtensionContent(ExtensionContext extensionContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.kadai.common.internal.security.CurrentUserContextImpl;
import io.kadai.testapi.security.JaasExtensionTestExtensions.ShouldThrowJunitException;
import io.kadai.testapi.security.JaasExtensionTestExtensions.ShouldThrowParameterResolutionException;
import java.time.DayOfWeek;
import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;
Expand All @@ -34,6 +35,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DynamicContainer;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Nested;
Expand All @@ -43,6 +45,8 @@
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

@ExtendWith(JaasExtension.class)
class JaasExtensionTest {
Expand Down Expand Up @@ -217,8 +221,19 @@ List<DynamicTest> should_SetJaasSubject_When_AnnotationExists_On_TestFactory() {

// region JaasExtension#interceptTestTemplateMethod

@ParameterizedTest
@EnumSource(DayOfWeek.class)
@WithAccessId(user = "testtemplate")
void should_SetSaasSubject_When_SingleAnnotationExistsOnParameterizedTest(DayOfWeek dayOfWeek) {
assertThat(dayOfWeek).isNotNull();
assertThat(CURRENT_USER_CONTEXT.getUserid()).isEqualTo("testtemplate");
}

@WithAccessId(user = "testtemplate")
@TestTemplate
@Disabled("Disabled because of kadai-io/kadai/#555 and it's temporary partial fix which now "
+ "does not provide a TestTemplateInvocationContext anymore when exactly one @WithAccessId "
+ "is provided, while still setting the CURRENT_USER_CONTEXT.")
void should_SetJaasSubject_When_AnnotationExists_On_TestTemplate() {
assertThat(CURRENT_USER_CONTEXT.getUserid()).isEqualTo("testtemplate");
}
Expand All @@ -232,11 +247,15 @@ void should_SetMultipleJaasSubjects_When_MultipleAnnotationsExist_On_TestTemplat
assertThat(CURRENT_USER_CONTEXT.getUserid()).isEqualTo(accessId.user());
}

@WithAccessId(user = "testtemplate1", groups = "abc")
@WithAccessId(user = "testtemplate1", groups = "abc", permissions = "perm")
@TestTemplate
@Disabled("Disabled because of kadai-io/kadai/#555 and it's temporary partial fix which now "
+ "does not provide a TestTemplateInvocationContext anymore when exactly one @WithAccessId "
+ "is provided, while still setting the CURRENT_USER_CONTEXT.")
void should_InjectCorrectAccessId_When_AnnotationExists_On_TestTemplate(WithAccessId accessId) {
assertThat(accessId.user()).isEqualTo("testtemplate1");
assertThat(accessId.groups()).containsExactly("abc");
assertThat(accessId.permissions()).containsExactly("perm");
}

// endregion
Expand Down

0 comments on commit 7923518

Please sign in to comment.