|
20 | 20 | import java.time.Duration;
|
21 | 21 | import java.util.function.Function;
|
22 | 22 |
|
| 23 | +import graphql.GraphqlErrorBuilder; |
23 | 24 | import io.micrometer.context.ContextSnapshot;
|
24 | 25 | import org.junit.jupiter.api.Nested;
|
25 | 26 | import org.junit.jupiter.api.Test;
|
26 | 27 | import org.junit.jupiter.params.ParameterizedTest;
|
27 | 28 | import org.junit.jupiter.params.provider.ValueSource;
|
| 29 | +import org.springframework.graphql.execution.DataFetcherExceptionResolver; |
| 30 | +import org.springframework.graphql.execution.ErrorType; |
28 | 31 | import reactor.core.publisher.Flux;
|
29 | 32 | import reactor.core.publisher.Mono;
|
30 | 33 | import reactor.test.StepVerifier;
|
@@ -63,6 +66,9 @@ public class SchemaMappingPrincipalMethodArgumentResolverTests {
|
63 | 66 | private final Function<Context, Context> reactiveContextWriter = context ->
|
64 | 67 | ReactiveSecurityContextHolder.withAuthentication(this.authentication);
|
65 | 68 |
|
| 69 | + private final Function<Context, Context> reactiveContextWriterWithoutAuthentication = context -> |
| 70 | + ReactiveSecurityContextHolder.withSecurityContext(Mono.just(SecurityContextHolder.createEmptyContext())); |
| 71 | + |
66 | 72 | private final Function<Context, Context> threadLocalContextWriter = context ->
|
67 | 73 | ContextSnapshot.captureAll().updateContext(context);
|
68 | 74 |
|
@@ -100,6 +106,68 @@ void resolveFromThreadLocalContext(String field) {
|
100 | 106 | }
|
101 | 107 | }
|
102 | 108 |
|
| 109 | + @Test |
| 110 | + void nullablePrincipalDoesntRequireSecurityContext() { |
| 111 | + Mono<ExecutionGraphQlResponse> responseMono = executeAsync( |
| 112 | + "type Query { greetingMonoNullable: String }", "{ greetingMonoNullable }", |
| 113 | + context -> context); |
| 114 | + |
| 115 | + ResponseHelper responseHelper = ResponseHelper.forResponse(responseMono); |
| 116 | + |
| 117 | + assertThat(responseHelper.errorCount()).isEqualTo(0); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void nonNullPrincipalRequiresSecurityContext() { |
| 122 | + DataFetcherExceptionResolver exceptionResolver = |
| 123 | + DataFetcherExceptionResolver.forSingleError((ex, env) -> GraphqlErrorBuilder.newError(env) |
| 124 | + .message("Resolved error: " + ex.getMessage()) |
| 125 | + .errorType(ErrorType.UNAUTHORIZED) |
| 126 | + .build()); |
| 127 | + |
| 128 | + Mono<ExecutionGraphQlResponse> responseMono = executeAsync( |
| 129 | + "type Query { greetingMono: String }", "{ greetingMono }", |
| 130 | + context -> context, |
| 131 | + exceptionResolver); |
| 132 | + |
| 133 | + ResponseHelper responseHelper = ResponseHelper.forResponse(responseMono); |
| 134 | + |
| 135 | + assertThat(responseHelper.errorCount()).isEqualTo(1); |
| 136 | + assertThat(responseHelper.error(0).errorType()).isEqualTo("UNAUTHORIZED"); |
| 137 | + assertThat(responseHelper.error(0).message()).isEqualTo("Resolved error: SecurityContext not available"); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void nonNullPrincipalRequiresAuthentication() { |
| 142 | + DataFetcherExceptionResolver exceptionResolver = |
| 143 | + DataFetcherExceptionResolver.forSingleError((ex, env) -> GraphqlErrorBuilder.newError(env) |
| 144 | + .message("Resolved error: " + ex.getMessage()) |
| 145 | + .errorType(ErrorType.UNAUTHORIZED) |
| 146 | + .build()); |
| 147 | + |
| 148 | + Mono<ExecutionGraphQlResponse> responseMono = executeAsync( |
| 149 | + "type Query { greetingMono: String }", "{ greetingMono }", |
| 150 | + reactiveContextWriterWithoutAuthentication, |
| 151 | + exceptionResolver); |
| 152 | + |
| 153 | + ResponseHelper responseHelper = ResponseHelper.forResponse(responseMono); |
| 154 | + |
| 155 | + assertThat(responseHelper.errorCount()).isEqualTo(1); |
| 156 | + assertThat(responseHelper.error(0).errorType()).isEqualTo("UNAUTHORIZED"); |
| 157 | + assertThat(responseHelper.error(0).message()).isEqualTo("Resolved error: An Authentication object was not found in the SecurityContext"); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + void nullablePrincipalDoesntRequireAuthentication() { |
| 162 | + Mono<ExecutionGraphQlResponse> responseMono = executeAsync( |
| 163 | + "type Query { greetingMonoNullable: String }", "{ greetingMonoNullable }", |
| 164 | + reactiveContextWriterWithoutAuthentication); |
| 165 | + |
| 166 | + ResponseHelper responseHelper = ResponseHelper.forResponse(responseMono); |
| 167 | + |
| 168 | + assertThat(responseHelper.errorCount()).isEqualTo(0); |
| 169 | + } |
| 170 | + |
103 | 171 | private void testQuery(String field, Function<Context, Context> contextWriter) {
|
104 | 172 | Mono<ExecutionGraphQlResponse> responseMono = executeAsync(
|
105 | 173 | "type Query { " + field + ": String }", "{ " + field + " }", contextWriter);
|
@@ -150,14 +218,24 @@ private void testSubscription(Function<Context, Context> contextModifier) {
|
150 | 218 |
|
151 | 219 | private Mono<ExecutionGraphQlResponse> executeAsync(
|
152 | 220 | String schema, String document, Function<Context, Context> contextWriter) {
|
| 221 | + return executeAsync(schema, document, contextWriter, null); |
| 222 | + } |
| 223 | + |
| 224 | + private Mono<ExecutionGraphQlResponse> executeAsync( |
| 225 | + String schema, String document, Function<Context, Context> contextWriter, @Nullable DataFetcherExceptionResolver exceptionResolver) { |
153 | 226 |
|
154 | 227 | AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
155 | 228 | context.registerBean(GreetingController.class, () -> greetingController);
|
156 | 229 | context.refresh();
|
157 | 230 |
|
158 |
| - TestExecutionGraphQlService graphQlService = GraphQlSetup.schemaContent(schema) |
159 |
| - .runtimeWiringForAnnotatedControllers(context) |
160 |
| - .toGraphQlService(); |
| 231 | + GraphQlSetup graphQlSetup = GraphQlSetup.schemaContent(schema) |
| 232 | + .runtimeWiringForAnnotatedControllers(context); |
| 233 | + |
| 234 | + if (exceptionResolver != null) { |
| 235 | + graphQlSetup.exceptionResolver(exceptionResolver); |
| 236 | + } |
| 237 | + |
| 238 | + TestExecutionGraphQlService graphQlService = graphQlSetup.toGraphQlService(); |
161 | 239 |
|
162 | 240 | return Mono.delay(Duration.ofMillis(10))
|
163 | 241 | .flatMap(aLong -> graphQlService.execute(document))
|
@@ -197,6 +275,12 @@ Mono<String> greetingMono(Principal principal) {
|
197 | 275 | return Mono.just("Hello");
|
198 | 276 | }
|
199 | 277 |
|
| 278 | + @QueryMapping |
| 279 | + Mono<String> greetingMonoNullable(@Nullable Principal principal) { |
| 280 | + this.principal = principal; |
| 281 | + return Mono.just("Hello"); |
| 282 | + } |
| 283 | + |
200 | 284 | @SubscriptionMapping
|
201 | 285 | Flux<String> greetingSubscription(Principal principal) {
|
202 | 286 | this.principal = principal;
|
|
0 commit comments