1 package org.cacert.gigi.pages.wot;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
6 import java.io.IOException;
7 import java.net.MalformedURLException;
8 import java.net.URLConnection;
9 import java.sql.Timestamp;
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.database.GigiPreparedStatement;
13 import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
14 import org.cacert.gigi.dbObjects.Name;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.pages.account.MyDetails;
17 import org.cacert.gigi.testUtils.IOUtils;
18 import org.cacert.gigi.testUtils.ManagedTest;
19 import org.cacert.gigi.util.DayDate;
20 import org.cacert.gigi.util.Notary;
21 import org.junit.Test;
23 public class TestAssuranceMail extends ManagedTest {
25 private String agentM;
27 private String applicantM;
29 private String cookieAgent;
31 private String cookieApplicant;
35 private User applicant;
37 private int firstName;
39 private int secondName;
41 private int thirdName;
43 private String message;
45 private void newAgent() throws IOException {
46 agentM = createUniqueName() + "@example.com";
47 int agentID = createAssuranceUser("Marianne", "Mustermann", agentM, TEST_PASSWORD);
48 agent = User.getById(agentID);
49 cookieAgent = login(agentM, TEST_PASSWORD);
53 private void newApplicant() throws IOException {
54 applicantM = createUniqueName() + "@example.com";
55 int applicantId = createVerifiedUser("John", "Doe", applicantM, TEST_PASSWORD);
56 cookieApplicant = login(applicantM, TEST_PASSWORD);
57 executeBasicWebInteraction(cookieApplicant, MyDetails.PATH, "fname=James&lname=Doe&action=addName", 0);
58 executeBasicWebInteraction(cookieApplicant, MyDetails.PATH, "fname=James+John&lname=Doe&action=addName", 0);
59 applicant = User.getById(applicantId);
60 Name[] names = applicant.getNames();
64 for (int i = 0; i < names.length; i++) {
65 if (names[i].toString().equals("John Doe")) {
66 firstName = names[i].getId();
68 if (names[i].toString().equals("James Doe")) {
69 secondName = names[i].getId();
71 if (names[i].toString().equals("James John Doe")) {
72 thirdName = names[i].getId();
75 assertNotEquals(0, firstName);
76 assertNotEquals(0, secondName);
77 assertNotEquals(0, thirdName);
80 private void raiseXP(User agentXP, int recurring) throws GigiApiException {
81 for (int i = 0; i < recurring; i++) {
82 String applicantT = createUniqueName() + "@example.com";
83 int applicantId = createVerifiedUser("John", "Doe", applicantT, TEST_PASSWORD);
84 User applicantXP = User.getById(applicantId);
85 applicantXP = User.getById(applicantId);
86 Notary.assure(agentXP, applicantXP, applicantXP.getNames()[0], applicantXP.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
90 private String enterVerification(String query) throws MalformedURLException, IOException {
91 return enterVerification(query, 10);
95 private String enterVerification(String query, int points) throws MalformedURLException, IOException {
96 URLConnection uc = TestAssurance.buildupAssureFormConnection(cookieAgent, applicant.getEmail(), true);
97 uc.getOutputStream().write((query + "&date=" + validVerificationDateString() + "&location=" + createUniqueName() + "&certify=1&rules=1&assertion=1&points=" + points).getBytes("UTF-8"));
98 uc.getOutputStream().flush();
99 return IOUtils.readURL(uc);
103 private void enterVerificationInPast(int points, int nameId) {
105 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
106 ps.setInt(1, agent.getId());
107 ps.setInt(2, nameId);
108 ps.setInt(3, points);
109 ps.setString(4, "test-location");
110 ps.setString(5, "2010-01-01");
111 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
117 public void testVerificationFirstApplicant() throws MalformedURLException, IOException {
122 // verify preferred name only
123 enterVerification("assuredName=" + firstName);
124 message = getMailReceiver().receive().getMessage();
125 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
126 assertThat(message, containsString("John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
128 // verification first two names
131 enterVerification("assuredName=" + firstName + "&assuredName=" + secondName);
132 message = getMailReceiver().receive().getMessage();
133 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
134 assertThat(message, containsString("John Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
135 assertThat(message, containsString("James Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
137 // verification all three names
140 enterVerification("assuredName=" + firstName + "&assuredName=" + secondName + "&assuredName=" + thirdName);
141 message = getMailReceiver().receive().getMessage();
142 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
143 assertThat(message, containsString("John Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
144 assertThat(message, containsString("James Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
145 assertThat(message, containsString("James John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
147 // New verification preferred name
150 enterVerification("assuredName=" + firstName);
151 message = getMailReceiver().receive().getMessage();
152 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
153 assertThat(message, containsString("John Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
155 // verification all three names reaches 50 VP
158 enterVerification("assuredName=" + firstName + "&assuredName=" + secondName + "&assuredName=" + thirdName);
159 message = getMailReceiver().receive().getMessage();
160 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
161 assertThat(message, containsString("John Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
162 assertThat(message, containsString("James Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
163 assertThat(message, containsString("James John Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
164 assertThat(message, containsString(requiresMoreTotal(50)));
166 // verification all three names reaches 60 VP
169 enterVerification("assuredName=" + firstName + "&assuredName=" + secondName + "&assuredName=" + thirdName);
170 message = getMailReceiver().receive().getMessage();
171 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
172 assertThat(message, containsString("John Doe: with 10 to total 60 Verification Points."));
173 assertThat(message, containsString("James Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
174 assertThat(message, containsString("James John Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
175 assertThat(message, containsString(requiresMoreTotal(40)));
177 // verification all three names reaches 70 VP
180 enterVerification("assuredName=" + firstName + "&assuredName=" + secondName + "&assuredName=" + thirdName);
181 message = getMailReceiver().receive().getMessage();
182 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
183 assertThat(message, containsString("John Doe: with 10 to total 70 Verification Points."));
184 assertThat(message, containsString("James Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
185 assertThat(message, containsString("James John Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
186 assertThat(message, containsString(requiresMoreTotal(30)));
188 // verification all three names reaches 80 VP
191 enterVerification("assuredName=" + firstName + "&assuredName=" + secondName + "&assuredName=" + thirdName);
192 message = getMailReceiver().receive().getMessage();
193 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
194 assertThat(message, containsString("John Doe: with 10 to total 80 Verification Points."));
195 assertThat(message, containsString("James Doe: with 10 to total 60 Verification Points."));
196 assertThat(message, containsString("James John Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
197 assertThat(message, containsString(requiresMoreTotal(20)));
199 // verification all three names reaches 90 VP
202 enterVerification("assuredName=" + firstName + "&assuredName=" + secondName + "&assuredName=" + thirdName);
203 message = getMailReceiver().receive().getMessage();
204 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
205 assertThat(message, containsString("John Doe: with 10 to total 90 Verification Points."));
206 assertThat(message, containsString("James Doe: with 10 to total 70 Verification Points."));
207 assertThat(message, containsString("James John Doe: with 10 to total 60 Verification Points."));
208 assertThat(message, containsString(requiresMoreTotal(10)));
210 // verification all three names reaches 100 VP
214 enterVerification("assuredName=" + firstName + "&assuredName=" + secondName + "&assuredName=" + thirdName);
215 message = getMailReceiver().receive().getMessage();
216 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
217 assertThat(message, containsString("John Doe: with 10 to total 100 Verification Points."));
218 assertThat(message, containsString("James Doe: with 10 to total 80 Verification Points."));
219 assertThat(message, containsString("James John Doe: with 10 to total 70 Verification Points."));
220 assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));
222 // verification all three names reaches 100 VP
225 enterVerification("assuredName=" + firstName + "&assuredName=" + secondName + "&assuredName=" + thirdName);
226 message = getMailReceiver().receive().getMessage();
227 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
228 assertThat(message, containsString("John Doe: with 10 to total 110 Verification Points."));
229 assertThat(message, containsString("James Doe: with 10 to total 90 Verification Points."));
230 assertThat(message, containsString("James John Doe: with 10 to total 80 Verification Points."));
233 private String requiresMore(int points) {
234 return "To issue client certificates with this name you need " + points + " more Verification Points.";
237 private String requiresMoreTotal(int points) {
238 return "To apply for RA Agent status or code signing ability you need " + points + " more Verification Points.";
242 public void testVerificationSecondApplicant() throws MalformedURLException, IOException {
246 // verify preferred name only 5 times
248 enterVerification("assuredName=" + firstName);
249 message = getMailReceiver().receive().getMessage();
252 enterVerification("assuredName=" + firstName);
253 message = getMailReceiver().receive().getMessage();
256 enterVerification("assuredName=" + firstName);
257 message = getMailReceiver().receive().getMessage();
260 enterVerification("assuredName=" + firstName);
261 message = getMailReceiver().receive().getMessage();
264 enterVerification("assuredName=" + firstName);
266 message = getMailReceiver().receive().getMessage();
267 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
268 assertThat(message, containsString("John Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
269 assertThat(message, containsString(requiresMoreTotal(50)));
273 // verify preferred name second name
275 enterVerification("assuredName=" + secondName);
276 message = getMailReceiver().receive().getMessage();
277 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
278 assertThat(message, containsString("James Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
279 assertThat(message, containsString(requiresMoreTotal(40)));
281 // verify preferred name second name 4 more times
283 enterVerification("assuredName=" + secondName);
284 message = getMailReceiver().receive().getMessage();
287 enterVerification("assuredName=" + secondName);
288 message = getMailReceiver().receive().getMessage();
291 enterVerification("assuredName=" + secondName);
292 message = getMailReceiver().receive().getMessage();
295 enterVerification("assuredName=" + secondName);
296 message = getMailReceiver().receive().getMessage();
297 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
298 assertThat(message, containsString("James Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
299 assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));
301 // get more than 100 VP in total
303 enterVerification("assuredName=" + secondName);
304 message = getMailReceiver().receive().getMessage();
305 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
306 assertThat(message, containsString("James Doe: with 10 to total 60 Verification Points."));
310 enterVerification("assuredName=" + thirdName);
311 message = getMailReceiver().receive().getMessage();
312 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
313 assertThat(message, containsString("James John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
318 public void testVerificationMultiple() throws MalformedURLException, IOException, GigiApiException {
324 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
325 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
328 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
329 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
332 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
333 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
336 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
337 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
339 // add first Verification in the past result first name 45 VP
342 enterVerificationInPast(10, firstName);
344 // add second Verification result first name 50 VP
345 enterVerification("assuredName=" + firstName, 15);
346 message = getMailReceiver().receive().getMessage();
347 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
348 assertThat(message, containsString("John Doe: with 15 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
349 assertThat(message, containsString(requiresMoreTotal(50)));
351 // verify first name to 85 VP
353 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
356 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
359 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
362 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
364 // add first Verification in the past result first name 95 VP
367 enterVerificationInPast(10, firstName);
368 enterVerificationInPast(10, secondName);
370 // add second Verification result first name 100 VP, second name 50 VP
371 enterVerification("assuredName=" + firstName + "&assuredName=" + secondName, 15);
372 message = getMailReceiver().receive().getMessage();
373 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
374 assertThat(message, containsString("John Doe: with 15 to total 100 Verification Points."));
375 assertThat(message, containsString("James Doe: with 15 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
376 assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));