forked from WebGoat/WebGoat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extra informational message when a failure occurs while sending a…
…n email from WebGoat to WebWolf.
- Loading branch information
Showing
7 changed files
with
121 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.time.LocalDateTime; | ||
|
@@ -39,7 +40,11 @@ public AttackResult sendEmail(@RequestParam String email) { | |
.contents("This is a test message from WebWolf, your unique code is: " + StringUtils.reverse(username)) | ||
.sender("[email protected]") | ||
.build(); | ||
restTemplate.postForEntity(webWolfURL, mailEvent, Object.class); | ||
try { | ||
restTemplate.postForEntity(webWolfURL, mailEvent, Object.class); | ||
} catch (RestClientException e ) { | ||
return informationMessage().feedback("webwolf.email_failed").output(e.getMessage()).build(); | ||
} | ||
return informationMessage().feedback("webwolf.email_send").feedbackArgs(email).build(); | ||
} else { | ||
return informationMessage().feedback("webwolf.email_mismatch").feedbackArgs(username).build(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
webwolf/src/test/java/org/owasp/webwolf/mailbox/MailboxControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.owasp.webwolf.mailbox; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.collect.Lists; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.CoreMatchers.not; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@RunWith(SpringRunner.class) | ||
@WebMvcTest(MailboxController.class) | ||
public class MailboxControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mvc; | ||
@MockBean | ||
private MailboxRepository mailbox; | ||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@JsonIgnoreProperties("time") | ||
public static class EmailMixIn { | ||
} | ||
|
||
@Before | ||
public void setup() { | ||
objectMapper.addMixIn(Email.class, EmailMixIn.class); | ||
} | ||
|
||
@Test | ||
@WithMockUser | ||
public void sendingMailShouldStoreIt() throws Exception { | ||
Email email = Email.builder() | ||
.contents("This is a test mail") | ||
.recipient("[email protected]") | ||
.sender("[email protected]") | ||
.title("Click this mail") | ||
.time(LocalDateTime.now()) | ||
.build(); | ||
this.mvc.perform(post("/mail").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsBytes(email))) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "test1234") | ||
public void userShouldBeAbleToReadOwnEmail() throws Exception { | ||
Email email = Email.builder() | ||
.contents("This is a test mail") | ||
.recipient("[email protected]") | ||
.sender("[email protected]") | ||
.title("Click this mail") | ||
.time(LocalDateTime.now()) | ||
.build(); | ||
Mockito.when(mailbox.findByRecipientOrderByTimeDesc("test1234")).thenReturn(Lists.newArrayList(email)); | ||
|
||
this.mvc.perform(get("/WebWolf/mail")) | ||
.andExpect(status().isOk()) | ||
.andExpect(view().name("mailbox")) | ||
.andExpect(content().string(containsString("Click this mail"))) | ||
.andExpect(content().string(containsString(DateTimeFormatter.ofPattern("h:mm a").format(email.getTimestamp())))); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "test1233") | ||
public void differentUserShouldNotBeAbleToReadOwnEmail() throws Exception { | ||
Email email = Email.builder() | ||
.contents("This is a test mail") | ||
.recipient("[email protected]") | ||
.sender("[email protected]") | ||
.title("Click this mail") | ||
.time(LocalDateTime.now()) | ||
.build(); | ||
Mockito.when(mailbox.findByRecipientOrderByTimeDesc("test1234")).thenReturn(Lists.newArrayList(email)); | ||
|
||
this.mvc.perform(get("/WebWolf/mail")) | ||
.andExpect(status().isOk()) | ||
.andExpect(view().name("mailbox")) | ||
.andExpect(content().string(not(containsString("Click this mail")))); | ||
} | ||
|
||
} |