]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/wot/TestAssurance.java
fix: assurance form, keep location and date + testCase
[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.URL;
11 import java.net.URLConnection;
12 import java.net.URLEncoder;
13 import java.sql.SQLException;
14 import java.text.SimpleDateFormat;
15 import java.util.Date;
16 import java.util.regex.Pattern;
17
18 import org.cacert.gigi.pages.account.MyDetails;
19 import org.cacert.gigi.testUtils.IOUtils;
20 import org.cacert.gigi.testUtils.ManagedTest;
21 import org.junit.Before;
22 import org.junit.Test;
23
24 public class TestAssurance extends ManagedTest {
25
26     private String assurerM;
27
28     private String assureeM;
29
30     private String cookie;
31
32     @Before
33     public void setup() throws IOException {
34         assurerM = createUniqueName() + "@cacert-test.org";
35         assureeM = createUniqueName() + "@cacert-test.org";
36
37         createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
38         createVerifiedUser("a", "c", assureeM, TEST_PASSWORD);
39
40         cookie = login(assurerM, TEST_PASSWORD);
41     }
42
43     @Test
44     public void testAssureSearch() throws IOException {
45         String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
46         assertTrue(loc, loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
47     }
48
49     @Test
50     public void testAssureSearchEmail() throws IOException {
51         String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
52         assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
53     }
54
55     @Test
56     public void testAssureSearchDob() throws IOException {
57         String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
58         assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
59         loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
60         assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
61         loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
62         assertTrue(loc, !loc.contains("type=\"checkbox\" name=\"CCAAgreed\""));
63     }
64
65     private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
66         URL u = new URL("https://" + getServerName() + AssurePage.PATH);
67         URLConnection uc = u.openConnection();
68         uc.setDoOutput(true);
69         uc.addRequestProperty("Cookie", cookie);
70         uc.getOutputStream().write(("search&" + query).getBytes("UTF-8"));
71         uc.getOutputStream().flush();
72
73         return IOUtils.readURL(uc);
74     }
75
76     @Test
77     public void testAssureForm() throws IOException {
78         String error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
79         assertNull(error);
80     }
81
82     @Test
83     public void testAssureFormContanisData() throws IOException {
84         URLConnection uc = buildupAssureFormConnection(true);
85         uc.getOutputStream().write(("date=2000-01-01&location=testcase&rules=1&CCAAgreed=1&assertion=1&points=10").getBytes("UTF-8"));
86         uc.getOutputStream().flush();
87         String data = IOUtils.readURL(uc);
88         assertThat(data, containsString("2000-01-01"));
89         assertThat(data, containsString("testcase"));
90     }
91
92     @Test
93     public void testAssureFormNoCSRF() throws IOException {
94         // override csrf
95         HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
96         uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10").getBytes("UTF-8"));
97         uc.getOutputStream().flush();
98         assertEquals(500, uc.getResponseCode());
99     }
100
101     @Test
102     public void testAssureFormWrongCSRF() throws IOException {
103         // override csrf
104         HttpURLConnection uc = (HttpURLConnection) buildupAssureFormConnection(false);
105         uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10&csrf=aragc").getBytes("UTF-8"));
106         uc.getOutputStream().flush();
107         assertEquals(500, uc.getResponseCode());
108     }
109
110     @Test
111     public void testAssureFormRaceName() throws IOException, SQLException {
112         testAssureFormRace(true);
113     }
114
115     @Test
116     public void testAssureFormRaceDoB() throws IOException, SQLException {
117         testAssureFormRace(false);
118     }
119
120     public void testAssureFormRace(boolean name) throws IOException, SQLException {
121         URLConnection uc = buildupAssureFormConnection(true);
122
123         String assureeCookie = login(assureeM, TEST_PASSWORD);
124         String newName = "lname=" + (name ? "c" : "a") + "&fname=a&mname=&suffix=";
125         String newDob = "day=1&month=1&year=" + (name ? 1910 : 1911);
126
127         assertNull(executeBasicWebInteraction(assureeCookie, MyDetails.PATH, newName + "&" + newDob + "&processDetails", 0));
128
129         uc.getOutputStream().write(("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10").getBytes("UTF-8"));
130         uc.getOutputStream().flush();
131         String error = fetchStartErrorMessage(IOUtils.readURL(uc));
132         assertTrue(error, !error.startsWith("</div>"));
133     }
134
135     @Test
136     public void testAssureFormFuture() throws IOException {
137         SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
138         int year = Integer.parseInt(sdf.format(new Date(System.currentTimeMillis()))) + 2;
139         String error = getError("date=" + year + "-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
140         assertTrue(error, !error.startsWith("</div>"));
141     }
142
143     @Test
144     public void testAssureFormNoLoc() throws IOException {
145         String error = getError("date=2000-01-01&location=a&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
146         assertTrue(error, !error.startsWith("</div>"));
147         error = getError("date=2000-01-01&location=&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
148         assertTrue(error, !error.startsWith("</div>"));
149     }
150
151     @Test
152     public void testAssureFormInvalDate() throws IOException {
153         String error = getError("date=20000101&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
154         assertTrue(error, !error.startsWith("</div>"));
155         error = getError("date=&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
156         assertTrue(error, !error.startsWith("</div>"));
157     }
158
159     @Test
160     public void testAssureFormBoxes() throws IOException {
161         String error = getError("date=2000-01-01&location=testcase&certify=0&rules=1&CCAAgreed=1&assertion=1&points=10");
162         assertTrue(error, !error.startsWith("</div>"));
163         error = getError("date=2000-01-01&location=testcase&certify=1&rules=&CCAAgreed=1&assertion=1&points=10");
164         assertTrue(error, !error.startsWith("</div>"));
165         error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=a&assertion=1&points=10");
166         assertTrue(error, !error.startsWith("</div>"));
167         error = getError("date=2000-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=z&points=10");
168         assertTrue(error, !error.startsWith("</div>"));
169     }
170
171     @Test
172     public void testAssureListingValid() throws IOException {
173         String uniqueLoc = createUniqueName();
174         String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
175         assertNull(error);
176         String cookie = login(assureeM, TEST_PASSWORD);
177         URLConnection url = new URL("https://" + getServerName() + MyPoints.PATH).openConnection();
178         url.setRequestProperty("Cookie", cookie);
179         String resp = IOUtils.readURL(url);
180         resp = resp.split(Pattern.quote("</table>"))[0];
181         assertThat(resp, containsString(uniqueLoc));
182     }
183
184     @Test
185     public void testAssurerListingValid() throws IOException {
186         String uniqueLoc = createUniqueName();
187         String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
188         assertNull(error);
189         String cookie = login(assurerM, TEST_PASSWORD);
190         URLConnection url = new URL("https://" + getServerName() + MyPoints.PATH).openConnection();
191         url.setRequestProperty("Cookie", cookie);
192         String resp = IOUtils.readURL(url);
193         resp = resp.split(Pattern.quote("</table>"))[1];
194         assertThat(resp, containsString(uniqueLoc));
195     }
196
197     private String getError(String query) throws MalformedURLException, IOException {
198         URLConnection uc = buildupAssureFormConnection(true);
199         uc.getOutputStream().write((query).getBytes("UTF-8"));
200         uc.getOutputStream().flush();
201         String error = fetchStartErrorMessage(IOUtils.readURL(uc));
202         return error;
203     }
204
205     private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
206         URL u = new URL("https://" + getServerName() + AssurePage.PATH);
207         URLConnection uc = u.openConnection();
208         uc.addRequestProperty("Cookie", cookie);
209         uc.setDoOutput(true);
210         uc.getOutputStream().write(("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
211
212         String csrf = getCSRF(uc);
213         uc = u.openConnection();
214         uc.addRequestProperty("Cookie", cookie);
215         uc.setDoOutput(true);
216         if (doCSRF) {
217             uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes("UTF-8"));
218         }
219         return uc;
220     }
221
222 }