Add extra informational message when a failure occurs while sending an email from WebGoat to WebWolf.
This commit is contained in:
@ -47,6 +47,7 @@ public class ReportCardServiceTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
this.mockMvc = standaloneSetup(new ReportCardService(websession, userTrackerRepository, course, pluginMessages)).build();
|
this.mockMvc = standaloneSetup(new ReportCardService(websession, userTrackerRepository, course, pluginMessages)).build();
|
||||||
|
when(pluginMessages.getMessage(anyString())).thenReturn("Test");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
@ -39,7 +40,11 @@ public class MailAssignment extends AssignmentEndpoint {
|
|||||||
.contents("This is a test message from WebWolf, your unique code is: " + StringUtils.reverse(username))
|
.contents("This is a test message from WebWolf, your unique code is: " + StringUtils.reverse(username))
|
||||||
.sender("webgoat@owasp.org")
|
.sender("webgoat@owasp.org")
|
||||||
.build();
|
.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();
|
return informationMessage().feedback("webwolf.email_send").feedbackArgs(email).build();
|
||||||
} else {
|
} else {
|
||||||
return informationMessage().feedback("webwolf.email_mismatch").feedbackArgs(username).build();
|
return informationMessage().feedback("webwolf.email_mismatch").feedbackArgs(username).build();
|
||||||
|
@ -2,7 +2,7 @@ webwolf.title=WebWolf
|
|||||||
|
|
||||||
webwolf.email_send=An email has been send to {0} please check your inbox.
|
webwolf.email_send=An email has been send to {0} please check your inbox.
|
||||||
webwolf.code_incorrect=That is not the correct code: {0}, please try again.
|
webwolf.code_incorrect=That is not the correct code: {0}, please try again.
|
||||||
|
webwolf.email_failed=There was an error while sending the e-mail. Is WebWolf running?
|
||||||
|
|
||||||
webwolf.email_mismatch=Of course you can send mail to user {0} however you will not be able to read this e-mail in WebWolf, please use your own username.
|
webwolf.email_mismatch=Of course you can send mail to user {0} however you will not be able to read this e-mail in WebWolf, please use your own username.
|
||||||
|
|
||||||
|
@ -85,6 +85,10 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security</groupId>
|
||||||
|
<artifactId>spring-security-test</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package org.owasp.webwolf.mailbox;
|
package org.owasp.webwolf.mailbox;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@ -13,6 +16,8 @@ import java.time.format.DateTimeFormatter;
|
|||||||
* @since 8/20/17.
|
* @since 8/20/17.
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
@Entity
|
@Entity
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class Email implements Serializable {
|
public class Email implements Serializable {
|
||||||
@ -20,7 +25,7 @@ public class Email implements Serializable {
|
|||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
private LocalDateTime time;
|
private LocalDateTime time = LocalDateTime.now();
|
||||||
@Column(length = 1024)
|
@Column(length = 1024)
|
||||||
private String contents;
|
private String contents;
|
||||||
private String sender;
|
private String sender;
|
||||||
@ -28,7 +33,7 @@ public class Email implements Serializable {
|
|||||||
private String recipient;
|
private String recipient;
|
||||||
|
|
||||||
public String getSummary() {
|
public String getSummary() {
|
||||||
return "-" + this.contents.substring(0, 50);
|
return "-" + this.contents.substring(0, Math.min(50, contents.length()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocalDateTime getTimestamp() {
|
public LocalDateTime getTimestamp() {
|
||||||
|
@ -7,6 +7,7 @@ import org.owasp.webwolf.user.WebGoatUser;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.User;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
@ -25,12 +26,11 @@ import java.util.concurrent.Callable;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class MailboxController {
|
public class MailboxController {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
|
||||||
private final MailboxRepository mailboxRepository;
|
private final MailboxRepository mailboxRepository;
|
||||||
|
|
||||||
@GetMapping(value = "/WebWolf/mail")
|
@GetMapping(value = "/WebWolf/mail")
|
||||||
public ModelAndView mail() {
|
public ModelAndView mail() {
|
||||||
WebGoatUser user = (WebGoatUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||||
ModelAndView modelAndView = new ModelAndView();
|
ModelAndView modelAndView = new ModelAndView();
|
||||||
List<Email> emails = mailboxRepository.findByRecipientOrderByTimeDesc(user.getUsername());
|
List<Email> emails = mailboxRepository.findByRecipientOrderByTimeDesc(user.getUsername());
|
||||||
if (emails != null && !emails.isEmpty()) {
|
if (emails != null && !emails.isEmpty()) {
|
||||||
@ -44,13 +44,8 @@ public class MailboxController {
|
|||||||
@PostMapping(value = "/mail")
|
@PostMapping(value = "/mail")
|
||||||
public Callable<ResponseEntity<?>> sendEmail(@RequestBody Email email) {
|
public Callable<ResponseEntity<?>> sendEmail(@RequestBody Email email) {
|
||||||
return () -> {
|
return () -> {
|
||||||
if (userRepository.findByUsername(email.getRecipient()) != null) {
|
mailboxRepository.save(email);
|
||||||
mailboxRepository.save(email);
|
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
|
||||||
} else {
|
|
||||||
log.trace("Mail received for unknown user: {}", email.getRecipient());
|
|
||||||
return ResponseEntity.notFound().build();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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("test1234@webgoat.org")
|
||||||
|
.sender("hacker@webgoat.org")
|
||||||
|
.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("test1234@webgoat.org")
|
||||||
|
.sender("hacker@webgoat.org")
|
||||||
|
.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("test1234@webgoat.org")
|
||||||
|
.sender("hacker@webgoat.org")
|
||||||
|
.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"))));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user