Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partially closes #555 - Partially fix collision between @WithAccessId and @TestTemplate #578

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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