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

Commit 4cf9a14

Browse files
test(#212): improved test suite for Playground Starter
1 parent 9028135 commit 4cf9a14

22 files changed

+501
-5
lines changed

playground-spring-boot-autoconfigure/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ dependencies{
2525

2626
testCompile "org.springframework.boot:spring-boot-starter-web:$LIB_SPRING_BOOT_VER"
2727
testCompile "org.springframework.boot:spring-boot-starter-test:$LIB_SPRING_BOOT_VER"
28+
testCompile "org.springframework.boot:spring-boot-starter-security:$LIB_SPRING_BOOT_VER"
29+
testCompile "org.jsoup:jsoup:1.12.1"
2830
}
2931

3032
compileJava.dependsOn(processResources)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.oembedler.moon.playground.boot;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.security.web.csrf.CsrfToken;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.springframework.test.web.servlet.MvcResult;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
16+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
18+
19+
@RunWith(SpringRunner.class)
20+
@SpringBootTest(classes = PlaygroundTestConfig.class)
21+
@AutoConfigureMockMvc
22+
public class PlaygroundCSRFTest {
23+
24+
private static final String CSRF_ATTRIBUTE_NAME = "_csrf";
25+
26+
@Autowired
27+
private MockMvc mockMvc;
28+
29+
@Autowired
30+
private ObjectMapper objectMapper;
31+
32+
@Test
33+
public void shouldLoadCSRFData() throws Exception {
34+
final MvcResult mvcResult = mockMvc.perform(get(PlaygroundTestHelper.DEFAULT_PLAYGROUND_ENDPOINT))
35+
.andExpect(status().isOk())
36+
.andExpect(model().attributeExists(CSRF_ATTRIBUTE_NAME))
37+
.andReturn();
38+
39+
assertThat(mvcResult.getModelAndView()).isNotNull();
40+
assertThat(mvcResult.getModelAndView().getModel()).isNotNull();
41+
assertThat(mvcResult.getModelAndView().getModel().get(CSRF_ATTRIBUTE_NAME)).isInstanceOf(CsrfToken.class);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.oembedler.moon.playground.boot;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.TestPropertySource;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
10+
@RunWith(SpringRunner.class)
11+
@SpringBootTest(classes = PlaygroundTestConfig.class)
12+
@AutoConfigureMockMvc
13+
@TestPropertySource("classpath:application-playground-cdn-custom-version-test.properties")
14+
public class PlaygroundCdnCustomVersionTest extends PlaygroundResourcesTestBase {
15+
16+
private final String CSS_CDN_PATH
17+
= "https://cdn.jsdelivr.net/npm/[email protected]/build/static/css/index.css";
18+
private final String SCRIPT_CDN_PATH
19+
= "https://cdn.jsdelivr.net/npm/[email protected]/build/static/js/middleware.js";
20+
private final String FAVICON_CDN_PATH
21+
= "https://cdn.jsdelivr.net/npm/[email protected]/build/favicon.png";
22+
private final String LOGO_CDN_PATH
23+
= "https://cdn.jsdelivr.net/npm/[email protected]/build/logo.png";
24+
25+
@Test
26+
public void shouldLoadSpecifiedVersionFromCdn() throws Exception {
27+
testPlaygroundResources(CSS_CDN_PATH, SCRIPT_CDN_PATH, FAVICON_CDN_PATH, LOGO_CDN_PATH);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.oembedler.moon.playground.boot;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.TestPropertySource;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
10+
@RunWith(SpringRunner.class)
11+
@SpringBootTest(classes = PlaygroundTestConfig.class)
12+
@AutoConfigureMockMvc
13+
@TestPropertySource("classpath:application-playground-cdn-test.properties")
14+
public class PlaygroundCdnTest extends PlaygroundResourcesTestBase {
15+
16+
private final String CSS_CDN_PATH
17+
= "https://cdn.jsdelivr.net/npm/graphql-playground-react@latest/build/static/css/index.css";
18+
private final String SCRIPT_CDN_PATH
19+
= "https://cdn.jsdelivr.net/npm/graphql-playground-react@latest/build/static/js/middleware.js";
20+
private final String FAVICON_CDN_PATH
21+
= "https://cdn.jsdelivr.net/npm/graphql-playground-react@latest/build/favicon.png";
22+
private final String LOGO_CDN_PATH
23+
= "https://cdn.jsdelivr.net/npm/graphql-playground-react@latest/build/logo.png";
24+
25+
@Test
26+
public void shouldLoadLatestVersionFromCdn() throws Exception {
27+
testPlaygroundResources(CSS_CDN_PATH, SCRIPT_CDN_PATH, FAVICON_CDN_PATH, LOGO_CDN_PATH);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.oembedler.moon.playground.boot;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.test.context.TestPropertySource;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
13+
import java.nio.charset.StandardCharsets;
14+
15+
import static org.hamcrest.Matchers.isEmptyString;
16+
import static org.hamcrest.Matchers.not;
17+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
18+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
20+
21+
@RunWith(SpringRunner.class)
22+
@SpringBootTest(classes = PlaygroundTestConfig.class)
23+
@AutoConfigureMockMvc
24+
@TestPropertySource("classpath:application-playground-mapping-test.properties")
25+
public class PlaygroundCustomMappingTest {
26+
27+
private static final String CONFIGURED_MAPPING = "/test-mapping";
28+
29+
@Autowired
30+
private MockMvc mockMvc;
31+
32+
@Test
33+
public void shouldUseTheConfiguredRequestMapping() throws Exception {
34+
mockMvc.perform(get(CONFIGURED_MAPPING))
35+
.andExpect(status().isOk())
36+
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
37+
.andExpect(content().encoding(StandardCharsets.UTF_8.name()))
38+
.andExpect(content().string(not(isEmptyString())));
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.oembedler.moon.playground.boot;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.TestPropertySource;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
10+
@RunWith(SpringRunner.class)
11+
@SpringBootTest(classes = PlaygroundTestConfig.class)
12+
@AutoConfigureMockMvc
13+
@TestPropertySource("classpath:application-playground-custom-static-path.properties")
14+
public class PlaygroundCustomStaticPathTest extends PlaygroundResourcesTestBase {
15+
16+
private static final String CUSTOM_CSS_URL = "/custom-static-path/static/css/index.css";
17+
private static final String CUSTOM_SCRIPT_URL = "/custom-static-path/static/js/middleware.js";
18+
private static final String CUSTOM_FAVICON_URL = "/custom-static-path/favicon.png";
19+
private static final String CUSTOM_LOGO_URL = "/custom-static-path/logo.png";
20+
21+
@Test
22+
public void shouldLoadStaticResourcesFromCustomPath() throws Exception {
23+
testPlaygroundResources(CUSTOM_CSS_URL, CUSTOM_SCRIPT_URL, CUSTOM_FAVICON_URL, CUSTOM_LOGO_URL);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.oembedler.moon.playground.boot;
2+
3+
import org.jsoup.Jsoup;
4+
import org.jsoup.nodes.Document;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.TestPropertySource;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.test.web.servlet.MockMvc;
13+
import org.springframework.test.web.servlet.MvcResult;
14+
15+
import static com.oembedler.moon.playground.boot.PlaygroundTestHelper.assertTitle;
16+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
18+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
19+
20+
@RunWith(SpringRunner.class)
21+
@SpringBootTest(classes = PlaygroundTestConfig.class)
22+
@AutoConfigureMockMvc
23+
@TestPropertySource("classpath:application-playground-custom-title.properties")
24+
public class PlaygroundCustomTitleTest {
25+
26+
private static final String CUSTOM_TITLE = "My CustomTest Title";
27+
28+
@Autowired
29+
private MockMvc mockMvc;
30+
31+
@Test
32+
public void shouldUseTheCustomPageTitle() throws Exception {
33+
final MvcResult mvcResult = mockMvc.perform(get(PlaygroundTestHelper.DEFAULT_PLAYGROUND_ENDPOINT))
34+
.andExpect(status().isOk())
35+
.andExpect(model().attribute(PlaygroundTestHelper.PAGE_TITLE_FIELD_NAME, CUSTOM_TITLE))
36+
.andReturn();
37+
38+
final Document document = Jsoup.parse(mvcResult.getResponse().getContentAsString());
39+
assertTitle(document, CUSTOM_TITLE);
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
package com.oembedler.moon.playground.boot;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import org.junit.Test;
54
import org.junit.runner.RunWith;
65
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
76
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
88
import org.springframework.boot.test.context.SpringBootTest;
99
import org.springframework.context.ApplicationContext;
1010
import org.springframework.test.context.junit4.SpringRunner;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
13+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
14+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1115

1216
@RunWith(SpringRunner.class)
13-
@SpringBootTest(classes = {PlaygroundAutoConfiguration.class, ObjectMapper.class},
14-
properties = "graphql.playground.enabled=false")
17+
@SpringBootTest(classes = PlaygroundTestConfig.class, properties = "graphql.playground.enabled=false")
18+
@AutoConfigureMockMvc
1519
public class PlaygroundDisabledTest {
1620

1721
@Autowired
1822
private ApplicationContext applicationContext;
1923

24+
@Autowired
25+
private MockMvc mockMvc;
26+
2027
@Test(expected = NoSuchBeanDefinitionException.class)
2128
public void playgroundShouldNotLoadIfDisabled() {
2229
applicationContext.getBean(PlaygroundController.class);
2330
}
31+
32+
@Test
33+
public void playgroundEndpointShouldNotExist() throws Exception {
34+
mockMvc.perform(get(PlaygroundTestHelper.DEFAULT_PLAYGROUND_ENDPOINT)).andExpect(status().isNotFound());
35+
}
2436
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,101 @@
11
package com.oembedler.moon.playground.boot;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.jsoup.Jsoup;
5+
import org.jsoup.nodes.Document;
46
import org.junit.Test;
57
import org.junit.runner.RunWith;
68
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
710
import org.springframework.boot.test.context.SpringBootTest;
811
import org.springframework.context.ApplicationContext;
12+
import org.springframework.http.MediaType;
913
import org.springframework.test.context.junit4.SpringRunner;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
import org.springframework.test.web.servlet.MvcResult;
1016

17+
import static com.oembedler.moon.playground.boot.PlaygroundTestHelper.assertStaticResources;
18+
import static com.oembedler.moon.playground.boot.PlaygroundTestHelper.assertTitle;
1119
import static org.assertj.core.api.Java6Assertions.assertThat;
20+
import static org.hamcrest.Matchers.isEmptyString;
21+
import static org.hamcrest.Matchers.not;
22+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
23+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
24+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
25+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1226

1327
@RunWith(SpringRunner.class)
14-
@SpringBootTest(classes = {PlaygroundAutoConfiguration.class, ObjectMapper.class},
15-
properties = "graphql.playground.enabled=true")
28+
@SpringBootTest(classes = PlaygroundTestConfig.class)
29+
@AutoConfigureMockMvc
1630
public class PlaygroundEnabledTest {
1731

32+
private static final String DEFAULT_CSS_PATH = "/vendor/playground/static/css/index.css";
33+
private static final String DEFAULT_SCRIPT_PATH = "/vendor/playground/static/js/middleware.js";
34+
private static final String DEFAULT_FAVICON_PATH = "/vendor/playground/favicon.png";
35+
private static final String DEFAULT_LOGO_PATH = "/vendor/playground/logo.png";
36+
private static final String DEFAULT_TITLE = "Playground";
37+
1838
@Autowired
1939
private ApplicationContext applicationContext;
2040

41+
@Autowired
42+
private MockMvc mockMvc;
43+
44+
@Autowired
45+
private ObjectMapper objectMapper;
46+
2147
@Test
2248
public void playgroundLoads() {
2349
assertThat(applicationContext.getBean(PlaygroundController.class)).isNotNull();
2450
}
51+
52+
@Test
53+
public void playgroundShouldBeAvailableAtDefaultEndpoint() throws Exception {
54+
final MvcResult mvcResult = mockMvc.perform(get(PlaygroundTestHelper.DEFAULT_PLAYGROUND_ENDPOINT))
55+
.andExpect(status().isOk())
56+
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
57+
.andExpect(content().string(not(isEmptyString())))
58+
.andExpect(model().attribute(PlaygroundTestHelper.PAGE_TITLE_FIELD_NAME, DEFAULT_TITLE))
59+
.andExpect(model().attribute(PlaygroundTestHelper.CSS_URL_FIELD_NAME, DEFAULT_CSS_PATH))
60+
.andExpect(model().attribute(PlaygroundTestHelper.SCRIPT_URL_FIELD_NAME, DEFAULT_SCRIPT_PATH))
61+
.andExpect(model().attribute(PlaygroundTestHelper.FAVICON_URL_FIELD_NAME, DEFAULT_FAVICON_PATH))
62+
.andExpect(model().attribute(PlaygroundTestHelper.LOGO_URL_FIELD_NAME, DEFAULT_LOGO_PATH))
63+
.andReturn();
64+
65+
final Document document = Jsoup.parse(mvcResult.getResponse().getContentAsString());
66+
assertTitle(document, DEFAULT_TITLE);
67+
assertStaticResources(document, DEFAULT_CSS_PATH, DEFAULT_SCRIPT_PATH, DEFAULT_FAVICON_PATH, DEFAULT_LOGO_PATH);
68+
}
69+
70+
@Test
71+
public void defaultCssShouldBeAvailable() throws Exception {
72+
mockMvc.perform(get(DEFAULT_CSS_PATH))
73+
.andExpect(status().isOk())
74+
.andExpect(content().string(not(isEmptyString())))
75+
.andExpect(content().contentTypeCompatibleWith("text/css"));
76+
}
77+
78+
@Test
79+
public void defaultScriptShouldBeAvailable() throws Exception {
80+
mockMvc.perform(get(DEFAULT_SCRIPT_PATH))
81+
.andExpect(status().isOk())
82+
.andExpect(content().string(not(isEmptyString())))
83+
.andExpect(content().contentTypeCompatibleWith("application/javascript"));
84+
}
85+
86+
@Test
87+
public void defaultFaviconShouldBeAvailable() throws Exception {
88+
mockMvc.perform(get(DEFAULT_FAVICON_PATH))
89+
.andExpect(status().isOk())
90+
.andExpect(content().string(not(isEmptyString())))
91+
.andExpect(content().contentTypeCompatibleWith("image/png"));
92+
}
93+
94+
@Test
95+
public void defaultLogoShouldBeAvailable() throws Exception {
96+
mockMvc.perform(get(DEFAULT_LOGO_PATH))
97+
.andExpect(status().isOk())
98+
.andExpect(content().string(not(isEmptyString())))
99+
.andExpect(content().contentTypeCompatibleWith("image/png"));
100+
}
25101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.oembedler.moon.playground.boot;
2+
3+
import org.jsoup.Jsoup;
4+
import org.jsoup.nodes.Document;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.test.web.servlet.MockMvc;
7+
import org.springframework.test.web.servlet.MvcResult;
8+
9+
import static com.oembedler.moon.playground.boot.PlaygroundTestHelper.assertStaticResources;
10+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
13+
14+
class PlaygroundResourcesTestBase {
15+
16+
@Autowired
17+
private MockMvc mockMvc;
18+
19+
void testPlaygroundResources(final String cssUrl, final String scriptUrl, final String faviconUrl,
20+
final String logoUrl) throws Exception {
21+
final MvcResult mvcResult = mockMvc.perform(get(PlaygroundTestHelper.DEFAULT_PLAYGROUND_ENDPOINT))
22+
.andExpect(status().isOk())
23+
.andExpect(model().attribute(PlaygroundTestHelper.CSS_URL_FIELD_NAME, cssUrl))
24+
.andExpect(model().attribute(PlaygroundTestHelper.SCRIPT_URL_FIELD_NAME, scriptUrl))
25+
.andExpect(model().attribute(PlaygroundTestHelper.FAVICON_URL_FIELD_NAME, faviconUrl))
26+
.andExpect(model().attribute(PlaygroundTestHelper.LOGO_URL_FIELD_NAME, logoUrl))
27+
.andReturn();
28+
29+
final Document document = Jsoup.parse(mvcResult.getResponse().getContentAsString());
30+
assertStaticResources(document, cssUrl, scriptUrl, faviconUrl, logoUrl);
31+
}
32+
}

0 commit comments

Comments
 (0)