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.Country.CountryCodeType;
12 import org.cacert.gigi.dbObjects.NamePart.NamePartType;
13 import org.cacert.gigi.testUtils.BusinessTest;
14 import org.cacert.gigi.util.DayDate;
15 import org.cacert.gigi.util.Notary;
16 import org.junit.Test;
18 public class TestAssuranceMail extends BusinessTest {
22 private User applicant;
24 private Name firstName;
26 private Name secondName;
28 private Name thirdName;
30 private String message;
32 private void newAgent() throws GigiApiException {
33 agent = User.getById(createAssuranceUser("Marianne", "Mustermann", createUniqueName() + "@example.com", TEST_PASSWORD));
36 private void newApplicant() throws GigiApiException {
37 applicant = User.getById(createVerifiedUser("John", "Doe", createUniqueName() + "@example.com", TEST_PASSWORD));
38 firstName = applicant.getPreferredName();
39 secondName = new Name(applicant, new NamePart(NamePartType.FIRST_NAME, "James"), new NamePart(NamePartType.LAST_NAME, "Doe"));
40 thirdName = new Name(applicant, new NamePart(NamePartType.FIRST_NAME, "James"), new NamePart(NamePartType.FIRST_NAME, "John"), new NamePart(NamePartType.LAST_NAME, "Doe"));
43 private void raiseXP(User agentXP, int recurring) throws GigiApiException {
44 for (int i = 0; i < recurring; i++) {
45 String applicantT = createUniqueName() + "@example.com";
46 int applicantId = createVerifiedUser("John", "Doe", applicantT, TEST_PASSWORD);
47 User applicantXP = User.getById(applicantId);
48 applicantXP = User.getById(applicantId);
49 Notary.assure(agentXP, applicantXP, applicantXP.getNames()[0], applicantXP.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
53 private void enterVerification(Name... names) throws GigiApiException {
54 enterVerification(10, names);
57 private void enterVerification(int points, Name... names) throws GigiApiException {
58 Notary.assureAll(agent, applicant, applicant.getDoB(), points, createUniqueName(), validVerificationDateString(), AssuranceType.FACE_TO_FACE, names, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
61 private void enterVerificationInPast(int points, Name name) {
63 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
64 ps.setInt(1, agent.getId());
65 ps.setInt(2, name.getId());
67 ps.setString(4, "test-location");
68 ps.setString(5, "2010-01-01");
69 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
75 public void testVerificationFirstApplicant() throws GigiApiException {
79 // verify preferred name only
80 enterVerification(firstName);
81 message = getMailReceiver().receive().getMessage();
82 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
83 assertThat(message, containsString("John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
85 // verification first two names
88 enterVerification(firstName, secondName);
89 message = getMailReceiver().receive().getMessage();
90 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
91 assertThat(message, containsString("John Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
92 assertThat(message, containsString("James Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
94 // verification all three names
97 enterVerification(firstName, secondName, thirdName);
98 message = getMailReceiver().receive().getMessage();
99 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
100 assertThat(message, containsString("John Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
101 assertThat(message, containsString("James Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
102 assertThat(message, containsString("James John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
104 // New verification preferred name
107 enterVerification(firstName);
108 message = getMailReceiver().receive().getMessage();
109 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
110 assertThat(message, containsString("John Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
112 // verification all three names reaches 50 VP
115 enterVerification(firstName, secondName, thirdName);
116 message = getMailReceiver().receive().getMessage();
117 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
118 assertThat(message, containsString("John Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
119 assertThat(message, containsString("James Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
120 assertThat(message, containsString("James John Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
121 assertThat(message, containsString(requiresMoreTotal(50)));
123 // verification all three names reaches 60 VP
126 enterVerification(firstName, secondName, thirdName);
127 message = getMailReceiver().receive().getMessage();
128 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
129 assertThat(message, containsString("John Doe: with 10 to total 60 Verification Points."));
130 assertThat(message, containsString("James Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
131 assertThat(message, containsString("James John Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
132 assertThat(message, containsString(requiresMoreTotal(40)));
134 // verification all three names reaches 70 VP
137 enterVerification(firstName, secondName, thirdName);
138 message = getMailReceiver().receive().getMessage();
139 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
140 assertThat(message, containsString("John Doe: with 10 to total 70 Verification Points."));
141 assertThat(message, containsString("James Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
142 assertThat(message, containsString("James John Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
143 assertThat(message, containsString(requiresMoreTotal(30)));
145 // verification all three names reaches 80 VP
148 enterVerification(firstName, secondName, thirdName);
149 message = getMailReceiver().receive().getMessage();
150 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
151 assertThat(message, containsString("John Doe: with 10 to total 80 Verification Points."));
152 assertThat(message, containsString("James Doe: with 10 to total 60 Verification Points."));
153 assertThat(message, containsString("James John Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
154 assertThat(message, containsString(requiresMoreTotal(20)));
156 // verification all three names reaches 90 VP
159 enterVerification(firstName, secondName, thirdName);
160 message = getMailReceiver().receive().getMessage();
161 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
162 assertThat(message, containsString("John Doe: with 10 to total 90 Verification Points."));
163 assertThat(message, containsString("James Doe: with 10 to total 70 Verification Points."));
164 assertThat(message, containsString("James John Doe: with 10 to total 60 Verification Points."));
165 assertThat(message, containsString(requiresMoreTotal(10)));
167 // verification all three names reaches 100 VP
170 enterVerification(firstName, secondName, thirdName);
171 message = getMailReceiver().receive().getMessage();
172 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
173 assertThat(message, containsString("John Doe: with 10 to total 100 Verification Points."));
174 assertThat(message, containsString("James Doe: with 10 to total 80 Verification Points."));
175 assertThat(message, containsString("James John Doe: with 10 to total 70 Verification Points."));
176 assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));
178 // verification all three names reaches 100 VP
181 enterVerification(firstName, secondName, thirdName);
182 message = getMailReceiver().receive().getMessage();
183 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
184 assertThat(message, containsString("John Doe: with 10 to total 110 Verification Points."));
185 assertThat(message, containsString("James Doe: with 10 to total 90 Verification Points."));
186 assertThat(message, containsString("James John Doe: with 10 to total 80 Verification Points."));
189 private String requiresMore(int points) {
190 return "To issue client certificates with this name you need " + points + " more Verification Points.";
193 private String requiresMoreTotal(int points) {
194 return "To apply for RA Agent status or code signing ability you need " + points + " more Verification Points.";
198 public void testVerificationSecondApplicant() throws GigiApiException {
201 // verify preferred name only 5 times
203 enterVerification(firstName);
204 message = getMailReceiver().receive().getMessage();
207 enterVerification(firstName);
208 message = getMailReceiver().receive().getMessage();
211 enterVerification(firstName);
212 message = getMailReceiver().receive().getMessage();
215 enterVerification(firstName);
216 message = getMailReceiver().receive().getMessage();
219 enterVerification(firstName);
221 message = getMailReceiver().receive().getMessage();
222 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
223 assertThat(message, containsString("John Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
224 assertThat(message, containsString(requiresMoreTotal(50)));
226 // verify preferred name second name
228 enterVerification(secondName);
229 message = getMailReceiver().receive().getMessage();
230 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
231 assertThat(message, containsString("James Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
232 assertThat(message, containsString(requiresMoreTotal(40)));
234 // verify preferred name second name 4 more times
236 enterVerification(secondName);
237 message = getMailReceiver().receive().getMessage();
240 enterVerification(secondName);
241 message = getMailReceiver().receive().getMessage();
244 enterVerification(secondName);
245 message = getMailReceiver().receive().getMessage();
248 enterVerification(secondName);
249 message = getMailReceiver().receive().getMessage();
250 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
251 assertThat(message, containsString("James Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
252 assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));
254 // get more than 100 VP in total
256 enterVerification(secondName);
257 message = getMailReceiver().receive().getMessage();
258 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
259 assertThat(message, containsString("James Doe: with 10 to total 60 Verification Points."));
263 enterVerification(thirdName);
264 message = getMailReceiver().receive().getMessage();
265 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
266 assertThat(message, containsString("James John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
271 public void testVerificationMultiple() throws GigiApiException {
276 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
277 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
280 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
281 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
284 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
285 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
288 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
289 Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
291 // add first Verification in the past result first name 45 VP
294 enterVerificationInPast(10, firstName);
296 // add second Verification result first name 50 VP
297 enterVerification(15, firstName);
298 message = getMailReceiver().receive().getMessage();
299 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
300 assertThat(message, containsString("John Doe: with 15 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
301 assertThat(message, containsString(requiresMoreTotal(50)));
303 // verify first name to 85 VP
305 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
308 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
311 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
314 Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
316 // add first Verification in the past result first name 95 VP
319 enterVerificationInPast(10, firstName);
320 enterVerificationInPast(10, secondName);
322 // add second Verification result first name 100 VP, second name 50 VP
323 enterVerification(15, firstName, secondName);
324 message = getMailReceiver().receive().getMessage();
325 assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
326 assertThat(message, containsString("John Doe: with 15 to total 100 Verification Points."));
327 assertThat(message, containsString("James Doe: with 15 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
328 assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));