]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/wot/TestAssurance.java
ADD: Assurance listing tests
[gigi.git] / tests / org / cacert / gigi / pages / wot / TestAssurance.java
1 package org.cacert.gigi.pages.wot;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.io.UnsupportedEncodingException;
7 import java.net.HttpURLConnection;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.net.URLConnection;
11 import java.net.URLEncoder;
12 import java.sql.PreparedStatement;
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.database.DatabaseConnection;
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 int assurer;
31
32     private int assuree;
33
34     private String cookie;
35
36     @Before
37     public void setup() throws IOException {
38         assurerM = createUniqueName() + "@cacert-test.org";
39         assureeM = createUniqueName() + "@cacert-test.org";
40         assurer = createAssuranceUser("a", "b", assurerM, TEST_PASSWORD);
41         assuree = createAssuranceUser("a", "c", assureeM, TEST_PASSWORD);
42         cookie = login(assurerM, TEST_PASSWORD);
43
44     }
45
46     @Test
47     public void testAssureSearch() throws IOException {
48         String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
49         assertTrue(loc, loc.endsWith(AssurePage.PATH + "/" + assuree));
50     }
51
52     @Test
53     public void testAssureSearchEmail() throws IOException {
54         String loc = search("email=1" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910");
55         assertNull(loc);
56     }
57
58     @Test
59     public void testAssureSearchDob() throws IOException {
60         String loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=2&month=1&year=1910");
61         assertNull(loc);
62         loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=2&year=1910");
63         assertNull(loc);
64         loc = search("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1911");
65         assertNull(loc);
66     }
67
68     private String search(String query) throws MalformedURLException, IOException, UnsupportedEncodingException {
69         URL u = new URL("https://" + getServerName() + AssurePage.PATH);
70         URLConnection uc = u.openConnection();
71         uc.setDoOutput(true);
72         uc.addRequestProperty("Cookie", cookie);
73         uc.getOutputStream().write((query).getBytes());
74         uc.getOutputStream().flush();
75
76         String loc = uc.getHeaderField("Location");
77         return loc;
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         assertTrue(resp.contains(uniqueLoc));
163     }
164
165
166     @Test
167     public void testAssurerListingValid() throws IOException {
168         String uniqueLoc = createUniqueName();
169         String error = getError("date=2000-01-01&location=" + uniqueLoc + "&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10");
170         assertNull(error);
171         String cookie = login(assurerM, TEST_PASSWORD);
172         URLConnection url = new URL("https://" + getServerName() + MyPoints.PATH).openConnection();
173         url.setRequestProperty("Cookie", cookie);
174         String resp = IOUtils.readURL(url);
175         resp = resp.split(Pattern.quote("</table>"))[1];
176         assertTrue(resp.contains(uniqueLoc));
177     }
178
179     private String getError(String query) throws MalformedURLException, IOException {
180         URLConnection uc = buildupAssureFormConnection(true);
181         uc.getOutputStream().write((query).getBytes());
182         uc.getOutputStream().flush();
183         String error = fetchStartErrorMessage(IOUtils.readURL(uc));
184         return error;
185     }
186
187     private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
188         URL u = new URL("https://" + getServerName() + AssurePage.PATH + "/" + assuree);
189         URLConnection uc = u.openConnection();
190         uc.addRequestProperty("Cookie", cookie);
191         String csrf = getCSRF(uc);
192         uc = u.openConnection();
193         uc.addRequestProperty("Cookie", cookie);
194         uc.setDoOutput(true);
195         if (doCSRF) {
196             uc.getOutputStream().write(("csrf=" + csrf + "&").getBytes());
197         }
198         return uc;
199     }
200
201 }