]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/dbObjects/TestAssuranceMail.java
add: country information where verification took place
[gigi.git] / tests / org / cacert / gigi / dbObjects / TestAssuranceMail.java
1 package org.cacert.gigi.dbObjects;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
6 import java.sql.Timestamp;
7
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.CountryCode.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;
17
18 public class TestAssuranceMail extends BusinessTest {
19
20     private User agent;
21
22     private User applicant;
23
24     private Name firstName;
25
26     private Name secondName;
27
28     private Name thirdName;
29
30     private String message;
31
32     private void newAgent() throws GigiApiException {
33         agent = User.getById(createAssuranceUser("Marianne", "Mustermann", createUniqueName() + "@example.com", TEST_PASSWORD));
34     }
35
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"));
41     }
42
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, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS));
50         }
51     }
52
53     private void enterVerification(Name... names) throws GigiApiException {
54         enterVerification(10, names);
55     }
56
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, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS));
59     }
60
61     private void enterVerificationInPast(int points, Name name) {
62
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());
66             ps.setInt(3, points);
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));
70             ps.execute();
71         }
72     }
73
74     @Test
75     public void testVerificationFirstApplicant() throws GigiApiException {
76         newApplicant();
77         newAgent();
78
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)));
84
85         // verification first two names
86         newAgent();
87
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)));
93
94         // verification all three names
95         newAgent();
96
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)));
103
104         // New verification preferred name
105         newAgent();
106
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)));
111
112         // verification all three names reaches 50 VP
113         newAgent();
114
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)));
122
123         // verification all three names reaches 60 VP
124         newAgent();
125
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)));
133
134         // verification all three names reaches 70 VP
135         newAgent();
136
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)));
144
145         // verification all three names reaches 80 VP
146         newAgent();
147
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)));
155
156         // verification all three names reaches 90 VP
157         newAgent();
158
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)));
166
167         // verification all three names reaches 100 VP
168         newAgent();
169
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."));
177
178         // verification all three names reaches 100 VP
179         newAgent();
180
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."));
187     }
188
189     private String requiresMore(int points) {
190         return "To issue client certificates with this name you need " + points + " more Verification Points.";
191     }
192
193     private String requiresMoreTotal(int points) {
194         return "To apply for RA Agent status or code signing ability you need " + points + " more Verification Points.";
195     }
196
197     @Test
198     public void testVerificationSecondApplicant() throws GigiApiException {
199         newApplicant();
200
201         // verify preferred name only 5 times
202         newAgent();
203         enterVerification(firstName);
204         message = getMailReceiver().receive().getMessage();
205
206         newAgent();
207         enterVerification(firstName);
208         message = getMailReceiver().receive().getMessage();
209
210         newAgent();
211         enterVerification(firstName);
212         message = getMailReceiver().receive().getMessage();
213
214         newAgent();
215         enterVerification(firstName);
216         message = getMailReceiver().receive().getMessage();
217
218         newAgent();
219         enterVerification(firstName);
220
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)));
225
226         // verify preferred name second name
227         newAgent();
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)));
233
234         // verify preferred name second name 4 more times
235         newAgent();
236         enterVerification(secondName);
237         message = getMailReceiver().receive().getMessage();
238
239         newAgent();
240         enterVerification(secondName);
241         message = getMailReceiver().receive().getMessage();
242
243         newAgent();
244         enterVerification(secondName);
245         message = getMailReceiver().receive().getMessage();
246
247         newAgent();
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."));
253
254         // get more than 100 VP in total
255         newAgent();
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."));
260
261         // verify third name
262         newAgent();
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)));
267
268     }
269
270     @Test
271     public void testVerificationMultiple() throws GigiApiException {
272         newApplicant();
273
274         // verify with 35 VP
275         newAgent();
276         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, CountryCode.getCountryCode("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, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS));
278
279         newAgent();
280         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, CountryCode.getCountryCode("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, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS));
282
283         newAgent();
284         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, CountryCode.getCountryCode("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, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS));
286
287         newAgent();
288         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, CountryCode.getCountryCode("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, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS));
290
291         // add first Verification in the past result first name 45 VP
292         newAgent();
293         raiseXP(agent, 5);
294         enterVerificationInPast(10, firstName);
295
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)));
302
303         // verify first name to 85 VP
304         newAgent();
305         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS));
306
307         newAgent();
308         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS));
309
310         newAgent();
311         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS));
312
313         newAgent();
314         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE, CountryCode.getCountryCode("DE", CountryCodeType.CODE_2_CHARS));
315
316         // add first Verification in the past result first name 95 VP
317         newAgent();
318         raiseXP(agent, 5);
319         enterVerificationInPast(10, firstName);
320         enterVerificationInPast(10, secondName);
321
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."));
329     }
330 }