1 package org.cacert.gigi.dbObjects;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
6 import java.sql.Timestamp;
8 import org.cacert.gigi.GigiApiException;
9 import org.cacert.gigi.database.GigiPreparedStatement;
10 import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
11 import org.cacert.gigi.dbObjects.Name;
12 import org.cacert.gigi.dbObjects.NamePart;
13 import org.cacert.gigi.dbObjects.NamePart.NamePartType;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.testUtils.BusinessTest;
16 import org.cacert.gigi.util.DayDate;
17 import org.cacert.gigi.util.Notary;
18 import org.junit.Test;
20 public class TestAssuranceMail extends BusinessTest {
24 private User applicant;
26 private Name firstName;
28 private Name secondName;
30 private Name thirdName;
32 private String message;
34 private void newAgent() throws GigiApiException {
35 agent = User.getById(createAssuranceUser("Marianne", "Mustermann", createUniqueName() + "@example.com", TEST_PASSWORD));
38 private void newApplicant() throws GigiApiException {
39 applicant = User.getById(createVerifiedUser("John", "Doe", createUniqueName() + "@example.com", TEST_PASSWORD));
40 firstName = applicant.getPreferredName();
41 secondName = new Name(applicant, new NamePart(NamePartType.FIRST_NAME, "James"), new NamePart(NamePartType.LAST_NAME, "Doe"));
42 thirdName = new Name(applicant, new NamePart(NamePartType.FIRST_NAME, "James"), new NamePart(NamePartType.FIRST_NAME, "John"), new NamePart(NamePartType.LAST_NAME, "Doe"));
45 private void raiseXP(User agentXP, int recurring) throws GigiApiException {
46 for (int i = 0; i < recurring; i++) {
47 String applicantT = createUniqueName() + "@example.com";
48 int applicantId = createVerifiedUser("John", "Doe", applicantT, TEST_PASSWORD);
49 User applicantXP = User.getById(applicantId);
50 applicantXP = User.getById(applicantId);
51 Notary.assure(agentXP, applicantXP, applicantXP.getNames()[0], applicantXP.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
55 private void enterVerification(Name... names) throws GigiApiException {
56 enterVerification(10, names);
59 private void enterVerification(int points, Name... names) throws GigiApiException {
60 Notary.assureAll(agent, applicant, applicant.getDoB(), points, createUniqueName(), validVerificationDateString(), AssuranceType.FACE_TO_FACE, names);
63 private void enterVerificationInPast(int points, Name name) {
65 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
66 ps.setInt(1, agent.getId());
67 ps.setInt(2, name.getId());
69 ps.setString(4, "test-location");
70 ps.setString(5, "2010-01-01");
71 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
77 public void testVerificationFirstApplicant() throws GigiApiException {
81 // verify preferred name only
82 enterVerification(firstName);
83 message = getMailReceiver().receive().getMessage();
84 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
85 assertThat(message, containsString("John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
87 // verification first two names
90 enterVerification(firstName, secondName);
91 message = getMailReceiver().receive().getMessage();
92 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
93 assertThat(message, containsString("John Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
94 assertThat(message, containsString("James Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
96 // verification all three names
99 enterVerification(firstName, secondName, thirdName);
100 message = getMailReceiver().receive().getMessage();
101 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
102 assertThat(message, containsString("John Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
103 assertThat(message, containsString("James Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
104 assertThat(message, containsString("James John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
106 // New verification preferred name
109 enterVerification(firstName);
110 message = getMailReceiver().receive().getMessage();
111 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
112 assertThat(message, containsString("John Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
114 // verification all three names reaches 50 VP
117 enterVerification(firstName, secondName, thirdName);
118 message = getMailReceiver().receive().getMessage();
119 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
120 assertThat(message, containsString("John Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
121 assertThat(message, containsString("James Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
122 assertThat(message, containsString("James John Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
123 assertThat(message, containsString(requiresMoreTotal(50)));
125 // verification all three names reaches 60 VP
128 enterVerification(firstName, secondName, thirdName);
129 message = getMailReceiver().receive().getMessage();
130 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
131 assertThat(message, containsString("John Doe: with 10 to total 60 Verification Points."));
132 assertThat(message, containsString("James Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
133 assertThat(message, containsString("James John Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
134 assertThat(message, containsString(requiresMoreTotal(40)));
136 // verification all three names reaches 70 VP
139 enterVerification(firstName, secondName, thirdName);
140 message = getMailReceiver().receive().getMessage();
141 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
142 assertThat(message, containsString("John Doe: with 10 to total 70 Verification Points."));
143 assertThat(message, containsString("James Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
144 assertThat(message, containsString("James John Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
145 assertThat(message, containsString(requiresMoreTotal(30)));
147 // verification all three names reaches 80 VP
150 enterVerification(firstName, secondName, thirdName);
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 80 Verification Points."));
154 assertThat(message, containsString("James Doe: with 10 to total 60 Verification Points."));
155 assertThat(message, containsString("James John Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
156 assertThat(message, containsString(requiresMoreTotal(20)));
158 // verification all three names reaches 90 VP
161 enterVerification(firstName, secondName, thirdName);
162 message = getMailReceiver().receive().getMessage();
163 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
164 assertThat(message, containsString("John Doe: with 10 to total 90 Verification Points."));
165 assertThat(message, containsString("James Doe: with 10 to total 70 Verification Points."));
166 assertThat(message, containsString("James John Doe: with 10 to total 60 Verification Points."));
167 assertThat(message, containsString(requiresMoreTotal(10)));
169 // verification all three names reaches 100 VP
172 enterVerification(firstName, secondName, thirdName);
173 message = getMailReceiver().receive().getMessage();
174 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
175 assertThat(message, containsString("John Doe: with 10 to total 100 Verification Points."));
176 assertThat(message, containsString("James Doe: with 10 to total 80 Verification Points."));
177 assertThat(message, containsString("James John Doe: with 10 to total 70 Verification Points."));
178 assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));
180 // verification all three names reaches 100 VP
183 enterVerification(firstName, secondName, thirdName);
184 message = getMailReceiver().receive().getMessage();
185 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
186 assertThat(message, containsString("John Doe: with 10 to total 110 Verification Points."));
187 assertThat(message, containsString("James Doe: with 10 to total 90 Verification Points."));
188 assertThat(message, containsString("James John Doe: with 10 to total 80 Verification Points."));
191 private String requiresMore(int points) {
192 return "To issue client certificates with this name you need " + points + " more Verification Points.";
195 private String requiresMoreTotal(int points) {
196 return "To apply for RA Agent status or code signing ability you need " + points + " more Verification Points.";
200 public void testVerificationSecondApplicant() throws GigiApiException {
203 // verify preferred name only 5 times
205 enterVerification(firstName);
206 message = getMailReceiver().receive().getMessage();
209 enterVerification(firstName);
210 message = getMailReceiver().receive().getMessage();
213 enterVerification(firstName);
214 message = getMailReceiver().receive().getMessage();
217 enterVerification(firstName);
218 message = getMailReceiver().receive().getMessage();
221 enterVerification(firstName);
223 message = getMailReceiver().receive().getMessage();
224 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
225 assertThat(message, containsString("John Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
226 assertThat(message, containsString(requiresMoreTotal(50)));
228 // verify preferred name second name
230 enterVerification(secondName);
231 message = getMailReceiver().receive().getMessage();
232 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
233 assertThat(message, containsString("James Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
234 assertThat(message, containsString(requiresMoreTotal(40)));
236 // verify preferred name second name 4 more times
238 enterVerification(secondName);
239 message = getMailReceiver().receive().getMessage();
242 enterVerification(secondName);
243 message = getMailReceiver().receive().getMessage();
246 enterVerification(secondName);
247 message = getMailReceiver().receive().getMessage();
250 enterVerification(secondName);
251 message = getMailReceiver().receive().getMessage();
252 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
253 assertThat(message, containsString("James Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
254 assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));
256 // get more than 100 VP in total
258 enterVerification(secondName);
259 message = getMailReceiver().receive().getMessage();
260 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
261 assertThat(message, containsString("James Doe: with 10 to total 60 Verification Points."));
265 enterVerification(thirdName);
266 message = getMailReceiver().receive().getMessage();
267 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
268 assertThat(message, containsString("James John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
273 public void testVerificationMultiple() throws GigiApiException {
278 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
279 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
282 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
283 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
286 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
287 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
290 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
291 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
293 // add first Verification in the past result first name 45 VP
296 enterVerificationInPast(10, firstName);
298 // add second Verification result first name 50 VP
299 enterVerification(15, firstName);
300 message = getMailReceiver().receive().getMessage();
301 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
302 assertThat(message, containsString("John Doe: with 15 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
303 assertThat(message, containsString(requiresMoreTotal(50)));
305 // verify first name to 85 VP
307 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
310 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
313 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
316 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
318 // add first Verification in the past result first name 95 VP
321 enterVerificationInPast(10, firstName);
322 enterVerificationInPast(10, secondName);
324 // add second Verification result first name 100 VP, second name 50 VP
325 enterVerification(15, firstName, secondName);
326 message = getMailReceiver().receive().getMessage();
327 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
328 assertThat(message, containsString("John Doe: with 15 to total 100 Verification Points."));
329 assertThat(message, containsString("James Doe: with 15 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
330 assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));