93 lines
3.4 KiB
Java
93 lines
3.4 KiB
Java
package de.nclazz.service.mailrelay;
|
|
|
|
import de.nclazz.service.mailrelay.domain.Relay;
|
|
import de.nclazz.service.mailrelay.domain.RelayAccount;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.boot.test.context.TestConfiguration;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.test.context.ActiveProfiles;
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import java.time.Clock;
|
|
import java.time.LocalDateTime;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
|
@SpringBootTest
|
|
@ActiveProfiles("integration")
|
|
@AutoConfigureMockMvc
|
|
public class ForwardMessageFormTest {
|
|
|
|
@Autowired
|
|
private MockMvc mockMvc;
|
|
|
|
private static final LocalDateTime FIXED_TIME = LocalDateTime.of(2019, 4, 25, 13, 12);
|
|
private static final String TOKEN = "my-token";
|
|
|
|
@TestConfiguration
|
|
static class TestConfig {
|
|
|
|
@Autowired
|
|
private Relay relay;
|
|
|
|
@Bean
|
|
public Clock clock() {
|
|
return FakeClock.fixed(FIXED_TIME);
|
|
}
|
|
|
|
@PostConstruct
|
|
public void init() {
|
|
RelayAccount account = new RelayAccount(UUID.randomUUID(), "company-account", TOKEN, List.of("vip@company.com"));
|
|
relay.saveAccount(account);
|
|
}
|
|
}
|
|
|
|
@Test
|
|
void forwardMessageRedirectsToOnSuccessUrl() throws Exception {
|
|
mockMvc.perform(post("/form").contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
.param("token", TOKEN)
|
|
.param("subject", "subject")
|
|
.param("message", "message")
|
|
.param("from", "sender@company.com")
|
|
.param("onSuccess", "http://mysite.com/next")
|
|
)
|
|
.andExpect(status().is3xxRedirection())
|
|
.andExpect(redirectedUrl("http://mysite.com/next"));
|
|
}
|
|
|
|
@Test
|
|
void forwardMessageRedirectsToRefererIfOnSuccessIsNotSet() throws Exception {
|
|
mockMvc.perform(post("/form").contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
.param("token", TOKEN)
|
|
.param("subject", "subject")
|
|
.param("message", "message")
|
|
.param("from", "sender@company.com")
|
|
.header("Referer", "http://myorigin.com")
|
|
)
|
|
.andExpect(status().is3xxRedirection())
|
|
.andExpect(redirectedUrl("http://myorigin.com"));
|
|
}
|
|
|
|
@Test
|
|
void forwardMessageRedirectsToOnErrorWhenAnErrorOccurs() throws Exception {
|
|
mockMvc.perform(post("/form").contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
.param("subject", "subject")
|
|
.param("message", "message")
|
|
.param("from", "sender@company.com")
|
|
.param("onError", "http://myerrorsite.com")
|
|
)
|
|
.andExpect(status().is3xxRedirection())
|
|
.andExpect(redirectedUrl("http://myerrorsite.com"));
|
|
}
|
|
|
|
}
|