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.User;
23 import org.cacert.gigi.pages.account.MyDetails;
24 import org.cacert.gigi.testUtils.IOUtils;
25 import org.cacert.gigi.testUtils.ManagedTest;
26 import org.cacert.gigi.util.DayDate;
27 import org.cacert.gigi.util.Notary;
28 import org.hamcrest.Matcher;
29 import org.junit.Before;
30 import org.junit.Test;
32 public class TestAssurance extends ManagedTest {
34 private String assurerM;
36 private String assureeM;
38 private int assureeName;
40 private String cookie;
43 public void setup() throws IOException {
45 assurerM = createUniqueName() + "@cacert-test.org";
46 assureeM = createUniqueName() + "@cacert-test.org";
48 createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
49 int assureeId = createVerifiedUser("a", "c", assureeM, TEST_PASSWORD);
50 assureeName = User.getById(assureeId).getPreferredName().getId();
52 cookie = login(assurerM, TEST_PASSWORD);
55 private Matcher<String> isAssuranceForm() {
56 return containsString("<select name=\"assuranceType\">");
60 public void testAssureSearch() throws IOException {
61 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
62 assertThat(loc, isAssuranceForm());
66 public void testAssureSearchEmail() throws IOException {
67 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
68 assertThat(loc, not(isAssuranceForm()));
72 public void testAssureSearchDobInvalid() throws IOException {
73 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=mal");
74 assertThat(loc, not(isAssuranceForm()));
78 public void testAssureSearchDob() throws IOException {
79 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
80 assertThat(loc, not(isAssuranceForm()));
81 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
82 assertThat(loc, not(isAssuranceForm()));
83 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
84 assertThat(loc, not(isAssuranceForm()));
87 private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
88 URLConnection uc = get(cookie, AssurePage.PATH);
90 uc.getOutputStream().write(("search&" + query).getBytes("UTF-8"));
91 uc.getOutputStream().flush();
93 return IOUtils.readURL(uc);
97 public void testAssureForm() throws IOException {
98 executeSuccess("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
102 public void testAssureFormEmpty() throws IOException {
103 URLConnection uc = buildupAssureFormConnection(true);
104 uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&rules=1&assertion=1&points=10").getBytes("UTF-8"));
105 uc.getOutputStream().flush();
106 String data = IOUtils.readURL(uc);
107 assertThat(data, hasError());
111 public void testAssureFormContanisData() throws IOException {
112 URLConnection uc = buildupAssureFormConnection(true);
113 uc.getOutputStream().write(("assuredName=" + assureeName + "&date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&rules=1&assertion=1&points=10").getBytes("UTF-8"));
114 uc.getOutputStream().flush();
115 String data = IOUtils.readURL(uc);
116 assertThat(data, containsString(validVerificationDateString()));
117 assertThat(data, containsString("testcase"));
121 public void testAssureFormNoCSRF() throws IOException {
123 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
124 uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
125 uc.getOutputStream().flush();
126 assertEquals(500, uc.getResponseCode());
130 public void testAssureFormWrongCSRF() throws IOException {
132 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
133 uc.getOutputStream().write(("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10&csrf=aragc").getBytes("UTF-8"));
134 uc.getOutputStream().flush();
135 assertEquals(500, uc.getResponseCode());
139 public void testAssureFormRaceDoB() throws IOException, SQLException {
140 testAssureFormRace(false);
144 public void testAssureFormRaceDoBBlind() throws IOException, SQLException {
145 testAssureFormRace(true);
148 public void testAssureFormRace(boolean succeed) throws IOException, SQLException {
149 URLConnection uc = buildupAssureFormConnection(true);
151 String assureeCookie = login(assureeM, TEST_PASSWORD);
152 String newDob = "day=1&month=1&year=" + ( !succeed ? 1911 : 1910);
154 assertNull(executeBasicWebInteraction(assureeCookie, MyDetails.PATH, newDob + "&action=updateDoB", 0));
156 uc.getOutputStream().write(("assuredName=" + assureeName + "&date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
157 uc.getOutputStream().flush();
158 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
162 assertTrue(error, !error.startsWith("</div>"));
163 assertThat(error, containsString("changed his personal details"));
168 public void testAssureFormFuture() throws IOException {
169 SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
170 int year = Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) + 2;
171 executeFails("date=" + year + "-01-01&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
175 public void testAssureFormFutureOK() throws IOException {
176 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
177 Calendar c = Calendar.getInstance();
178 c.setTimeInMillis(System.currentTimeMillis());
179 c.add(Calendar.HOUR_OF_DAY, 12);
181 executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
185 public void testAssureFormPastInRange() throws IOException {
186 executeSuccess("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
190 public void testAssureFormPastOnLimit() throws IOException {
191 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
192 Calendar c = Calendar.getInstance();
193 c.setTimeInMillis(System.currentTimeMillis());
194 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION);
195 c.add(Calendar.DAY_OF_MONTH, 1);
197 executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
201 public void testAssureFormPastOutOfRange() throws IOException {
202 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
203 Calendar c = Calendar.getInstance();
204 c.setTimeInMillis(System.currentTimeMillis());
205 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION);
207 executeFails("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
211 public void testAssureFormNoLoc() throws IOException {
212 executeFails("date=" + validVerificationDateString() + "&location=a&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
213 executeFails("date=" + validVerificationDateString() + "&location=&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
217 public void testAssureFormInvalDate() throws IOException {
218 executeFails("date=20000101&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
219 executeFails("date=&location=testcase&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
223 public void testAssureFormBoxes() throws IOException {
224 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=0&rules=1&assertion=1&points=10");
225 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=&assertion=1&points=10");
226 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=DE&certify=1&rules=1&assertion=z&points=10");
230 public void testAssureListingValid() throws IOException, GigiApiException {
231 String uniqueLoc = createUniqueName();
232 execute("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
234 String cookie = login(assureeM, TEST_PASSWORD);
235 URLConnection url = get(cookie, Points.PATH);
236 String resp = IOUtils.readURL(url);
237 resp = resp.split(Pattern.quote("</table>"))[1];
238 assertThat(resp, containsString(uniqueLoc));
239 assertThat(resp, containsString(Country.getCountryByCode("DE", Country.CountryCodeType.CODE_2_CHARS).getName()));
243 public void testAssurerListingValid() throws IOException, GigiApiException {
244 String uniqueLoc = createUniqueName();
245 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
246 String cookie = login(assurerM, TEST_PASSWORD);
247 URLConnection url = get(cookie, Points.PATH);
248 String resp = IOUtils.readURL(url);
249 resp = resp.split(Pattern.quote("</table>"))[2];
250 assertThat(resp, containsString(uniqueLoc));
251 assertThat(resp, containsString(Country.getCountryByCode("DE", Country.CountryCodeType.CODE_2_CHARS).getName()));
254 private void executeFails(String query) throws MalformedURLException, IOException {
255 assertThat(execute(query), hasError());
259 private void executeSuccess(String query) throws MalformedURLException, IOException {
260 assertThat(execute(query), hasNoError());
264 private String execute(String query) throws MalformedURLException, IOException {
265 URLConnection uc = buildupAssureFormConnection(true);
266 uc.getOutputStream().write(("assuredName=" + assureeName + "&" + query).getBytes("UTF-8"));
267 uc.getOutputStream().flush();
268 return IOUtils.readURL(uc);
271 private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
272 return buildupAssureFormConnection(cookie, assureeM, doCSRF);
275 public static URLConnection buildupAssureFormConnection(String cookie, String email, boolean doCSRF) throws MalformedURLException, IOException {
276 URLConnection uc = get(cookie, AssurePage.PATH);
277 uc.setDoOutput(true);
278 uc.getOutputStream().write(("email=" + URLEncoder.encode(email, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
280 String csrf = getCSRF(uc);
281 uc = get(cookie, AssurePage.PATH);
282 uc.setDoOutput(true);
284 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8"));
290 public void testMultipleAssurance() throws IOException {
292 User users[] = User.findByEmail(assurerM);
293 int agentID = users[0].getId();
295 users = User.findByEmail(assureeM);
296 int applicantID = users[0].getId();
298 // enter first entry 200 days in the past
299 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
300 ps.setInt(1, agentID);
301 ps.setInt(2, applicantID);
303 ps.setString(4, "test-location");
304 ps.setString(5, "2010-01-01");
305 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
309 // enter second entry
310 String uniqueLoc = createUniqueName();
311 executeSuccess("date=" + validVerificationDateString() + "&location=" + uniqueLoc + "&countryCode=DE&certify=1&rules=1&assertion=1&points=10");
313 // enter third entry on the same day
314 URLConnection uc = get(cookie, AssurePage.PATH);
315 uc.setDoOutput(true);
316 uc.getOutputStream().write(("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
317 assertThat(IOUtils.readURL(uc), hasError());
322 public void testAssureFormNoCountry() throws IOException {
323 executeFails("date=" + validVerificationDateString() + "&location=testcase&countryCode=&certify=1&rules=1&assertion=1&points=10");