■ ■ ■ ■ ■ ■
src/test/java/org/owasp/wrongsecrets/ctftests/ChallengesControllerCTFClientModeTest.java
| 1 | + | package org.owasp.wrongsecrets.ctftests; |
| 2 | + | |
| 3 | + | import org.junit.jupiter.api.Test; |
| 4 | + | import org.junit.jupiter.api.extension.ExtendWith; |
| 5 | + | import org.owasp.wrongsecrets.InMemoryScoreCard; |
| 6 | + | import org.owasp.wrongsecrets.WrongSecretsApplication; |
| 7 | + | import org.owasp.wrongsecrets.challenges.docker.Challenge1; |
| 8 | + | import org.owasp.wrongsecrets.challenges.docker.Challenge8; |
| 9 | + | import org.springframework.beans.factory.annotation.Autowired; |
| 10 | + | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 11 | + | import org.springframework.boot.test.context.SpringBootTest; |
| 12 | + | import org.springframework.http.MediaType; |
| 13 | + | import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 14 | + | import org.springframework.test.web.servlet.MockMvc; |
| 15 | + | |
| 16 | + | import static org.hamcrest.Matchers.containsString; |
| 17 | + | import static org.hamcrest.Matchers.not; |
| 18 | + | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
| 19 | + | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 20 | + | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 21 | + | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 22 | + | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 23 | + | |
| 24 | + | @ExtendWith(SpringExtension.class) |
| 25 | + | @SpringBootTest( |
| 26 | + | properties = {"ctf_enabled=true", "ctf_key=randomtextforkey", "CTF_SERVER_ADDRESS=https://www.google.nl", "challenge_acht_ctf_to_provide_to_host_value=workit"}, |
| 27 | + | classes = WrongSecretsApplication.class |
| 28 | + | ) |
| 29 | + | @AutoConfigureMockMvc |
| 30 | + | class ChallengesControllerCTFClientModeTest { |
| 31 | + | |
| 32 | + | @Autowired |
| 33 | + | private MockMvc mvc; |
| 34 | + | |
| 35 | + | |
| 36 | + | @Test |
| 37 | + | void shouldNotSpoilWhenInCTFMode() throws Exception { |
| 38 | + | mvc.perform(get("/spoil-1")) |
| 39 | + | .andExpect(status().isOk()) |
| 40 | + | .andExpect(content().string(containsString("Spoils are disabled in CTF mode"))); |
| 41 | + | |
| 42 | + | } |
| 43 | + | |
| 44 | + | @Test |
| 45 | + | void shouldNotSpoilWhenInCTFModeEvenWhenChallengeUnsupported() throws Exception { |
| 46 | + | mvc.perform(get("/spoil-5")) |
| 47 | + | .andExpect(status().isOk()) |
| 48 | + | .andExpect(content().string(containsString("Spoils are disabled in CTF mode"))); |
| 49 | + | |
| 50 | + | } |
| 51 | + | |
| 52 | + | @Test |
| 53 | + | void shouldNotShowFlagButClientInstead() throws Exception { |
| 54 | + | var spoil = new Challenge1(new InMemoryScoreCard(1)).spoiler().solution(); |
| 55 | + | mvc.perform(post("/challenge/1") |
| 56 | + | .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 57 | + | .param("solution", spoil) |
| 58 | + | .param("action", "submit") |
| 59 | + | .with(csrf())) |
| 60 | + | .andExpect(status().isOk()) |
| 61 | + | .andExpect(content().string(not(containsString("ba9a72ac7057576344856")))) |
| 62 | + | .andExpect(content().string(containsString("https://www.google.nl"))); |
| 63 | + | } |
| 64 | + | |
| 65 | + | @Test |
| 66 | + | void shouldEnableK8sExercises() throws Exception { |
| 67 | + | mvc.perform(get("/")) |
| 68 | + | .andExpect(status().isOk()) |
| 69 | + | .andExpect(content().string(containsString("class=\"disabled\">Challenge 5</a></td>"))) |
| 70 | + | .andExpect(content().string(containsString("class=\"disabled\">Challenge 6</a></td>"))) |
| 71 | + | .andExpect(content().string(containsString("class=\"disabled\">Challenge 7</a></td>"))); |
| 72 | + | } |
| 73 | + | |
| 74 | + | @Test |
| 75 | + | void shouldStillDissableTestsIfNotPreconfigured() throws Exception { |
| 76 | + | testK8sChallenge("/challenge/5"); |
| 77 | + | testK8sChallenge("/challenge/6"); |
| 78 | + | testK8sChallenge("/challenge/7"); |
| 79 | + | testForCloudCluster("/challenge/9"); |
| 80 | + | testForCloudCluster("/challenge/10"); |
| 81 | + | testForCloudCluster("/challenge/11"); |
| 82 | + | } |
| 83 | + | |
| 84 | + | private void testK8sChallenge(String url) throws Exception { |
| 85 | + | mvc.perform(get(url) |
| 86 | + | .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 87 | + | .with(csrf())) |
| 88 | + | .andExpect(status().isOk()) |
| 89 | + | .andExpect(content().string(containsString("We are running outside a K8s cluster"))); |
| 90 | + | } |
| 91 | + | |
| 92 | + | private void testForCloudCluster(String url) throws Exception { |
| 93 | + | mvc.perform(get(url) |
| 94 | + | .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 95 | + | .with(csrf())) |
| 96 | + | .andExpect(status().isOk()) |
| 97 | + | .andExpect(content().string(containsString("We are running outside a properly configured Cloud environment."))); |
| 98 | + | } |
| 99 | + | } |
| 100 | + | |