]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/wot/TestVerification.java
7a6aed4ea4770fdb9a30e7dd1516c21a844c337e
[gigi.git] / tests / club / wpia / gigi / pages / wot / TestVerification.java
1 package club.wpia.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.io.UnsupportedEncodingException;
8 import java.net.HttpURLConnection;
9 import java.net.MalformedURLException;
10 import java.net.URLConnection;
11 import java.net.URLEncoder;
12 import java.sql.SQLException;
13 import java.sql.Timestamp;
14 import java.text.SimpleDateFormat;
15 import java.util.Calendar;
16 import java.util.Date;
17 import java.util.regex.Pattern;
18
19 import org.hamcrest.Matcher;
20 import org.junit.Before;
21 import org.junit.Test;
22
23 import club.wpia.gigi.GigiApiException;
24 import club.wpia.gigi.database.GigiPreparedStatement;
25 import club.wpia.gigi.dbObjects.Country;
26 import club.wpia.gigi.dbObjects.Group;
27 import club.wpia.gigi.dbObjects.User;
28 import club.wpia.gigi.pages.account.MyDetails;
29 import club.wpia.gigi.testUtils.IOUtils;
30 import club.wpia.gigi.testUtils.ManagedTest;
31 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
32 import club.wpia.gigi.util.DayDate;
33 import club.wpia.gigi.util.Notary;
34
35 public class TestVerification extends ManagedTest {
36
37     private String agentM;
38
39     private String applicantM;
40
41     private int applicantName;
42
43     private String cookie;
44
45     @Before
46     public void setup() throws IOException {
47         clearCaches();
48         agentM = createUniqueName() + "@example.org";
49         applicantM = createUniqueName() + "@example.org";
50
51         createVerificationUser("a", "b", agentM, TEST_PASSWORD);
52         int applicantId = createVerifiedUser("a", "c", applicantM, TEST_PASSWORD);
53         applicantName = User.getById(applicantId).getPreferredName().getId();
54
55         cookie = login(agentM, TEST_PASSWORD);
56     }
57
58     private Matcher<String> isVerificationForm() {
59         return containsString("<select name=\"verificationType\">");
60     }
61
62     @Test
63     public void testVerifySearch() throws IOException {
64         String loc = search("email=" + URLEncoder.encode(applicantM, "UTF-8") + "&day=1&month=1&year=1910");
65         assertThat(loc, isVerificationForm());
66     }
67
68     @Test
69     public void testVerifySearchEmail() throws IOException {
70         String loc = search("email=1" + URLEncoder.encode(applicantM, "UTF-8") + "&day=1&month=1&year=1910");
71         assertThat(loc, not(isVerificationForm()));
72     }
73
74     @Test
75     public void testVerifySearchDobInvalid() throws IOException {
76         String loc = search("email=" + URLEncoder.encode(applicantM, "UTF-8") + "&day=1&month=1&year=mal");
77         assertThat(loc, not(isVerificationForm()));
78     }
79
80     @Test
81     public void testVerifySearchDob() throws IOException {
82         String loc = search("email=" + URLEncoder.encode(applicantM, "UTF-8") + "&day=2&month=1&year=1910");
83         assertThat(loc, not(isVerificationForm()));
84         loc = search("email=" + URLEncoder.encode(applicantM, "UTF-8") + "&day=1&month=2&year=1910");
85         assertThat(loc, not(isVerificationForm()));
86         loc = search("email=" + URLEncoder.encode(applicantM, "UTF-8") + "&day=1&month=1&year=1911");
87         assertThat(loc, not(isVerificationForm()));
88     }
89
90     private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
91         URLConnection uc = get(cookie, VerifyPage.PATH);
92         uc.setDoOutput(true);
93         uc.getOutputStream().write(("search&" + query).getBytes("UTF-8"));
94         uc.getOutputStream().flush();
95
96         return IOUtils.readURL(uc);
97     }
98
99     @Test
100     public void testVerifyForm() throws IOException {
101         String body = executeSuccess("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
102         assertThat(body, containsString("10"));
103         assertThat(body, containsString(applicantM));
104     }
105
106     @Test
107     public void testVerifyFormEmpty() throws IOException {
108         URLConnection uc = buildupVerifyFormConnection(true);
109         uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&rules=1&assertion=1&points=10").getBytes("UTF-8"));
110         uc.getOutputStream().flush();
111         String data = IOUtils.readURL(uc);
112         assertThat(data, hasError());
113     }
114
115     @Test
116     public void testVerifyFormContainsData() throws IOException {
117         URLConnection uc = buildupVerifyFormConnection(true);
118         uc.getOutputStream().write(("verifiedName=" + applicantName + "&date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&rules=1&assertion=1&points=10").getBytes("UTF-8"));
119         uc.getOutputStream().flush();
120         String data = IOUtils.readURL(uc);
121         assertThat(data, containsString(validVerificationDateString()));
122         assertThat(data, containsString("testcase"));
123     }
124
125     @Test
126     public void testVerifyFormNoCSRF() throws IOException {
127         // override csrf
128         HttpURLConnection uc = (HttpURLConnection) buildupVerifyFormConnection(false);
129         uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
130         uc.getOutputStream().flush();
131         assertEquals(500, uc.getResponseCode());
132     }
133
134     @Test
135     public void testVerifyFormWrongCSRF() throws IOException {
136         // override csrf
137         HttpURLConnection uc = (HttpURLConnection) buildupVerifyFormConnection(false);
138         uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10&csrf=aragc").getBytes("UTF-8"));
139         uc.getOutputStream().flush();
140         assertEquals(500, uc.getResponseCode());
141     }
142
143     @Test
144     public void testVerifyFormRaceDoB() throws IOException, SQLException {
145         testVerifyFormRace(false);
146     }
147
148     @Test
149     public void testVerifyFormRaceDoBBlind() throws IOException, SQLException {
150         testVerifyFormRace(true);
151     }
152
153     public void testVerifyFormRace(boolean succeed) throws IOException, SQLException {
154         URLConnection uc = buildupVerifyFormConnection(true);
155
156         String applicantCookie = login(applicantM, TEST_PASSWORD);
157         String newDob = "day=1&month=1&year=" + ( !succeed ? 1911 : 1910);
158
159         assertNull(executeBasicWebInteraction(applicantCookie, MyDetails.PATH, newDob + "&action=updateDoB", 0));
160
161         uc.getOutputStream().write(("verifiedName=" + applicantName + "&date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
162         uc.getOutputStream().flush();
163         String error = fetchStartErrorMessage(IOUtils.readURL(uc));
164         if (succeed) {
165             assertNull(error);
166         } else {
167             assertTrue(error, !error.startsWith("</div>"));
168             assertThat(error, containsString("changed his personal details"));
169         }
170     }
171
172     @Test
173     public void testVerifyFormFuture() throws IOException {
174         SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
175         int year = Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) + 2;
176         executeFails("date=" + year + "-01-01&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
177     }
178
179     @Test
180     public void testVerifyFormFutureOK() throws IOException {
181         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
182         Calendar c = Calendar.getInstance();
183         c.setTimeInMillis(System.currentTimeMillis());
184         c.add(Calendar.HOUR_OF_DAY, 12);
185
186         executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
187     }
188
189     @Test
190     public void testVerifyFormPastInRange() throws IOException {
191         executeSuccess("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
192     }
193
194     @Test
195     public void testVerifyFormPastOnLimit() throws IOException {
196         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
197         Calendar c = Calendar.getInstance();
198         c.setTimeInMillis(System.currentTimeMillis());
199         c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION);
200         c.add(Calendar.DAY_OF_MONTH, 1);
201
202         executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
203     }
204
205     @Test
206     public void testVerifyFormPastOutOfRange() throws IOException {
207         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
208         Calendar c = Calendar.getInstance();
209         c.setTimeInMillis(System.currentTimeMillis());
210         c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION);
211
212         executeFails("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
213     }
214
215     @Test
216     public void testVerifyFormNoLoc() throws IOException {
217         executeFails("date=" + validVerificationDateString() + "&location=a&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
218         executeFails("date=" + validVerificationDateString() + "&location=&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
219     }
220
221     @Test
222     public void testVerifyFormInvalDate() throws IOException {
223         executeFails("date=20000101&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
224         executeFails("date=&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
225     }
226
227     @Test
228     public void testVerifyFormBoxes() throws IOException {
229         executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=0&rules=1&assertion=1&points=10");
230         executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=&assertion=1&points=10");
231         executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=z&points=10");
232     }
233
234     @Test
235     public void testVerifyListingValid() throws IOException, GigiApiException {
236         String uniqueLoc = createUniqueName();
237         execute("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
238
239         String cookie = login(applicantM, TEST_PASSWORD);
240         URLConnection url = get(cookie, Points.PATH);
241         String resp = IOUtils.readURL(url);
242         resp = resp.split(Pattern.quote("</table>"))[1];
243         assertThat(resp, containsString(uniqueLoc));
244         assertThat(resp, containsString(Country.getCountryByCode("DE", Country.CountryCodeType.CODE_2_CHARS).getName()));
245     }
246
247     @Test
248     public void testAgentListingValid() throws IOException, GigiApiException {
249         String uniqueLoc = createUniqueName();
250         executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
251         String cookie = login(agentM, TEST_PASSWORD);
252         URLConnection url = get(cookie, Points.PATH);
253         String resp = IOUtils.readURL(url);
254         resp = resp.split(Pattern.quote("</table>"))[2];
255         assertThat(resp, containsString(uniqueLoc));
256         assertThat(resp, containsString(Country.getCountryByCode("DE", Country.CountryCodeType.CODE_2_CHARS).getName()));
257     }
258
259     private void executeFails(String query) throws MalformedURLException, IOException {
260         assertThat(execute(query), hasError());
261
262     }
263
264     private String executeSuccess(String query) throws MalformedURLException, IOException {
265         String response = execute(query);
266         assertThat(response, hasNoError());
267         return response;
268     }
269
270     private String execute(String query) throws MalformedURLException, IOException {
271         URLConnection uc = buildupVerifyFormConnection(true);
272         uc.getOutputStream().write(("verifiedName=" + applicantName + "&" + query).getBytes("UTF-8"));
273         uc.getOutputStream().flush();
274         return IOUtils.readURL(uc);
275     }
276
277     private URLConnection buildupVerifyFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
278         return buildupVerifyFormConnection(cookie, applicantM, doCSRF);
279     }
280
281     public static URLConnection buildupVerifyFormConnection(String cookie, String email, boolean doCSRF) throws MalformedURLException, IOException {
282         URLConnection uc = get(cookie, VerifyPage.PATH);
283         uc.setDoOutput(true);
284         uc.getOutputStream().write(("email=" + URLEncoder.encode(email, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
285
286         String csrf = getCSRF(uc);
287         uc = get(cookie, VerifyPage.PATH);
288         uc.setDoOutput(true);
289         if (doCSRF) {
290             uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8"));
291         }
292         return uc;
293     }
294
295     @Test
296     public void testMultipleVerification() throws IOException {
297
298         User users[] = User.findByEmail(agentM);
299         int agentID = users[0].getId();
300
301         users = User.findByEmail(applicantM);
302         int applicantID = users[0].getId();
303
304         // enter first entry 200 days in the past
305         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
306             ps.setInt(1, agentID);
307             ps.setInt(2, applicantID);
308             ps.setInt(3, 10);
309             ps.setString(4, "test-location");
310             ps.setString(5, "2010-01-01");
311             ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
312             ps.execute();
313         }
314
315         // enter second entry
316         String uniqueLoc = createUniqueName();
317         executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
318
319         // enter third entry on the same day
320         URLConnection uc = get(cookie, VerifyPage.PATH);
321         uc.setDoOutput(true);
322         uc.getOutputStream().write(("email=" + URLEncoder.encode(applicantM, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
323         assertThat(IOUtils.readURL(uc), hasError());
324
325     }
326
327     @Test
328     public void testVerifyFormNoCountry() throws IOException {
329         executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=&certify=1&rules=1&assertion=1&points=10");
330     }
331
332     @Test
333     public void testRANotificationSet() throws IOException, GigiApiException {
334         getMailReceiver().clearMails();
335
336         User users[] = User.findByEmail(agentM);
337         assertTrue("user RA Agent not found", users != null && users.length > 0);
338
339         User u = users[0];
340         u.grantGroup(u, Group.VERIFY_NOTIFICATION);
341         clearCaches();
342         cookie = login(agentM, TEST_PASSWORD);
343
344         String targetMail = u.getEmail();
345
346         // enter verification
347         String uniqueLoc = createUniqueName();
348         executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
349         TestMail tm;
350
351         do {
352             tm = getMailReceiver().receive();
353         } while ( !tm.getTo().equals(targetMail));
354         assertThat(tm.getMessage(), containsString("You entered a verification for the account with email address " + applicantM));
355
356     }
357
358     @Test
359     public void testRANotificationNotSet() throws IOException, GigiApiException {
360         getMailReceiver().clearMails();
361
362         User users[] = User.findByEmail(agentM);
363         assertTrue("user RA Agent not found", users != null && users.length > 0);
364
365         User u = users[0];
366         u.revokeGroup(u, Group.VERIFY_NOTIFICATION);
367         clearCaches();
368         cookie = login(agentM, TEST_PASSWORD);
369
370         // enter verification
371         String uniqueLoc = createUniqueName();
372         executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
373
374         TestMail tm;
375
376         tm = getMailReceiver().receive();
377         assertThat(tm.getMessage(), not(containsString("You entered a verification for the account with email address " + applicantM)));
378
379     }
380 }