]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/dbObjects/TestAssuranceMail.java
upd: convert TestAssuranceMail into a Business test.
[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.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;
19
20 public class TestAssuranceMail extends BusinessTest {
21
22     private User agent;
23
24     private User applicant;
25
26     private Name firstName;
27
28     private Name secondName;
29
30     private Name thirdName;
31
32     private String message;
33
34     private void newAgent() throws GigiApiException {
35         agent = User.getById(createAssuranceUser("Marianne", "Mustermann", createUniqueName() + "@example.com", TEST_PASSWORD));
36     }
37
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"));
43     }
44
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);
52         }
53     }
54
55     private void enterVerification(Name... names) throws GigiApiException {
56         enterVerification(10, names);
57     }
58
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);
61     }
62
63     private void enterVerificationInPast(int points, Name name) {
64
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());
68             ps.setInt(3, points);
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));
72             ps.execute();
73         }
74     }
75
76     @Test
77     public void testVerificationFirstApplicant() throws GigiApiException {
78         newApplicant();
79         newAgent();
80
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)));
86
87         // verification first two names
88         newAgent();
89
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)));
95
96         // verification all three names
97         newAgent();
98
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)));
105
106         // New verification preferred name
107         newAgent();
108
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)));
113
114         // verification all three names reaches 50 VP
115         newAgent();
116
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)));
124
125         // verification all three names reaches 60 VP
126         newAgent();
127
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)));
135
136         // verification all three names reaches 70 VP
137         newAgent();
138
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)));
146
147         // verification all three names reaches 80 VP
148         newAgent();
149
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)));
157
158         // verification all three names reaches 90 VP
159         newAgent();
160
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)));
168
169         // verification all three names reaches 100 VP
170         newAgent();
171
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."));
179
180         // verification all three names reaches 100 VP
181         newAgent();
182
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."));
189     }
190
191     private String requiresMore(int points) {
192         return "To issue client certificates with this name you need " + points + " more Verification Points.";
193     }
194
195     private String requiresMoreTotal(int points) {
196         return "To apply for RA Agent status or code signing ability you need " + points + " more Verification Points.";
197     }
198
199     @Test
200     public void testVerificationSecondApplicant() throws GigiApiException {
201         newApplicant();
202
203         // verify preferred name only 5 times
204         newAgent();
205         enterVerification(firstName);
206         message = getMailReceiver().receive().getMessage();
207
208         newAgent();
209         enterVerification(firstName);
210         message = getMailReceiver().receive().getMessage();
211
212         newAgent();
213         enterVerification(firstName);
214         message = getMailReceiver().receive().getMessage();
215
216         newAgent();
217         enterVerification(firstName);
218         message = getMailReceiver().receive().getMessage();
219
220         newAgent();
221         enterVerification(firstName);
222
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)));
227
228         // verify preferred name second name
229         newAgent();
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)));
235
236         // verify preferred name second name 4 more times
237         newAgent();
238         enterVerification(secondName);
239         message = getMailReceiver().receive().getMessage();
240
241         newAgent();
242         enterVerification(secondName);
243         message = getMailReceiver().receive().getMessage();
244
245         newAgent();
246         enterVerification(secondName);
247         message = getMailReceiver().receive().getMessage();
248
249         newAgent();
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."));
255
256         // get more than 100 VP in total
257         newAgent();
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."));
262
263         // verify third name
264         newAgent();
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)));
269
270     }
271
272     @Test
273     public void testVerificationMultiple() throws GigiApiException {
274         newApplicant();
275
276         // verify with 35 VP
277         newAgent();
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);
280
281         newAgent();
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);
284
285         newAgent();
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);
288
289         newAgent();
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);
292
293         // add first Verification in the past result first name 45 VP
294         newAgent();
295         raiseXP(agent, 5);
296         enterVerificationInPast(10, firstName);
297
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)));
304
305         // verify first name to 85 VP
306         newAgent();
307         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
308
309         newAgent();
310         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
311
312         newAgent();
313         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
314
315         newAgent();
316         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
317
318         // add first Verification in the past result first name 95 VP
319         newAgent();
320         raiseXP(agent, 5);
321         enterVerificationInPast(10, firstName);
322         enterVerificationInPast(10, secondName);
323
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."));
331     }
332 }