]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/wot/TestAssuranceMail.java
add: add some test case verification notification for multiple
[gigi.git] / tests / org / cacert / gigi / pages / wot / TestAssuranceMail.java
1 package org.cacert.gigi.pages.wot;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
6 import java.io.IOException;
7 import java.net.MalformedURLException;
8 import java.net.URLConnection;
9 import java.sql.Timestamp;
10
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;
22
23 public class TestAssuranceMail extends ManagedTest {
24
25     private String agentM;
26
27     private String applicantM;
28
29     private String cookieAgent;
30
31     private String cookieApplicant;
32
33     private User agent;
34
35     private User applicant;
36
37     private int firstName;
38
39     private int secondName;
40
41     private int thirdName;
42
43     private String message;
44
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);
50
51     }
52
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();
61         firstName = 0;
62         secondName = 0;
63         thirdName = 0;
64         for (int i = 0; i < names.length; i++) {
65             if (names[i].toString().equals("John Doe")) {
66                 firstName = names[i].getId();
67             }
68             if (names[i].toString().equals("James Doe")) {
69                 secondName = names[i].getId();
70             }
71             if (names[i].toString().equals("James John Doe")) {
72                 thirdName = names[i].getId();
73             }
74         }
75         assertNotEquals(0, firstName);
76         assertNotEquals(0, secondName);
77         assertNotEquals(0, thirdName);
78     }
79
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);
87         }
88     }
89
90     private String enterVerification(String query) throws MalformedURLException, IOException {
91         return enterVerification(query, 10);
92
93     }
94
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);
100
101     }
102
103     private void enterVerificationInPast(int points, int nameId) {
104
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));
112             ps.execute();
113         }
114     }
115
116     @Test
117     public void testVerificationFirstApplicant() throws MalformedURLException, IOException {
118         clearCaches();
119         newApplicant();
120         newAgent();
121
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)));
127
128         // verification first two names
129         newAgent();
130
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)));
136
137         // verification all three names
138         newAgent();
139
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)));
146
147         // New verification preferred name
148         newAgent();
149
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)));
154
155         // verification all three names reaches 50 VP
156         newAgent();
157
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)));
165
166         // verification all three names reaches 60 VP
167         newAgent();
168
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)));
176
177         // verification all three names reaches 70 VP
178         newAgent();
179
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)));
187
188         // verification all three names reaches 80 VP
189         newAgent();
190
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)));
198
199         // verification all three names reaches 90 VP
200         newAgent();
201
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)));
209
210         // verification all three names reaches 100 VP
211         clearCaches();
212         newAgent();
213
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."));
221
222         // verification all three names reaches 100 VP
223         newAgent();
224
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."));
231     }
232
233     private String requiresMore(int points) {
234         return "To issue client certificates with this name you need " + points + " more Verification Points.";
235     }
236
237     private String requiresMoreTotal(int points) {
238         return "To apply for RA Agent status or code signing ability you need " + points + " more Verification Points.";
239     }
240
241     @Test
242     public void testVerificationSecondApplicant() throws MalformedURLException, IOException {
243         clearCaches();
244         newApplicant();
245
246         // verify preferred name only 5 times
247         newAgent();
248         enterVerification("assuredName=" + firstName);
249         message = getMailReceiver().receive().getMessage();
250
251         newAgent();
252         enterVerification("assuredName=" + firstName);
253         message = getMailReceiver().receive().getMessage();
254
255         newAgent();
256         enterVerification("assuredName=" + firstName);
257         message = getMailReceiver().receive().getMessage();
258
259         newAgent();
260         enterVerification("assuredName=" + firstName);
261         message = getMailReceiver().receive().getMessage();
262
263         newAgent();
264         enterVerification("assuredName=" + firstName);
265
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)));
270
271         clearCaches();
272
273         // verify preferred name second name
274         newAgent();
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)));
280
281         // verify preferred name second name 4 more times
282         newAgent();
283         enterVerification("assuredName=" + secondName);
284         message = getMailReceiver().receive().getMessage();
285
286         newAgent();
287         enterVerification("assuredName=" + secondName);
288         message = getMailReceiver().receive().getMessage();
289
290         newAgent();
291         enterVerification("assuredName=" + secondName);
292         message = getMailReceiver().receive().getMessage();
293
294         newAgent();
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."));
300
301         // get more than 100 VP in total
302         newAgent();
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."));
307
308         // verify third name
309         newAgent();
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)));
314
315     }
316
317     @Test
318     public void testVerificationMultiple() throws MalformedURLException, IOException, GigiApiException {
319         clearCaches();
320         newApplicant();
321
322         // verify with 35 VP
323         newAgent();
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);
326
327         newAgent();
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);
330
331         newAgent();
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);
334
335         newAgent();
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);
338
339         // add first Verification in the past result first name 45 VP
340         newAgent();
341         raiseXP(agent, 5);
342         enterVerificationInPast(10, firstName);
343
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)));
350
351         // verify first name to 85 VP
352         newAgent();
353         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
354
355         newAgent();
356         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
357
358         newAgent();
359         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
360
361         newAgent();
362         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", "2014-11-06", AssuranceType.FACE_TO_FACE);
363
364         // add first Verification in the past result first name 95 VP
365         newAgent();
366         raiseXP(agent, 5);
367         enterVerificationInPast(10, firstName);
368         enterVerificationInPast(10, secondName);
369
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."));
377     }
378 }