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.database.GigiPreparedStatement;
20 import org.cacert.gigi.dbObjects.User;
21 import org.cacert.gigi.pages.account.MyDetails;
22 import org.cacert.gigi.testUtils.IOUtils;
23 import org.cacert.gigi.testUtils.ManagedTest;
24 import org.cacert.gigi.util.DayDate;
25 import org.hamcrest.Matcher;
26 import org.junit.Before;
27 import org.junit.Test;
29 public class TestAssurance extends ManagedTest {
31 private String assurerM;
33 private String assureeM;
35 private int assureeName;
37 private String cookie;
40 public void setup() throws IOException {
42 assurerM = createUniqueName() + "@cacert-test.org";
43 assureeM = createUniqueName() + "@cacert-test.org";
45 createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
46 int assureeId = createVerifiedUser("a", "c", assureeM, TEST_PASSWORD);
47 assureeName = User.getById(assureeId).getPreferredName().getId();
49 cookie = login(assurerM, TEST_PASSWORD);
52 private Matcher<String> isAssuranceForm() {
53 return containsString("<select name=\"assuranceType\">");
57 public void testAssureSearch() throws IOException {
58 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
59 assertThat(loc, isAssuranceForm());
63 public void testAssureSearchEmail() throws IOException {
64 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
65 assertThat(loc, not(isAssuranceForm()));
69 public void testAssureSearchDobInvalid() throws IOException {
70 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=mal");
71 assertThat(loc, not(isAssuranceForm()));
75 public void testAssureSearchDob() throws IOException {
76 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
77 assertThat(loc, not(isAssuranceForm()));
78 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
79 assertThat(loc, not(isAssuranceForm()));
80 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
81 assertThat(loc, not(isAssuranceForm()));
84 private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
85 URLConnection uc = get(cookie, AssurePage.PATH);
87 uc.getOutputStream().write(("search&" + query).getBytes("UTF-8"));
88 uc.getOutputStream().flush();
90 return IOUtils.readURL(uc);
94 public void testAssureForm() throws IOException {
95 executeSuccess("date=2000-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10");
99 public void testAssureFormContanisData() throws IOException {
100 URLConnection uc = buildupAssureFormConnection(true);
101 uc.getOutputStream().write(("assuredName=" + assureeName + "&date=2000-01-01&location=testcase&rules=1&assertion=1&points=10").getBytes("UTF-8"));
102 uc.getOutputStream().flush();
103 String data = IOUtils.readURL(uc);
104 assertThat(data, containsString("2000-01-01"));
105 assertThat(data, containsString("testcase"));
109 public void testAssureFormNoCSRF() throws IOException {
111 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
112 uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
113 uc.getOutputStream().flush();
114 assertEquals(500, uc.getResponseCode());
118 public void testAssureFormWrongCSRF() throws IOException {
120 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
121 uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10&csrf=aragc").getBytes("UTF-8"));
122 uc.getOutputStream().flush();
123 assertEquals(500, uc.getResponseCode());
127 public void testAssureFormRaceDoB() throws IOException, SQLException {
128 testAssureFormRace(false);
132 public void testAssureFormRaceDoBBlind() throws IOException, SQLException {
133 testAssureFormRace(true);
136 public void testAssureFormRace(boolean succeed) throws IOException, SQLException {
137 URLConnection uc = buildupAssureFormConnection(true);
139 String assureeCookie = login(assureeM, TEST_PASSWORD);
140 String newDob = "day=1&month=1&year=" + ( !succeed ? 1911 : 1910);
142 assertNull(executeBasicWebInteraction(assureeCookie, MyDetails.PATH, newDob + "&action=updateDoB", 0));
144 uc.getOutputStream().write(("assuredName=" + assureeName + "&date=2000-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
145 uc.getOutputStream().flush();
146 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
150 assertTrue(error, !error.startsWith("</div>"));
151 assertThat(error, containsString("changed his personal details"));
156 public void testAssureFormFuture() throws IOException {
157 SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
158 int year = Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) + 2;
159 executeFails("date=" + year + "-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10");
163 public void testAssureFormFutureOK() throws IOException {
164 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
165 Calendar c = Calendar.getInstance();
166 c.setTimeInMillis(System.currentTimeMillis());
167 c.add(Calendar.HOUR_OF_DAY, 12);
169 executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&certify=1&rules=1&assertion=1&points=10");
173 public void testAssureFormNoLoc() throws IOException {
174 executeFails("date=2000-01-01&location=a&certify=1&rules=1&assertion=1&points=10");
175 executeFails("date=2000-01-01&location=&certify=1&rules=1&assertion=1&points=10");
179 public void testAssureFormInvalDate() throws IOException {
180 executeFails("date=20000101&location=testcase&certify=1&rules=1&assertion=1&points=10");
181 executeFails("date=&location=testcase&certify=1&rules=1&assertion=1&points=10");
185 public void testAssureFormBoxes() throws IOException {
186 executeFails("date=2000-01-01&location=testcase&certify=0&rules=1&assertion=1&points=10");
187 executeFails("date=2000-01-01&location=testcase&certify=1&rules=&assertion=1&points=10");
188 executeFails("date=2000-01-01&location=testcase&certify=1&rules=1&assertion=z&points=10");
192 public void testAssureListingValid() throws IOException {
193 String uniqueLoc = createUniqueName();
194 execute("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&assertion=1&points=10");
196 String cookie = login(assureeM, TEST_PASSWORD);
197 URLConnection url = get(cookie, MyPoints.PATH);
198 String resp = IOUtils.readURL(url);
199 resp = resp.split(Pattern.quote("</table>"))[0];
200 assertThat(resp, containsString(uniqueLoc));
204 public void testAssurerListingValid() throws IOException {
205 String uniqueLoc = createUniqueName();
206 executeSuccess("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&assertion=1&points=10");
207 String cookie = login(assurerM, TEST_PASSWORD);
208 URLConnection url = get(cookie, MyPoints.PATH);
209 String resp = IOUtils.readURL(url);
210 resp = resp.split(Pattern.quote("</table>"))[1];
211 assertThat(resp, containsString(uniqueLoc));
214 private void executeFails(String query) throws MalformedURLException, IOException {
215 assertThat(execute(query), hasError());
219 private void executeSuccess(String query) throws MalformedURLException, IOException {
220 assertThat(execute(query), hasNoError());
224 private String execute(String query) throws MalformedURLException, IOException {
225 URLConnection uc = buildupAssureFormConnection(true);
226 uc.getOutputStream().write(("assuredName=" + assureeName + "&" + query).getBytes("UTF-8"));
227 uc.getOutputStream().flush();
228 return IOUtils.readURL(uc);
231 private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
232 return buildupAssureFormConnection(cookie, assureeM, doCSRF);
235 public static URLConnection buildupAssureFormConnection(String cookie, String email, boolean doCSRF) throws MalformedURLException, IOException {
236 URLConnection uc = get(cookie, AssurePage.PATH);
237 uc.setDoOutput(true);
238 uc.getOutputStream().write(("email=" + URLEncoder.encode(email, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
240 String csrf = getCSRF(uc);
241 uc = get(cookie, AssurePage.PATH);
242 uc.setDoOutput(true);
244 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8"));
250 public void testMultipleAssurance() throws IOException {
252 User users[] = User.findByEmail(assurerM);
253 int agentID = users[0].getId();
255 users = User.findByEmail(assureeM);
256 int applicantID = users[0].getId();
258 // enter first entry 200 days in the past
259 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
260 ps.setInt(1, agentID);
261 ps.setInt(2, applicantID);
263 ps.setString(4, "test-location");
264 ps.setString(5, "2010-01-01");
265 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
269 // enter second entry
270 String uniqueLoc = createUniqueName();
271 executeSuccess("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&assertion=1&points=10");
273 // enter third entry on the same day
274 URLConnection uc = get(cookie, AssurePage.PATH);
275 uc.setDoOutput(true);
276 uc.getOutputStream().write(("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
277 assertThat(IOUtils.readURL(uc), hasError());