-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormEmailerRouteBuilderTest.java
167 lines (140 loc) · 5.27 KB
/
FormEmailerRouteBuilderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package xyz.tomd;
import io.agroal.api.AgroalDataSource;
import io.quarkus.test.junit.QuarkusTest;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.junit.jupiter.api.Test;
import org.jvnet.mock_javamail.Mailbox;
import javax.inject.Inject;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
@QuarkusTest
//@QuarkusTestResource(SmtpServerResource.class) // See: https://www.morling.dev/blog/quarkus-and-testcontainers/
class FormEmailerRouteBuilderTest {
@Inject
CamelContext camelContext;
@Inject
ProducerTemplate template;
@ConfigProperty(name = "redirect.fail")
String redirectFailUrl;
@ConfigProperty(name = "redirect.success")
String redirectSuccessUrl;
@ConfigProperty(name = "antispam.answer")
String antispamAnswer;
@ConfigProperty(name = "mail.to")
String mailToAddress;
@Inject
AgroalDataSource dataSource;
/**
* Make sure that the healthcheck exists at the root endpoint (should return "NOOP")
* @throws Exception
*/
@Test
public void testHealthcheck() throws Exception {
given()
.when().get("/")
.then()
.statusCode(200)
.body(is("NOOP"));
}
/**
* A happy-path test. Use a mock Javamail, and assert that an email arrives in the inbox.
* @throws Exception
*/
@Test
public void testSendsEmail() throws Exception {
Mailbox.clearAll();
// Possibly replace this with a Testcontainer in future
Connection conn;
Statement statement;
conn = dataSource.getConnection();
statement = conn.createStatement();
int deleteResult = statement.executeUpdate("drop table if exists responses");
int dbResult = statement.executeUpdate("create table responses (\n" +
" id integer primary key,\n" +
" sender_name text,\n" +
" sender_email text,\n" +
" message text,\n" +
" received text\n" +
");");
conn.close(); // Tidy up before the Camel stuff runs
given()
.when()
.redirects().follow(false)
.formParam("name", "Dave Angel")
.formParam("email", "dav@example.com")
.formParam("message", "Great website!")
.formParam("antispam", antispamAnswer)
.post("/")
.then()
.statusCode(303)
.header("Location", is(redirectSuccessUrl));
// Check that the email arrived
Mailbox box = Mailbox.get(mailToAddress);
assertEquals(1, box.size());
// Create a new connection
conn = dataSource.getConnection();
statement = conn.createStatement();
// Check that the data was persisted to the sqlite DB
ResultSet results = statement.executeQuery("select * from responses");
String senderName = results.getString("sender_name"); // Get the value from the first row
assertEquals("Dave Angel", senderName);
}
/**
* If the user provides an incorrect antispam answer, then redirect to the failure page
* @throws Exception
*/
@Test
public void testIncorrectAntispamAnswerShouldRedirectToFailureUrl() throws Exception {
given()
.when()
.redirects().follow(false) // Don't follow any redirects to my website, etc...
.formParam("name", "Dave Smith")
.formParam("antispam", "XXX")
.post("/")
.then()
.statusCode(303)
.header("Location", is(redirectFailUrl));
}
/**
* If the user omits a required field, don't crash, redirect to failure page
* @throws Exception
*/
@Test
public void testMissingRequiredFieldShouldRedirectToFailureUrl() throws Exception {
given()
.when()
.redirects().follow(false) // Don't follow any redirects to my website, etc...
.formParam("name", "Dave Smith")
// Missing the 'email' field
.formParam("antispam", antispamAnswer)
.post("/")
.then()
.statusCode(303)
.header("Location", is(redirectFailUrl));
}
/**
* If the user supplies an antispam answer with padding or in the wrong case,
* allow it
* @throws Exception
*/
@Test
public void testWrongCaseWithSpacesAntispamIsPermitted() throws Exception {
given()
.when()
.redirects().follow(false)
.formParam("name", "Dave Smith")
.formParam("email", "user@example.com")
.formParam("message", "Great website!")
.formParam("antispam", " CaMeLs ")
.post("/")
.then()
.statusCode(303) // Redirect 303 See Other
.header("Location", is(redirectSuccessUrl)); // check for redirect to success URL
}
}