Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 9a592f0

Browse files
committed
chore(tests): add voyager reactive tests
1 parent 0fe225f commit 9a592f0

File tree

9 files changed

+95
-136
lines changed

9 files changed

+95
-136
lines changed

graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/editor/voyager/ReactiveVoyagerAutoConfiguration.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
/** @author Max David Günther */
1414
@Configuration
1515
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
16-
@ConditionalOnProperty(
17-
value = "graphql.voyager.enabled",
18-
havingValue = "true",
19-
matchIfMissing = true)
16+
@ConditionalOnProperty(value = "graphql.voyager.enabled", havingValue = "true")
2017
@EnableConfigurationProperties(VoyagerPropertiesConfiguration.class)
2118
public class ReactiveVoyagerAutoConfiguration {
2219

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package graphql.kickstart.autoconfigure.editor.voyager;
22

3-
import static graphql.kickstart.autoconfigure.editor.EditorConstants.CSRF_ATTRIBUTE_NAME;
4-
53
import java.io.IOException;
64
import java.util.Map;
75
import lombok.RequiredArgsConstructor;
86
import org.springframework.beans.factory.annotation.Autowired;
97
import org.springframework.http.MediaType;
108
import org.springframework.http.ResponseEntity;
9+
import org.springframework.security.web.server.csrf.CsrfToken;
1110
import org.springframework.stereotype.Controller;
1211
import org.springframework.web.bind.annotation.GetMapping;
1312
import org.springframework.web.bind.annotation.PathVariable;
14-
import org.springframework.web.bind.annotation.RequestAttribute;
13+
import org.springframework.web.server.ServerWebExchange;
14+
import reactor.core.publisher.Mono;
1515

1616
/** @author Max David Günther */
1717
@Controller
@@ -21,14 +21,23 @@ public class ReactiveVoyagerController {
2121
@Autowired private VoyagerIndexHtmlTemplate indexTemplate;
2222

2323
@GetMapping(path = "${graphql.voyager.mapping:/voyager}")
24-
public ResponseEntity<String> voyager(
25-
final @RequestAttribute(value = CSRF_ATTRIBUTE_NAME, required = false) Object csrf,
26-
@PathVariable Map<String, String> params)
27-
throws IOException {
24+
public Mono<ResponseEntity<String>> voyager(
25+
ServerWebExchange exchange, @PathVariable Map<String, String> params) {
2826
// no context path in spring-webflux
29-
String indexHtmlContent = indexTemplate.fillIndexTemplate("", csrf, params);
30-
return ResponseEntity.ok()
31-
.contentType(MediaType.valueOf("text/html; charset=UTF-8"))
32-
.body(indexHtmlContent);
27+
Mono<CsrfToken> csrfToken = exchange.getAttribute(CsrfToken.class.getName());
28+
return csrfToken != null
29+
? csrfToken.map(csrf -> fillTemplate(csrf, params))
30+
: Mono.just(fillTemplate(null, params));
31+
}
32+
33+
private ResponseEntity<String> fillTemplate(CsrfToken csrf, Map<String, String> params) {
34+
try {
35+
String indexHtmlContent = indexTemplate.fillIndexTemplate("", csrf, params);
36+
return ResponseEntity.ok()
37+
.contentType(MediaType.valueOf("text/html; charset=UTF-8"))
38+
.body(indexHtmlContent);
39+
} catch (IOException e) {
40+
return ResponseEntity.status(500).body(null);
41+
}
3342
}
3443
}

graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/editor/voyager/VoyagerAutoConfiguration.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
/** @author Guilherme Blanco */
1010
@Configuration
1111
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
12-
@ConditionalOnProperty(
13-
value = "graphql.voyager.enabled",
14-
havingValue = "true",
15-
matchIfMissing = true)
12+
@ConditionalOnProperty(value = "graphql.voyager.enabled", havingValue = "true")
1613
@EnableConfigurationProperties(VoyagerPropertiesConfiguration.class)
1714
public class VoyagerAutoConfiguration {
1815

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package graphql.kickstart.autoconfigure;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
6+
import org.springframework.security.config.web.server.ServerHttpSecurity;
7+
import org.springframework.security.web.server.SecurityWebFilterChain;
8+
import org.springframework.web.reactive.config.EnableWebFlux;
9+
10+
@EnableWebFlux
11+
@Configuration
12+
@EnableWebFluxSecurity
13+
public class PermitAllWebFluxSecurity {
14+
15+
@Bean
16+
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
17+
return http.authorizeExchange().anyExchange().permitAll().and().csrf().and().build();
18+
}
19+
}

graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/voyager/ReactiveVoyagerControllerTest.java

Lines changed: 0 additions & 59 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package graphql.kickstart.autoconfigure.editor.voyager;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import graphql.kickstart.autoconfigure.PermitAllWebFluxSecurity;
6+
import org.jsoup.Jsoup;
7+
import org.jsoup.nodes.Document;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
12+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.context.annotation.Import;
15+
import org.springframework.test.context.ActiveProfiles;
16+
import org.springframework.test.context.junit.jupiter.SpringExtension;
17+
import org.springframework.test.web.reactive.server.WebTestClient;
18+
19+
@Import(PermitAllWebFluxSecurity.class)
20+
@ExtendWith(SpringExtension.class)
21+
@SpringBootTest(
22+
classes = {ReactiveVoyagerAutoConfiguration.class, ReactiveSecurityAutoConfiguration.class},
23+
properties = {"graphql.voyager.enabled=true", "spring.main.web-application-type=reactive"})
24+
@AutoConfigureWebTestClient
25+
@ActiveProfiles("voyager")
26+
class ReactiveVoyagerWithCsrfTest {
27+
28+
@Autowired private WebTestClient webTestClient;
29+
30+
@Test
31+
void shouldLoadCSRFData() {
32+
final String responseBody =
33+
webTestClient
34+
.get()
35+
.uri("/voyager")
36+
.exchange()
37+
.expectStatus()
38+
.isOk()
39+
.expectBody(String.class)
40+
.returnResult()
41+
.getResponseBody();
42+
43+
final Document document = Jsoup.parse(responseBody);
44+
final String script = document.body().select("body script").dataNodes().get(0).getWholeData();
45+
assertThat(script)
46+
.contains("let csrf = {\"")
47+
.contains("\"token\":\"")
48+
.contains("\"parameterName\":\"_csrf\"")
49+
.contains("\"headerName\":\"X-CSRF-TOKEN\"");
50+
}
51+
}

graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/voyager/VoyagerControllerTest.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/voyager/VoyagerWithCsrfTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
@Import(PermitAllWebSecurity.class)
2424
@SpringBootTest(
2525
classes = {VoyagerAutoConfiguration.class, SecurityAutoConfiguration.class},
26-
properties = {"spring.main.web-application-type=servlet"})
26+
properties = {"graphql.voyager.enabled=true", "spring.main.web-application-type=servlet"})
2727
@AutoConfigureMockMvc
2828
@ActiveProfiles("voyager")
2929
class VoyagerWithCsrfTest {

graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/voyager/VoyagerWithoutCsrfTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1313
import org.springframework.boot.test.context.SpringBootTest;
1414
import org.springframework.test.context.ActiveProfiles;
15+
import org.springframework.test.context.TestPropertySource;
1516
import org.springframework.test.context.junit.jupiter.SpringExtension;
1617
import org.springframework.test.web.servlet.MockMvc;
1718
import org.springframework.test.web.servlet.MvcResult;
@@ -20,6 +21,7 @@
2021
@SpringBootTest(classes = {VoyagerAutoConfiguration.class})
2122
@AutoConfigureMockMvc
2223
@ActiveProfiles("voyager")
24+
@TestPropertySource("classpath:enabled-config.properties")
2325
class VoyagerWithoutCsrfTest {
2426

2527
@Autowired private MockMvc mockMvc;

0 commit comments

Comments
 (0)