■ ■ ■ ■ ■ ■
src/test/java/org/owasp/wrongsecrets/ChallengesControllerCTFModeWithPresetCloudValuesTest.java
| 1 | + | package org.owasp.wrongsecrets; |
| 2 | + | |
| 3 | + | import org.junit.jupiter.api.Test; |
| 4 | + | import org.junit.jupiter.api.extension.ExtendWith; |
| 5 | + | import org.owasp.wrongsecrets.challenges.cloud.Challenge10; |
| 6 | + | import org.owasp.wrongsecrets.challenges.cloud.Challenge11; |
| 7 | + | import org.owasp.wrongsecrets.challenges.cloud.Challenge9; |
| 8 | + | import org.springframework.beans.factory.annotation.Autowired; |
| 9 | + | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 10 | + | import org.springframework.boot.test.context.SpringBootTest; |
| 11 | + | import org.springframework.http.MediaType; |
| 12 | + | import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 13 | + | import org.springframework.test.web.servlet.MockMvc; |
| 14 | + | |
| 15 | + | import static org.hamcrest.Matchers.containsString; |
| 16 | + | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
| 17 | + | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 18 | + | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 19 | + | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 20 | + | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 21 | + | |
| 22 | + | @ExtendWith(SpringExtension.class) |
| 23 | + | @SpringBootTest( |
| 24 | + | properties = {"ctf_enabled=true", "ctf_key=randomtextforkey", |
| 25 | + | "SPECIAL_K8S_SECRET=test5", "SPECIAL_SPECIAL_K8S_SECRET=test6", "vaultpassword=test7", |
| 26 | + | "secretmountpath=nothere", "default_aws_value_challenge_9=ACTUAL_ANSWER_CHALLENGE9", |
| 27 | + | "default_aws_value_challenge_10=ACTUAL_ANSWER_CHALLENGE10", "default_aws_value_challenge_11=ACTUAL_ANSWER_CHALLENGE_11"}, |
| 28 | + | classes = WrongSecretsApplication.class |
| 29 | + | ) |
| 30 | + | @AutoConfigureMockMvc |
| 31 | + | class ChallengesControllerCTFModeWithPresetCloudValuesTest { |
| 32 | + | |
| 33 | + | @Autowired |
| 34 | + | private MockMvc mvc; |
| 35 | + | |
| 36 | + | |
| 37 | + | @Test |
| 38 | + | void shouldNotSpoilWhenInCTFMode() throws Exception { |
| 39 | + | mvc.perform(get("/spoil-9")) |
| 40 | + | .andExpect(status().isOk()) |
| 41 | + | .andExpect(content().string(containsString("Spoils are disabled in CTF mode"))); |
| 42 | + | |
| 43 | + | } |
| 44 | + | |
| 45 | + | @Test |
| 46 | + | void shouldShowFlagWhenRespondingWithSuccessInCTFModeChallenge9() throws Exception { |
| 47 | + | var spoil = new Challenge9(new InMemoryScoreCard(1), null, "ACTUAL_ANSWER_CHALLENGE9", new RuntimeEnvironment(RuntimeEnvironment.Environment.HEROKU_DOCKER)).spoiler().solution(); |
| 48 | + | mvc.perform(post("/challenge/9") |
| 49 | + | .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 50 | + | .param("solution", spoil) |
| 51 | + | .param("action", "submit") |
| 52 | + | .with(csrf())) |
| 53 | + | .andExpect(status().isOk()) |
| 54 | + | .andExpect(content().string(containsString("70d75bf845890b2419bd8795c"))); |
| 55 | + | } |
| 56 | + | |
| 57 | + | @Test |
| 58 | + | void shouldShowFlagWhenRespondingWithSuccessInCTFModeChallenge10() throws Exception { |
| 59 | + | var spoil = new Challenge10(new InMemoryScoreCard(1), null, "ACTUAL_ANSWER_CHALLENGE10", new RuntimeEnvironment(RuntimeEnvironment.Environment.HEROKU_DOCKER)).spoiler().solution(); |
| 60 | + | mvc.perform(post("/challenge/10") |
| 61 | + | .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 62 | + | .param("solution", spoil) |
| 63 | + | .param("action", "submit") |
| 64 | + | .with(csrf())) |
| 65 | + | .andExpect(status().isOk()) |
| 66 | + | .andExpect(content().string(containsString("176e937a2cafea3b0da3"))); |
| 67 | + | } |
| 68 | + | |
| 69 | + | @Test |
| 70 | + | void shouldShowFlagWhenRespondingWithSuccessInCTFModeChallenge11() throws Exception { |
| 71 | + | var spoil = new Challenge11(new InMemoryScoreCard(1), |
| 72 | + | "awsRoleArn", "tokenFileLocation", |
| 73 | + | "awsRegion", "gcpDefualtValue", "awsDefaultValue", |
| 74 | + | "azureDefaultValue", "azureVaultUri", "azureWrongSecret3", |
| 75 | + | "projectId", "ACTUAL_ANSWER_CHALLENGE_11", true, |
| 76 | + | new RuntimeEnvironment(RuntimeEnvironment.Environment.HEROKU_DOCKER)).spoiler().solution(); |
| 77 | + | |
| 78 | + | mvc.perform(post("/challenge/11") |
| 79 | + | .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 80 | + | .param("solution", spoil) |
| 81 | + | .param("action", "submit") |
| 82 | + | .with(csrf())) |
| 83 | + | .andExpect(status().isOk()) |
| 84 | + | .andExpect(content().string(containsString("89aeb4b29d4a0bc13bd"))); |
| 85 | + | } |
| 86 | + | |
| 87 | + | @Test |
| 88 | + | void shouldEnableCloudExercises() throws Exception { |
| 89 | + | mvc.perform(get("/")) |
| 90 | + | .andExpect(status().isOk()) |
| 91 | + | .andExpect(content().string(containsString("<td> <a href=\"/challenge/9\">Challenge 9</a></td>"))) |
| 92 | + | .andExpect(content().string(containsString("<td> <a href=\"/challenge/10\">Challenge 10</a></td>"))) |
| 93 | + | .andExpect(content().string(containsString("<td> <a href=\"/challenge/11\">Challenge 11</a></td>"))); |
| 94 | + | } |
| 95 | + | |
| 96 | + | @Test |
| 97 | + | void shouldEnableK8sExercises() throws Exception{ |
| 98 | + | mvc.perform(get("/")) |
| 99 | + | .andExpect(status().isOk()) |
| 100 | + | .andExpect(content().string(containsString("<td> <a href=\"/challenge/5\">Challenge 5</a></td>"))) |
| 101 | + | .andExpect(content().string(containsString("<td> <a href=\"/challenge/6\">Challenge 6</a></td>"))) |
| 102 | + | .andExpect(content().string(containsString("<td> <a href=\"/challenge/7\">Challenge 7</a></td>"))); |
| 103 | + | } |
| 104 | + | |
| 105 | + | } |
| 106 | + | |