5efc69e88d319420a448ce57d4d86c71ca37a180
[gigi.git] / tests / club / wpia / gigi / dbObjects / TestAssuranceMail.java
1 package club.wpia.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.junit.Test;
9
10 import club.wpia.gigi.GigiApiException;
11 import club.wpia.gigi.database.GigiPreparedStatement;
12 import club.wpia.gigi.dbObjects.Country;
13 import club.wpia.gigi.dbObjects.Name;
14 import club.wpia.gigi.dbObjects.NamePart;
15 import club.wpia.gigi.dbObjects.User;
16 import club.wpia.gigi.dbObjects.Assurance.AssuranceType;
17 import club.wpia.gigi.dbObjects.Country.CountryCodeType;
18 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
19 import club.wpia.gigi.testUtils.BusinessTest;
20 import club.wpia.gigi.util.DayDate;
21 import club.wpia.gigi.util.Notary;
22
23 public class TestAssuranceMail extends BusinessTest {
24
25     private User agent;
26
27     private User applicant;
28
29     private Name firstName;
30
31     private Name secondName;
32
33     private Name thirdName;
34
35     private String message;
36
37     private void newAgent() throws GigiApiException {
38         agent = User.getById(createAssuranceUser("Marianne", "Mustermann", createUniqueName() + "@example.com", TEST_PASSWORD));
39     }
40
41     private void newApplicant() throws GigiApiException {
42         applicant = User.getById(createVerifiedUser("John", "Doe", createUniqueName() + "@example.com", TEST_PASSWORD));
43         firstName = applicant.getPreferredName();
44         secondName = new Name(applicant, new NamePart(NamePartType.FIRST_NAME, "James"), new NamePart(NamePartType.LAST_NAME, "Doe"));
45         thirdName = new Name(applicant, new NamePart(NamePartType.FIRST_NAME, "James"), new NamePart(NamePartType.FIRST_NAME, "John"), new NamePart(NamePartType.LAST_NAME, "Doe"));
46     }
47
48     private void raiseXP(User agentXP, int recurring) throws GigiApiException {
49         for (int i = 0; i < recurring; i++) {
50             String applicantT = createUniqueName() + "@example.com";
51             int applicantId = createVerifiedUser("John", "Doe", applicantT, TEST_PASSWORD);
52             User applicantXP = User.getById(applicantId);
53             applicantXP = User.getById(applicantId);
54             Notary.assure(agentXP, applicantXP, applicantXP.getNames()[0], applicantXP.getDoB(), 10, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
55         }
56     }
57
58     private void enterVerification(Name... names) throws GigiApiException {
59         enterVerification(10, names);
60     }
61
62     private void enterVerification(int points, Name... names) throws GigiApiException {
63         Notary.assureAll(agent, applicant, applicant.getDoB(), points, createUniqueName(), validVerificationDateString(), AssuranceType.FACE_TO_FACE, names, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
64     }
65
66     private void enterVerificationInPast(int points, Name name) {
67
68         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
69             ps.setInt(1, agent.getId());
70             ps.setInt(2, name.getId());
71             ps.setInt(3, points);
72             ps.setString(4, "test-location");
73             ps.setString(5, "2010-01-01");
74             ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
75             ps.execute();
76         }
77     }
78
79     @Test
80     public void testVerificationFirstApplicant() throws GigiApiException {
81         newApplicant();
82         newAgent();
83
84         // verify preferred name only
85         enterVerification(firstName);
86         message = getMailReceiver().receive().getMessage();
87         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
88         assertThat(message, containsString("John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
89
90         // verification first two names
91         newAgent();
92
93         enterVerification(firstName, secondName);
94         message = getMailReceiver().receive().getMessage();
95         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
96         assertThat(message, containsString("John Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
97         assertThat(message, containsString("James Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
98
99         // verification all three names
100         newAgent();
101
102         enterVerification(firstName, secondName, thirdName);
103         message = getMailReceiver().receive().getMessage();
104         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
105         assertThat(message, containsString("John Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
106         assertThat(message, containsString("James Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
107         assertThat(message, containsString("James John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
108
109         // New verification preferred name
110         newAgent();
111
112         enterVerification(firstName);
113         message = getMailReceiver().receive().getMessage();
114         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
115         assertThat(message, containsString("John Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
116
117         // verification all three names reaches 50 VP
118         newAgent();
119
120         enterVerification(firstName, secondName, thirdName);
121         message = getMailReceiver().receive().getMessage();
122         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
123         assertThat(message, containsString("John Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
124         assertThat(message, containsString("James Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
125         assertThat(message, containsString("James John Doe: with 10 to total 20 Verification Points." + "\n" + requiresMore(30)));
126         assertThat(message, containsString(requiresMoreTotal(50)));
127
128         // verification all three names reaches 60 VP
129         newAgent();
130
131         enterVerification(firstName, secondName, thirdName);
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 60 Verification Points."));
135         assertThat(message, containsString("James Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
136         assertThat(message, containsString("James John Doe: with 10 to total 30 Verification Points." + "\n" + requiresMore(20)));
137         assertThat(message, containsString(requiresMoreTotal(40)));
138
139         // verification all three names reaches 70 VP
140         newAgent();
141
142         enterVerification(firstName, secondName, thirdName);
143         message = getMailReceiver().receive().getMessage();
144         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
145         assertThat(message, containsString("John Doe: with 10 to total 70 Verification Points."));
146         assertThat(message, containsString("James Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
147         assertThat(message, containsString("James John Doe: with 10 to total 40 Verification Points." + "\n" + requiresMore(10)));
148         assertThat(message, containsString(requiresMoreTotal(30)));
149
150         // verification all three names reaches 80 VP
151         newAgent();
152
153         enterVerification(firstName, secondName, thirdName);
154         message = getMailReceiver().receive().getMessage();
155         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
156         assertThat(message, containsString("John Doe: with 10 to total 80 Verification Points."));
157         assertThat(message, containsString("James Doe: with 10 to total 60 Verification Points."));
158         assertThat(message, containsString("James John Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
159         assertThat(message, containsString(requiresMoreTotal(20)));
160
161         // verification all three names reaches 90 VP
162         newAgent();
163
164         enterVerification(firstName, secondName, thirdName);
165         message = getMailReceiver().receive().getMessage();
166         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
167         assertThat(message, containsString("John Doe: with 10 to total 90 Verification Points."));
168         assertThat(message, containsString("James Doe: with 10 to total 70 Verification Points."));
169         assertThat(message, containsString("James John Doe: with 10 to total 60 Verification Points."));
170         assertThat(message, containsString(requiresMoreTotal(10)));
171
172         // verification all three names reaches 100 VP
173         newAgent();
174
175         enterVerification(firstName, secondName, thirdName);
176         message = getMailReceiver().receive().getMessage();
177         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
178         assertThat(message, containsString("John Doe: with 10 to total 100 Verification Points."));
179         assertThat(message, containsString("James Doe: with 10 to total 80 Verification Points."));
180         assertThat(message, containsString("James John Doe: with 10 to total 70 Verification Points."));
181         assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));
182
183         // verification all three names reaches 100 VP
184         newAgent();
185
186         enterVerification(firstName, secondName, thirdName);
187         message = getMailReceiver().receive().getMessage();
188         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
189         assertThat(message, containsString("John Doe: with 10 to total 110 Verification Points."));
190         assertThat(message, containsString("James Doe: with 10 to total 90 Verification Points."));
191         assertThat(message, containsString("James John Doe: with 10 to total 80 Verification Points."));
192     }
193
194     private String requiresMore(int points) {
195         return "To issue client certificates with this name you need " + points + " more Verification Points.";
196     }
197
198     private String requiresMoreTotal(int points) {
199         return "To apply for RA Agent status or code signing ability you need " + points + " more Verification Points.";
200     }
201
202     @Test
203     public void testVerificationSecondApplicant() throws GigiApiException {
204         newApplicant();
205
206         // verify preferred name only 5 times
207         newAgent();
208         enterVerification(firstName);
209         message = getMailReceiver().receive().getMessage();
210
211         newAgent();
212         enterVerification(firstName);
213         message = getMailReceiver().receive().getMessage();
214
215         newAgent();
216         enterVerification(firstName);
217         message = getMailReceiver().receive().getMessage();
218
219         newAgent();
220         enterVerification(firstName);
221         message = getMailReceiver().receive().getMessage();
222
223         newAgent();
224         enterVerification(firstName);
225
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 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
229         assertThat(message, containsString(requiresMoreTotal(50)));
230
231         // verify preferred name second name
232         newAgent();
233         enterVerification(secondName);
234         message = getMailReceiver().receive().getMessage();
235         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
236         assertThat(message, containsString("James Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
237         assertThat(message, containsString(requiresMoreTotal(40)));
238
239         // verify preferred name second name 4 more times
240         newAgent();
241         enterVerification(secondName);
242         message = getMailReceiver().receive().getMessage();
243
244         newAgent();
245         enterVerification(secondName);
246         message = getMailReceiver().receive().getMessage();
247
248         newAgent();
249         enterVerification(secondName);
250         message = getMailReceiver().receive().getMessage();
251
252         newAgent();
253         enterVerification(secondName);
254         message = getMailReceiver().receive().getMessage();
255         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
256         assertThat(message, containsString("James Doe: with 10 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
257         assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));
258
259         // get more than 100 VP in total
260         newAgent();
261         enterVerification(secondName);
262         message = getMailReceiver().receive().getMessage();
263         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
264         assertThat(message, containsString("James Doe: with 10 to total 60 Verification Points."));
265
266         // verify third name
267         newAgent();
268         enterVerification(thirdName);
269         message = getMailReceiver().receive().getMessage();
270         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
271         assertThat(message, containsString("James John Doe: with 10 to total 10 Verification Points." + "\n" + requiresMore(40)));
272
273     }
274
275     @Test
276     public void testVerificationMultiple() throws GigiApiException {
277         newApplicant();
278
279         // verify with 35 VP
280         newAgent();
281         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
282         Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
283
284         newAgent();
285         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
286         Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
287
288         newAgent();
289         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
290         Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 10, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
291
292         newAgent();
293         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
294         Notary.assure(agent, applicant, applicant.getNames()[1], applicant.getDoB(), 5, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
295
296         // add first Verification in the past result first name 45 VP
297         newAgent();
298         raiseXP(agent, 5);
299         enterVerificationInPast(10, firstName);
300
301         // add second Verification result first name 50 VP
302         enterVerification(15, firstName);
303         message = getMailReceiver().receive().getMessage();
304         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
305         assertThat(message, containsString("John Doe: with 15 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
306         assertThat(message, containsString(requiresMoreTotal(50)));
307
308         // verify first name to 85 VP
309         newAgent();
310         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
311
312         newAgent();
313         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
314
315         newAgent();
316         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 10, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
317
318         newAgent();
319         Notary.assure(agent, applicant, applicant.getNames()[0], applicant.getDoB(), 5, "Test location", validVerificationDateString(), AssuranceType.FACE_TO_FACE, Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS));
320
321         // add first Verification in the past result first name 95 VP
322         newAgent();
323         raiseXP(agent, 5);
324         enterVerificationInPast(10, firstName);
325         enterVerificationInPast(10, secondName);
326
327         // add second Verification result first name 100 VP, second name 50 VP
328         enterVerification(15, firstName, secondName);
329         message = getMailReceiver().receive().getMessage();
330         assertThat(message, containsString("RA-Agent Marianne Mustermann verified your name(s):"));
331         assertThat(message, containsString("John Doe: with 15 to total 100 Verification Points."));
332         assertThat(message, containsString("James Doe: with 15 to total 50 Verification Points." + "\n" + "You can now issue client certificates with this name."));
333         assertThat(message, containsString("You can now apply for RA Agent status or code signing ability."));
334     }
335 }