1 package club.wpia.gigi.pages.wot;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
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;
19 import org.hamcrest.Matcher;
20 import org.junit.Before;
21 import org.junit.Test;
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;
35 public class TestVerification extends ManagedTest {
37 private String agentM;
39 private String applicantM;
41 private int applicantName;
43 private String cookie;
46 public void setup() throws IOException {
48 agentM = createUniqueName() + "@example.org";
49 applicantM = createUniqueName() + "@example.org";
51 createVerificationUser("a", "b", agentM, TEST_PASSWORD);
52 int applicantId = createVerifiedUser("a", "c", applicantM, TEST_PASSWORD);
53 applicantName = User.getById(applicantId).getPreferredName().getId();
55 cookie = login(agentM, TEST_PASSWORD);
58 private Matcher<String> isVerificationForm() {
59 return containsString("<select name=\"verificationType\">");
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());
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()));
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()));
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()));
90 private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
91 URLConnection uc = get(cookie, VerifyPage.PATH);
93 uc.getOutputStream().write(("search&" + query).getBytes("UTF-8"));
94 uc.getOutputStream().flush();
96 return IOUtils.readURL(uc);
100 public void testVerifyForm() throws IOException {
101 executeSuccess("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
105 public void testVerifyFormEmpty() throws IOException {
106 URLConnection uc = buildupVerifyFormConnection(true);
107 uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&rules=1&assertion=1&points=10").getBytes("UTF-8"));
108 uc.getOutputStream().flush();
109 String data = IOUtils.readURL(uc);
110 assertThat(data, hasError());
114 public void testVerifyFormContainsData() throws IOException {
115 URLConnection uc = buildupVerifyFormConnection(true);
116 uc.getOutputStream().write(("verifiedName=" + applicantName + "&date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&rules=1&assertion=1&points=10").getBytes("UTF-8"));
117 uc.getOutputStream().flush();
118 String data = IOUtils.readURL(uc);
119 assertThat(data, containsString(validVerificationDateString()));
120 assertThat(data, containsString("testcase"));
124 public void testVerifyFormNoCSRF() throws IOException {
126 HttpURLConnection uc = (HttpURLConnection) buildupVerifyFormConnection(false);
127 uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
128 uc.getOutputStream().flush();
129 assertEquals(500, uc.getResponseCode());
133 public void testVerifyFormWrongCSRF() throws IOException {
135 HttpURLConnection uc = (HttpURLConnection) buildupVerifyFormConnection(false);
136 uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10&csrf=aragc").getBytes("UTF-8"));
137 uc.getOutputStream().flush();
138 assertEquals(500, uc.getResponseCode());
142 public void testVerifyFormRaceDoB() throws IOException, SQLException {
143 testVerifyFormRace(false);
147 public void testVerifyFormRaceDoBBlind() throws IOException, SQLException {
148 testVerifyFormRace(true);
151 public void testVerifyFormRace(boolean succeed) throws IOException, SQLException {
152 URLConnection uc = buildupVerifyFormConnection(true);
154 String applicantCookie = login(applicantM, TEST_PASSWORD);
155 String newDob = "day=1&month=1&year=" + ( !succeed ? 1911 : 1910);
157 assertNull(executeBasicWebInteraction(applicantCookie, MyDetails.PATH, newDob + "&action=updateDoB", 0));
159 uc.getOutputStream().write(("verifiedName=" + applicantName + "&date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
160 uc.getOutputStream().flush();
161 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
165 assertTrue(error, !error.startsWith("</div>"));
166 assertThat(error, containsString("changed his personal details"));
171 public void testVerifyFormFuture() throws IOException {
172 SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
173 int year = Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) + 2;
174 executeFails("date=" + year + "-01-01&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
178 public void testVerifyFormFutureOK() throws IOException {
179 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
180 Calendar c = Calendar.getInstance();
181 c.setTimeInMillis(System.currentTimeMillis());
182 c.add(Calendar.HOUR_OF_DAY, 12);
184 executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
188 public void testVerifyFormPastInRange() throws IOException {
189 executeSuccess("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
193 public void testVerifyFormPastOnLimit() throws IOException {
194 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
195 Calendar c = Calendar.getInstance();
196 c.setTimeInMillis(System.currentTimeMillis());
197 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION);
198 c.add(Calendar.DAY_OF_MONTH, 1);
200 executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
204 public void testVerifyFormPastOutOfRange() throws IOException {
205 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
206 Calendar c = Calendar.getInstance();
207 c.setTimeInMillis(System.currentTimeMillis());
208 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION);
210 executeFails("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
214 public void testVerifyFormNoLoc() throws IOException {
215 executeFails("date=" + validVerificationDateString() + "&location=a&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
216 executeFails("date=" + validVerificationDateString() + "&location=&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
220 public void testVerifyFormInvalDate() throws IOException {
221 executeFails("date=20000101&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
222 executeFails("date=&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
226 public void testVerifyFormBoxes() throws IOException {
227 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=0&rules=1&assertion=1&points=10");
228 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=&assertion=1&points=10");
229 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=z&points=10");
233 public void testVerifyListingValid() throws IOException, GigiApiException {
234 String uniqueLoc = createUniqueName();
235 execute("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
237 String cookie = login(applicantM, TEST_PASSWORD);
238 URLConnection url = get(cookie, Points.PATH);
239 String resp = IOUtils.readURL(url);
240 resp = resp.split(Pattern.quote("</table>"))[1];
241 assertThat(resp, containsString(uniqueLoc));
242 assertThat(resp, containsString(Country.getCountryByCode("DE", Country.CountryCodeType.CODE_2_CHARS).getName()));
246 public void testAgentListingValid() throws IOException, GigiApiException {
247 String uniqueLoc = createUniqueName();
248 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
249 String cookie = login(agentM, TEST_PASSWORD);
250 URLConnection url = get(cookie, Points.PATH);
251 String resp = IOUtils.readURL(url);
252 resp = resp.split(Pattern.quote("</table>"))[2];
253 assertThat(resp, containsString(uniqueLoc));
254 assertThat(resp, containsString(Country.getCountryByCode("DE", Country.CountryCodeType.CODE_2_CHARS).getName()));
257 private void executeFails(String query) throws MalformedURLException, IOException {
258 assertThat(execute(query), hasError());
262 private void executeSuccess(String query) throws MalformedURLException, IOException {
263 assertThat(execute(query), hasNoError());
267 private String execute(String query) throws MalformedURLException, IOException {
268 URLConnection uc = buildupVerifyFormConnection(true);
269 uc.getOutputStream().write(("verifiedName=" + applicantName + "&" + query).getBytes("UTF-8"));
270 uc.getOutputStream().flush();
271 return IOUtils.readURL(uc);
274 private URLConnection buildupVerifyFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
275 return buildupVerifyFormConnection(cookie, applicantM, doCSRF);
278 public static URLConnection buildupVerifyFormConnection(String cookie, String email, boolean doCSRF) throws MalformedURLException, IOException {
279 URLConnection uc = get(cookie, VerifyPage.PATH);
280 uc.setDoOutput(true);
281 uc.getOutputStream().write(("email=" + URLEncoder.encode(email, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
283 String csrf = getCSRF(uc);
284 uc = get(cookie, VerifyPage.PATH);
285 uc.setDoOutput(true);
287 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8"));
293 public void testMultipleVerification() throws IOException {
295 User users[] = User.findByEmail(agentM);
296 int agentID = users[0].getId();
298 users = User.findByEmail(applicantM);
299 int applicantID = users[0].getId();
301 // enter first entry 200 days in the past
302 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
303 ps.setInt(1, agentID);
304 ps.setInt(2, applicantID);
306 ps.setString(4, "test-location");
307 ps.setString(5, "2010-01-01");
308 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
312 // enter second entry
313 String uniqueLoc = createUniqueName();
314 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
316 // enter third entry on the same day
317 URLConnection uc = get(cookie, VerifyPage.PATH);
318 uc.setDoOutput(true);
319 uc.getOutputStream().write(("email=" + URLEncoder.encode(applicantM, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
320 assertThat(IOUtils.readURL(uc), hasError());
325 public void testVerifyFormNoCountry() throws IOException {
326 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=&certify=1&rules=1&assertion=1&points=10");
330 public void testRANotificationSet() throws IOException, GigiApiException {
331 getMailReceiver().clearMails();
333 User users[] = User.findByEmail(agentM);
334 assertTrue("user RA Agent not found", users != null && users.length > 0);
337 u.grantGroup(u, Group.VERIFY_NOTIFICATION);
339 cookie = login(agentM, TEST_PASSWORD);
341 String targetMail = u.getEmail();
343 // enter verification
344 String uniqueLoc = createUniqueName();
345 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
349 tm = getMailReceiver().receive();
350 } while ( !tm.getTo().equals(targetMail));
351 assertThat(tm.getMessage(), containsString("You entered a verification for the account with email address " + applicantM));
356 public void testRANotificationNotSet() throws IOException, GigiApiException {
357 getMailReceiver().clearMails();
359 User users[] = User.findByEmail(agentM);
360 assertTrue("user RA Agent not found", users != null && users.length > 0);
363 u.revokeGroup(u, Group.VERIFY_NOTIFICATION);
365 cookie = login(agentM, TEST_PASSWORD);
367 // enter verification
368 String uniqueLoc = createUniqueName();
369 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
373 tm = getMailReceiver().receive();
374 assertThat(tm.getMessage(), not(containsString("You entered a verification for the account with email address " + applicantM)));