1 package org.cacert.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.cacert.gigi.GigiApiException;
20 import org.cacert.gigi.database.GigiPreparedStatement;
21 import org.cacert.gigi.dbObjects.Country;
22 import org.cacert.gigi.dbObjects.Group;
23 import org.cacert.gigi.dbObjects.User;
24 import org.cacert.gigi.pages.account.MyDetails;
25 import org.cacert.gigi.testUtils.IOUtils;
26 import org.cacert.gigi.testUtils.ManagedTest;
27 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
28 import org.cacert.gigi.util.DayDate;
29 import org.cacert.gigi.util.Notary;
30 import org.hamcrest.Matcher;
31 import org.junit.Before;
32 import org.junit.Test;
34 public class TestAssurance extends ManagedTest {
36 private String assurerM;
38 private String assureeM;
40 private int assureeName;
42 private String cookie;
45 public void setup() throws IOException {
47 assurerM = createUniqueName() + "@cacert-test.org";
48 assureeM = createUniqueName() + "@cacert-test.org";
50 createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
51 int assureeId = createVerifiedUser("a", "c", assureeM, TEST_PASSWORD);
52 assureeName = User.getById(assureeId).getPreferredName().getId();
54 cookie = login(assurerM, TEST_PASSWORD);
57 private Matcher<String> isAssuranceForm() {
58 return containsString("<select name=\"assuranceType\">");
62 public void testAssureSearch() throws IOException {
63 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
64 assertThat(loc, isAssuranceForm());
68 public void testAssureSearchEmail() throws IOException {
69 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
70 assertThat(loc, not(isAssuranceForm()));
74 public void testAssureSearchDobInvalid() throws IOException {
75 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=mal");
76 assertThat(loc, not(isAssuranceForm()));
80 public void testAssureSearchDob() throws IOException {
81 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
82 assertThat(loc, not(isAssuranceForm()));
83 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
84 assertThat(loc, not(isAssuranceForm()));
85 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
86 assertThat(loc, not(isAssuranceForm()));
89 private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
90 URLConnection uc = get(cookie, AssurePage.PATH);
92 uc.getOutputStream().write(("search&" + query).getBytes("UTF-8"));
93 uc.getOutputStream().flush();
95 return IOUtils.readURL(uc);
99 public void testAssureForm() throws IOException {
100 executeSuccess("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
104 public void testAssureFormEmpty() throws IOException {
105 URLConnection uc = buildupAssureFormConnection(true);
106 uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&rules=1&assertion=1&points=10").getBytes("UTF-8"));
107 uc.getOutputStream().flush();
108 String data = IOUtils.readURL(uc);
109 assertThat(data, hasError());
113 public void testAssureFormContanisData() throws IOException {
114 URLConnection uc = buildupAssureFormConnection(true);
115 uc.getOutputStream().write(("assuredName=" + assureeName + "&date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&rules=1&assertion=1&points=10").getBytes("UTF-8"));
116 uc.getOutputStream().flush();
117 String data = IOUtils.readURL(uc);
118 assertThat(data, containsString(validVerificationDateString()));
119 assertThat(data, containsString("testcase"));
123 public void testAssureFormNoCSRF() throws IOException {
125 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
126 uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
127 uc.getOutputStream().flush();
128 assertEquals(500, uc.getResponseCode());
132 public void testAssureFormWrongCSRF() throws IOException {
134 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
135 uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10&csrf=aragc").getBytes("UTF-8"));
136 uc.getOutputStream().flush();
137 assertEquals(500, uc.getResponseCode());
141 public void testAssureFormRaceDoB() throws IOException, SQLException {
142 testAssureFormRace(false);
146 public void testAssureFormRaceDoBBlind() throws IOException, SQLException {
147 testAssureFormRace(true);
150 public void testAssureFormRace(boolean succeed) throws IOException, SQLException {
151 URLConnection uc = buildupAssureFormConnection(true);
153 String assureeCookie = login(assureeM, TEST_PASSWORD);
154 String newDob = "day=1&month=1&year=" + ( !succeed ? 1911 : 1910);
156 assertNull(executeBasicWebInteraction(assureeCookie, MyDetails.PATH, newDob + "&action=updateDoB", 0));
158 uc.getOutputStream().write(("assuredName=" + assureeName + "&date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
159 uc.getOutputStream().flush();
160 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
164 assertTrue(error, !error.startsWith("</div>"));
165 assertThat(error, containsString("changed his personal details"));
170 public void testAssureFormFuture() throws IOException {
171 SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
172 int year = Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) + 2;
173 executeFails("date=" + year + "-01-01&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
177 public void testAssureFormFutureOK() throws IOException {
178 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
179 Calendar c = Calendar.getInstance();
180 c.setTimeInMillis(System.currentTimeMillis());
181 c.add(Calendar.HOUR_OF_DAY, 12);
183 executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
187 public void testAssureFormPastInRange() throws IOException {
188 executeSuccess("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
192 public void testAssureFormPastOnLimit() throws IOException {
193 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
194 Calendar c = Calendar.getInstance();
195 c.setTimeInMillis(System.currentTimeMillis());
196 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION);
197 c.add(Calendar.DAY_OF_MONTH, 1);
199 executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
203 public void testAssureFormPastOutOfRange() throws IOException {
204 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
205 Calendar c = Calendar.getInstance();
206 c.setTimeInMillis(System.currentTimeMillis());
207 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION);
209 executeFails("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
213 public void testAssureFormNoLoc() throws IOException {
214 executeFails("date=" + validVerificationDateString() + "&location=a&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
215 executeFails("date=" + validVerificationDateString() + "&location=&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
219 public void testAssureFormInvalDate() throws IOException {
220 executeFails("date=20000101&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
221 executeFails("date=&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
225 public void testAssureFormBoxes() throws IOException {
226 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=0&rules=1&assertion=1&points=10");
227 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=&assertion=1&points=10");
228 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=z&points=10");
232 public void testAssureListingValid() throws IOException, GigiApiException {
233 String uniqueLoc = createUniqueName();
234 execute("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
236 String cookie = login(assureeM, TEST_PASSWORD);
237 URLConnection url = get(cookie, Points.PATH);
238 String resp = IOUtils.readURL(url);
239 resp = resp.split(Pattern.quote("</table>"))[1];
240 assertThat(resp, containsString(uniqueLoc));
241 assertThat(resp, containsString(Country.getCountryByCode("DE", Country.CountryCodeType.CODE_2_CHARS).getName()));
245 public void testAssurerListingValid() throws IOException, GigiApiException {
246 String uniqueLoc = createUniqueName();
247 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
248 String cookie = login(assurerM, TEST_PASSWORD);
249 URLConnection url = get(cookie, Points.PATH);
250 String resp = IOUtils.readURL(url);
251 resp = resp.split(Pattern.quote("</table>"))[2];
252 assertThat(resp, containsString(uniqueLoc));
253 assertThat(resp, containsString(Country.getCountryByCode("DE", Country.CountryCodeType.CODE_2_CHARS).getName()));
256 private void executeFails(String query) throws MalformedURLException, IOException {
257 assertThat(execute(query), hasError());
261 private void executeSuccess(String query) throws MalformedURLException, IOException {
262 assertThat(execute(query), hasNoError());
266 private String execute(String query) throws MalformedURLException, IOException {
267 URLConnection uc = buildupAssureFormConnection(true);
268 uc.getOutputStream().write(("assuredName=" + assureeName + "&" + query).getBytes("UTF-8"));
269 uc.getOutputStream().flush();
270 return IOUtils.readURL(uc);
273 private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
274 return buildupAssureFormConnection(cookie, assureeM, doCSRF);
277 public static URLConnection buildupAssureFormConnection(String cookie, String email, boolean doCSRF) throws MalformedURLException, IOException {
278 URLConnection uc = get(cookie, AssurePage.PATH);
279 uc.setDoOutput(true);
280 uc.getOutputStream().write(("email=" + URLEncoder.encode(email, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
282 String csrf = getCSRF(uc);
283 uc = get(cookie, AssurePage.PATH);
284 uc.setDoOutput(true);
286 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8"));
292 public void testMultipleAssurance() throws IOException {
294 User users[] = User.findByEmail(assurerM);
295 int agentID = users[0].getId();
297 users = User.findByEmail(assureeM);
298 int applicantID = users[0].getId();
300 // enter first entry 200 days in the past
301 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
302 ps.setInt(1, agentID);
303 ps.setInt(2, applicantID);
305 ps.setString(4, "test-location");
306 ps.setString(5, "2010-01-01");
307 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
311 // enter second entry
312 String uniqueLoc = createUniqueName();
313 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
315 // enter third entry on the same day
316 URLConnection uc = get(cookie, AssurePage.PATH);
317 uc.setDoOutput(true);
318 uc.getOutputStream().write(("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
319 assertThat(IOUtils.readURL(uc), hasError());
324 public void testAssureFormNoCountry() throws IOException {
325 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=&certify=1&rules=1&assertion=1&points=10");
329 public void testRANotificationSet() throws IOException, GigiApiException {
330 getMailReceiver().clearMails();
332 User users[] = User.findByEmail(assurerM);
333 assertTrue("user RA Agent not found", users != null && users.length > 0);
336 u.grantGroup(u, Group.VERIFY_NOTIFICATION);
338 cookie = login(assurerM, TEST_PASSWORD);
340 String targetMail = u.getEmail();
342 // enter verification
343 String uniqueLoc = createUniqueName();
344 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
348 tm = getMailReceiver().receive();
349 } while ( !tm.getTo().equals(targetMail));
350 assertThat(tm.getMessage(), containsString("You entered a verification for the account with email address " + assureeM));
355 public void testRANotificationNotSet() throws IOException, GigiApiException {
356 getMailReceiver().clearMails();
358 User users[] = User.findByEmail(assurerM);
359 assertTrue("user RA Agent not found", users != null && users.length > 0);
362 u.revokeGroup(u, Group.VERIFY_NOTIFICATION);
364 cookie = login(assurerM, TEST_PASSWORD);
366 // enter verification
367 String uniqueLoc = createUniqueName();
368 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
372 tm = getMailReceiver().receive();
373 assertThat(tm.getMessage(), not(containsString("You entered a verification for the account with email address " + assureeM)));