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

Commit ab20598

Browse files
committed
chore(sonar): add unit test coverage
1 parent 91d59cf commit ab20598

File tree

7 files changed

+107
-2
lines changed

7 files changed

+107
-2
lines changed

graphql-spring-boot-autoconfigure/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ dependencies {
5454
testImplementation "org.springframework.boot:spring-boot-starter-web"
5555
testImplementation "org.springframework.boot:spring-boot-starter-actuator"
5656
testImplementation "org.springframework.boot:spring-boot-starter-webflux"
57-
// testImplementation "org.springframework.boot:spring-boot-starter-security"
57+
testImplementation "org.springframework.boot:spring-boot-starter-security"
5858
testImplementation "org.springframework.security:spring-security-test"
5959
testImplementation "io.projectreactor:reactor-core"
6060
testImplementation "io.reactivex.rxjava2:rxjava"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package graphql.kickstart.autoconfigure;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
7+
8+
@Configuration
9+
@EnableWebSecurity
10+
public class PermitAllWebSecurity extends WebSecurityConfigurerAdapter {
11+
12+
@Override
13+
protected void configure(final HttpSecurity http) throws Exception {
14+
http.authorizeRequests().anyRequest().permitAll();
15+
}
16+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package graphql.kickstart.autoconfigure.editor.voyager;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6+
7+
import graphql.kickstart.autoconfigure.PermitAllWebSecurity;
8+
import org.jsoup.Jsoup;
9+
import org.jsoup.nodes.Document;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.ExtendWith;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
14+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
15+
import org.springframework.boot.test.context.SpringBootTest;
16+
import org.springframework.context.annotation.Import;
17+
import org.springframework.test.context.ActiveProfiles;
18+
import org.springframework.test.context.junit.jupiter.SpringExtension;
19+
import org.springframework.test.web.servlet.MockMvc;
20+
import org.springframework.test.web.servlet.MvcResult;
21+
22+
@ExtendWith(SpringExtension.class)
23+
@Import(PermitAllWebSecurity.class)
24+
@SpringBootTest(classes = {VoyagerAutoConfiguration.class, ReactiveSecurityAutoConfiguration.class})
25+
@AutoConfigureMockMvc
26+
@ActiveProfiles("voyager")
27+
class VoyagerWithCsrfTest {
28+
29+
@Autowired private MockMvc mockMvc;
30+
31+
@Test
32+
void shouldLoadCSRFData() throws Exception {
33+
final MvcResult mvcResult =
34+
mockMvc.perform(get("/voyager")).andExpect(status().isOk()).andReturn();
35+
36+
final Document document = Jsoup.parse(mvcResult.getResponse().getContentAsString());
37+
var script = document.body().select("body script").dataNodes().get(0).getWholeData();
38+
assertThat(script)
39+
.contains("let csrf = {\"")
40+
.contains("\"token\":\"")
41+
.contains("\"parameterName\":\"_csrf\"")
42+
.contains("\"headerName\":\"X-CSRF-TOKEN\"");
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package graphql.kickstart.autoconfigure.editor.voyager;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6+
7+
import org.jsoup.Jsoup;
8+
import org.jsoup.nodes.Document;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.test.context.ActiveProfiles;
15+
import org.springframework.test.context.junit.jupiter.SpringExtension;
16+
import org.springframework.test.web.servlet.MockMvc;
17+
import org.springframework.test.web.servlet.MvcResult;
18+
19+
@ExtendWith(SpringExtension.class)
20+
@SpringBootTest(classes = {VoyagerAutoConfiguration.class})
21+
@AutoConfigureMockMvc
22+
@ActiveProfiles("voyager")
23+
class VoyagerWithoutCsrfTest {
24+
25+
@Autowired private MockMvc mockMvc;
26+
27+
@Test
28+
void shouldLoadCSRFData() throws Exception {
29+
final MvcResult mvcResult =
30+
mockMvc.perform(get("/voyager")).andExpect(status().isOk()).andReturn();
31+
32+
final Document document = Jsoup.parse(mvcResult.getResponse().getContentAsString());
33+
var script = document.body().select("body script").dataNodes().get(0).getWholeData();
34+
assertThat(script).contains("let csrf = null");
35+
}
36+
}

graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/web/reactive/MonoAutoConfigurationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.junit.jupiter.api.Test;
1010
import org.junit.jupiter.api.extension.ExtendWith;
1111
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
13+
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
1214
import org.springframework.boot.test.context.SpringBootTest;
1315
import org.springframework.http.MediaType;
1416
import org.springframework.test.context.junit.jupiter.SpringExtension;

graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/web/reactive/MonoGenericWrapperAlreadyDefinedTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import graphql.kickstart.tools.SchemaParserOptions.GenericWrapper;
66
import lombok.RequiredArgsConstructor;
7+
import lombok.extern.slf4j.Slf4j;
78
import lombok.val;
89
import org.json.JSONException;
910
import org.json.JSONObject;
@@ -18,6 +19,7 @@
1819
import org.springframework.test.web.reactive.server.WebTestClient;
1920
import reactor.core.publisher.Mono;
2021

22+
@Slf4j
2123
@RequiredArgsConstructor
2224
@ExtendWith(SpringExtension.class)
2325
@SpringBootTest(
@@ -41,6 +43,7 @@ void monoWrapper() throws JSONException {
4143
.exchange()
4244
.returnResult(String.class);
4345
val response = result.getResponseBody().blockFirst();
46+
log.info("Response: {}", response);
4447
val json = new JSONObject(response);
4548
assertThat(json.getJSONObject("data").get("hello")).isEqualTo("Hello world");
4649
}

graphql-spring-boot-autoconfigure/src/test/resources/application.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ server:
33
forward-headers-strategy: framework
44
spring:
55
autoconfigure:
6-
exclude: org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
6+
exclude:
7+
- org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
8+
- org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration
9+
- org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration
10+
- org.springframework.boot.actuate.autoconfigure.security.reactive.ReactiveManagementWebSecurityAutoConfiguration

0 commit comments

Comments
 (0)