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