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