]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/wot/TestAssurance.java
add: handling of multiple verifications by the same RA Agent
[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 String cookie;
36
37     @Before
38     public void setup() throws IOException {
39         clearCaches();
40         assurerM = createUniqueName() + "@cacert-test.org";
41         assureeM = createUniqueName() + "@cacert-test.org";
42
43         createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
44         createVerifiedUser("a", "c", assureeM, TEST_PASSWORD);
45
46         cookie = login(assurerM, TEST_PASSWORD);
47     }
48
49     private Matcher<String> isAssuranceForm() {
50         return containsString("<select name=\"assuranceType\">");
51     }
52
53     @Test
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());
57     }
58
59     @Test
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()));
63     }
64
65     @Test
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()));
69     }
70
71     @Test
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()));
79     }
80
81     private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
82         URLConnection uc = get(cookie, AssurePage.PATH);
83         uc.setDoOutput(true);
84         uc.getOutputStream().write(("search&" + query).getBytes("UTF-8"));
85         uc.getOutputStream().flush();
86
87         return IOUtils.readURL(uc);
88     }
89
90     @Test
91     public void testAssureForm() throws IOException {
92         executeSuccess("date=2000-01-01&location=testcase&certify=1&rules=1&assertion=1&points=10");
93     }
94
95     @Test
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"));
103     }
104
105     @Test
106     public void testAssureFormNoCSRF() throws IOException {
107         // override csrf
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());
112     }
113
114     @Test
115     public void testAssureFormWrongCSRF() throws IOException {
116         // override csrf
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());
121     }
122
123     @Test
124     public void testAssureFormRaceName() throws IOException, SQLException {
125         testAssureFormRace(true, false);
126     }
127
128     @Test
129     public void testAssureFormRaceDoB() throws IOException, SQLException {
130         testAssureFormRace(false, false);
131     }
132
133     @Test
134     public void testAssureFormRaceNameBlind() throws IOException, SQLException {
135         testAssureFormRace(true, true);
136     }
137
138     @Test
139     public void testAssureFormRaceDoBBlind() throws IOException, SQLException {
140         testAssureFormRace(false, true);
141     }
142
143     public void testAssureFormRace(boolean name, boolean succeed) throws IOException, SQLException {
144         URLConnection uc = buildupAssureFormConnection(true);
145
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);
149
150         assertNull(executeBasicWebInteraction(assureeCookie, MyDetails.PATH, newName + "&" + newDob + "&processDetails", 0));
151
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));
155         if (succeed) {
156             assertNull(error);
157         } else {
158             assertTrue(error, !error.startsWith("</div>"));
159             assertThat(error, containsString("changed his personal details"));
160         }
161     }
162
163     @Test
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");
168     }
169
170     @Test
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);
176
177         executeSuccess("date=" + sdf.format(new Date(c.getTimeInMillis())) + "&location=testcase&certify=1&rules=1&assertion=1&points=10");
178     }
179
180     @Test
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");
184     }
185
186     @Test
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");
190     }
191
192     @Test
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");
197     }
198
199     @Test
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");
203
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));
209     }
210
211     @Test
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));
220     }
221
222     private void executeFails(String query) throws MalformedURLException, IOException {
223         assertThat(execute(query), hasError());
224
225     }
226
227     private void executeSuccess(String query) throws MalformedURLException, IOException {
228         assertThat(execute(query), hasNoError());
229
230     }
231
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);
237     }
238
239     private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
240         return buildupAssureFormConnection(cookie, assureeM, doCSRF);
241     }
242
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"));
247
248         String csrf = getCSRF(uc);
249         uc = get(cookie, AssurePage.PATH);
250         uc.setDoOutput(true);
251         if (doCSRF) {
252             uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8"));
253         }
254         return uc;
255     }
256
257     @Test
258     public void testMultipleAssurance() throws IOException {
259
260         User users[] = User.findByEmail(assurerM);
261         int agentID = users[0].getId();
262
263         users = User.findByEmail(assureeM);
264         int applicantID = users[0].getId();
265
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);
270             ps.setInt(3, 10);
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));
274             ps.execute();
275         }
276
277         // enter second entry
278         String uniqueLoc = createUniqueName();
279         executeSuccess("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&assertion=1&points=10");
280
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());
286
287     }
288 }