-
-
Notifications
You must be signed in to change notification settings - Fork 80
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
Support for parametrized OAuth2 Authentications in @ParameterizedTest
#122
Comments
I'm not sure that what you ask is possible: as per its name, If you only test @ParameterizedTest
@ValueSource(strings = { "NICE", "VERY_NICE" })
void givenUserIsGrantedWithAnyNiceAuthority_whenGetRestricted_thenOk(String authority) throws Exception {
api.perform(get("/restricted").with(SecurityMockMvcRequestPostProcessors.jwt().authorities(new SimpleGrantedAuthority(authority))))
.andExpect(status().isOk())
.andExpect(jsonPath("$.body").value("You are so nice!"));
} I will investigate how complicated it would be to write an equivalent of |
Here is what I came to so far: @ParameterizedTest
@JwtAuthenticationSource({ @WithMockJwtAuth(authorities = "NICE"), @WithMockJwtAuth(authorities = "VERY_NICE") })
void givenUserIsGrantedWithAnyNiceAuthentication_whenGetRestricted_thenOk(@JwtAuth JwtAuthenticationToken auth) throws Exception {
api.perform(get("/restricted"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.body").value("You are so nice!"));
} Mind the
@thekalinga what do you think of this solution? This relies on the following: @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@ArgumentsSource(JwtAuthenticationSource.JwtAuthenticationsProvider.class)
public @interface JwtAuthenticationSource {
WithMockJwtAuth[] value() default {};
static class JwtAuthenticationsProvider implements ArgumentsProvider, AnnotationConsumer<JwtAuthenticationSource> {
private final WithMockJwtAuth.JwtAuthenticationTokenFactory authFactory = new WithMockJwtAuth.JwtAuthenticationTokenFactory();
private Collection<JwtAuthenticationToken> arguments;
@Override
public void accept(JwtAuthenticationSource source) {
// @formatter:off
arguments =
Stream.of(source.value())
.map(authFactory::authentication)
.toList();
}
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return arguments.stream().map(Arguments::of);
}
}
} public class JwtAuthenticationArgumentProcessor extends TypedArgumentConverter<JwtAuthenticationToken, JwtAuthenticationToken> {
protected JwtAuthenticationArgumentProcessor() {
super(JwtAuthenticationToken.class, JwtAuthenticationToken.class);
}
@Override
protected JwtAuthenticationToken convert(JwtAuthenticationToken source) {
SecurityContextHolder.getContext().setAuthentication(source);
return source;
}
} @Target({ ElementType.ANNOTATION_TYPE, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@ConvertWith(JwtAuthenticationArgumentProcessor.class)
public @interface JwtAuth {
} |
@ParameterizedTest
Released in |
Is your feature request related to a problem? Please describe.
If an endpoint is accessible to users with multiple authorities, I have to create one test per role and put same annotations on each of them method except authority.
Describe the solution you'd like
I would want to use junit's
ParameterizedTest
to pass this informationDescribe alternatives you've considered
None as I am still a noob with this framework.
The text was updated successfully, but these errors were encountered: