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 String cookie;
38 public void setup() throws IOException {
40 assurerM = createUniqueName() + "@cacert-test.org";
41 assureeM = createUniqueName() + "@cacert-test.org";
43 createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
44 createVerifiedUser("a", "c", assureeM, TEST_PASSWORD);
46 cookie = login(assurerM, TEST_PASSWORD);
49 private Matcher<String> isAssuranceForm() {
50 return containsString("<select name=\"assuranceType\">");
54 public void testAssureSearch() throws IOException {
55 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
56 assertThat(loc, isAssuranceForm());
60 public void testAssureSearchEmail() throws IOException {
61 String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
62 assertThat(loc, not(isAssuranceForm()));
66 public void testAssureSearchDobInvalid() throws IOException {
67 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=mal");
68 assertThat(loc, not(isAssuranceForm()));
72 public void testAssureSearchDob() throws IOException {
73 String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
74 assertThat(loc, not(isAssuranceForm()));
75 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
76 assertThat(loc, not(isAssuranceForm()));
77 loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
78 assertThat(loc, not(isAssuranceForm()));
81 private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
82 URLConnection uc = get(cookie, AssurePage.PATH);
84 uc.getOutputStream().write(("search&" + query).getBytes("UTF-8"));
85 uc.getOutputStream().flush();
87 return IOUtils.readURL(uc);
91 public void testAssureForm() throws IOException {
92 executeSuccess("date=2000-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10");
96 public void testAssureFormContanisData() throws IOException {
97 URLConnection uc = buildupAssureFormConnection(true);
98 uc.getOutputStream().write(("date=2000-01-01&location=testcase&rules=1&assertion=1&points=10").getBytes("UTF-8"));
99 uc.getOutputStream().flush();
100 String data = IOUtils.readURL(uc);
101 assertThat(data, containsString("2000-01-01"));
102 assertThat(data, containsString("testcase"));
106 public void testAssureFormNoCSRF() throws IOException {
108 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
109 uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
110 uc.getOutputStream().flush();
111 assertEquals(500, uc.getResponseCode());
115 public void testAssureFormWrongCSRF() throws IOException {
117 HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
118 uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10&csrf=aragc").getBytes("UTF-8"));
119 uc.getOutputStream().flush();
120 assertEquals(500, uc.getResponseCode());
124 public void testAssureFormRaceName() throws IOException, SQLException {
125 testAssureFormRace(true, false);
129 public void testAssureFormRaceDoB() throws IOException, SQLException {
130 testAssureFormRace(false, false);
134 public void testAssureFormRaceNameBlind() throws IOException, SQLException {
135 testAssureFormRace(true, true);
139 public void testAssureFormRaceDoBBlind() throws IOException, SQLException {
140 testAssureFormRace(false, true);
143 public void testAssureFormRace(boolean name, boolean succeed) throws IOException, SQLException {
144 URLConnection uc = buildupAssureFormConnection(true);
146 String assureeCookie = login(assureeM, TEST_PASSWORD);
147 String newName = "lname=" + (name && !succeed ? "a" : "c") + "&fname=a&mname=&suffix=";
148 String newDob = "day=1&month=1&year=" + ( !name && !succeed ? 1911 : 1910);
150 assertNull(executeBasicWebInteraction(assureeCookie, MyDetails.PATH, newName + "&" + newDob + "&processDetails", 0));
152 uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10").getBytes("UTF-8"));
153 uc.getOutputStream().flush();
154 String error = fetchStartErrorMessage(IOUtils.readURL(uc));
158 assertTrue(error, !error.startsWith("</div>"));
159 assertThat(error, containsString("changed his personal details"));
164 public void testAssureFormFuture() throws IOException {
165 SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
166 int year = Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) + 2;
167 executeFails("date=" + year + "-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10");
171 public void testAssureFormFutureOK() throws IOException {
172 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
173 Calendar c = Calendar.getInstance();
174 c.setTimeInMillis(System.currentTimeMillis());
175 c.add(Calendar.HOUR_OF_DAY, 12);
177 executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&certify=1&rules=1&assertion=1&points=10");
181 public void testAssureFormNoLoc() throws IOException {
182 executeFails("date=2000-01-01&location=a&certify=1&rules=1&assertion=1&points=10");
183 executeFails("date=2000-01-01&location=&certify=1&rules=1&assertion=1&points=10");
187 public void testAssureFormInvalDate() throws IOException {
188 executeFails("date=20000101&location=testcase&certify=1&rules=1&assertion=1&points=10");
189 executeFails("date=&location=testcase&certify=1&rules=1&assertion=1&points=10");
193 public void testAssureFormBoxes() throws IOException {
194 executeFails("date=2000-01-01&location=testcase&certify=0&rules=1&assertion=1&points=10");
195 executeFails("date=2000-01-01&location=testcase&certify=1&rules=&assertion=1&points=10");
196 executeFails("date=2000-01-01&location=testcase&certify=1&rules=1&assertion=z&points=10");
200 public void testAssureListingValid() throws IOException {
201 String uniqueLoc = createUniqueName();
202 execute("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&assertion=1&points=10");
204 String cookie = login(assureeM, TEST_PASSWORD);
205 URLConnection url = get(cookie, MyPoints.PATH);
206 String resp = IOUtils.readURL(url);
207 resp = resp.split(Pattern.quote("</table>"))[0];
208 assertThat(resp, containsString(uniqueLoc));
212 public void testAssurerListingValid() throws IOException {
213 String uniqueLoc = createUniqueName();
214 executeSuccess("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&assertion=1&points=10");
215 String cookie = login(assurerM, TEST_PASSWORD);
216 URLConnection url = get(cookie, MyPoints.PATH);
217 String resp = IOUtils.readURL(url);
218 resp = resp.split(Pattern.quote("</table>"))[1];
219 assertThat(resp, containsString(uniqueLoc));
222 private void executeFails(String query) throws MalformedURLException, IOException {
223 assertThat(execute(query), hasError());
227 private void executeSuccess(String query) throws MalformedURLException, IOException {
228 assertThat(execute(query), hasNoError());
232 private String execute(String query) throws MalformedURLException, IOException {
233 URLConnection uc = buildupAssureFormConnection(true);
234 uc.getOutputStream().write((query).getBytes("UTF-8"));
235 uc.getOutputStream().flush();
236 return IOUtils.readURL(uc);
239 private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
240 return buildupAssureFormConnection(cookie, assureeM, doCSRF);
243 public static URLConnection buildupAssureFormConnection(String cookie, String email, boolean doCSRF) throws MalformedURLException, IOException {
244 URLConnection uc = get(cookie, AssurePage.PATH);
245 uc.setDoOutput(true);
246 uc.getOutputStream().write(("email=" + URLEncoder.encode(email, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
248 String csrf = getCSRF(uc);
249 uc = get(cookie, AssurePage.PATH);
250 uc.setDoOutput(true);
252 uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8"));
258 public void testMultipleAssurance() throws IOException {
260 User users[] = User.findByEmail(assurerM);
261 int agentID = users[0].getId();
263 users = User.findByEmail(assureeM);
264 int applicantID = users[0].getId();
266 // enter first entry 200 days in the past
267 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
268 ps.setInt(1, agentID);
269 ps.setInt(2, applicantID);
271 ps.setString(4, "test-location");
272 ps.setString(5, "2010-01-01");
273 ps.setTimestamp(6, new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 200));
277 // enter second entry
278 String uniqueLoc = createUniqueName();
279 executeSuccess("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&assertion=1&points=10");
281 // enter third entry on the same day
282 URLConnection uc = get(cookie, AssurePage.PATH);
283 uc.setDoOutput(true);
284 uc.getOutputStream().write(("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
285 assertThat(IOUtils.readURL(uc), hasError());