]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/wot/TestAssurance.java
add: Allow multiple names, name-schemes, multi-name-assurance, etc.
[gigi.git] / tests / org / cacert / gigi / pages / wot / TestAssurance.java
1 package org.cacert.gigi.pages.wot;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
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;
18
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;
28
29 public class TestAssurance extends ManagedTest {
30
31     private String assurerM;
32
33     private String assureeM;
34
35     private int assureeName;
36
37     private String cookie;
38
39     @Before
40     public void setup() throws IOException {
41         clearCaches();
42         assurerM = createUniqueName() + "@cacert-test.org";
43         assureeM = createUniqueName() + "@cacert-test.org";
44
45         createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
46         int assureeId = createVerifiedUser("a", "c", assureeM, TEST_PASSWORD);
47         assureeName = User.getById(assureeId).getPreferredName().getId();
48
49         cookie = login(assurerM, TEST_PASSWORD);
50     }
51
52     private Matcher<String> isAssuranceForm() {
53         return containsString("<select name=\"assuranceType\">");
54     }
55
56     @Test
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());
60     }
61
62     @Test
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()));
66     }
67
68     @Test
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()));
72     }
73
74     @Test
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()));
82     }
83
84     private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
85         URLConnection uc = get(cookie, AssurePage.PATH);
86         uc.setDoOutput(true);
87         uc.getOutputStream().write(("search&" + query).getBytes("UTF-8"));
88         uc.getOutputStream().flush();
89
90         return IOUtils.readURL(uc);
91     }
92
93     @Test
94     public void testAssureForm() throws IOException {
95         executeSuccess("date=2000-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10");
96     }
97
98     @Test
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"));
106     }
107
108     @Test
109     public void testAssureFormNoCSRF() throws IOException {
110         // override csrf
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());
115     }
116
117     @Test
118     public void testAssureFormWrongCSRF() throws IOException {
119         // override csrf
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());
124     }
125
126     @Test
127     public void testAssureFormRaceDoB() throws IOException, SQLException {
128         testAssureFormRace(false);
129     }
130
131     @Test
132     public void testAssureFormRaceDoBBlind() throws IOException, SQLException {
133         testAssureFormRace(true);
134     }
135
136     public void testAssureFormRace(boolean succeed) throws IOException, SQLException {
137         URLConnection uc = buildupAssureFormConnection(true);
138
139         String assureeCookie = login(assureeM, TEST_PASSWORD);
140         String newDob = "day=1&month=1&year=" + ( !succeed ? 1911 : 1910);
141
142         assertNull(executeBasicWebInteraction(assureeCookie, MyDetails.PATH, newDob + "&action=updateDoB", 0));
143
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));
147         if (succeed) {
148             assertNull(error);
149         } else {
150             assertTrue(error, !error.startsWith("</div>"));
151             assertThat(error, containsString("changed his personal details"));
152         }
153     }
154
155     @Test
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");
160     }
161
162     @Test
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);
168
169         executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&certify=1&rules=1&assertion=1&points=10");
170     }
171
172     @Test
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");
176     }
177
178     @Test
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");
182     }
183
184     @Test
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");
189     }
190
191     @Test
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");
195
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));
201     }
202
203     @Test
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));
212     }
213
214     private void executeFails(String query) throws MalformedURLException, IOException {
215         assertThat(execute(query), hasError());
216
217     }
218
219     private void executeSuccess(String query) throws MalformedURLException, IOException {
220         assertThat(execute(query), hasNoError());
221
222     }
223
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);
229     }
230
231     private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
232         return buildupAssureFormConnection(cookie, assureeM, doCSRF);
233     }
234
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"));
239
240         String csrf = getCSRF(uc);
241         uc = get(cookie, AssurePage.PATH);
242         uc.setDoOutput(true);
243         if (doCSRF) {
244             uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8"));
245         }
246         return uc;
247     }
248
249     @Test
250     public void testMultipleAssurance() throws IOException {
251
252         User users[] = User.findByEmail(assurerM);
253         int agentID = users[0].getId();
254
255         users = User.findByEmail(assureeM);
256         int applicantID = users[0].getId();
257
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);
262             ps.setInt(3, 10);
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));
266             ps.execute();
267         }
268
269         // enter second entry
270         String uniqueLoc = createUniqueName();
271         executeSuccess("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&assertion=1&points=10");
272
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());
278
279     }
280 }