]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/main/RegisterPageTest.java
Add basic "RegisterPage"-Tests
[gigi.git] / tests / org / cacert / gigi / pages / main / RegisterPageTest.java
1 package org.cacert.gigi.pages.main;
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.URLEncoder;
11
12 import org.cacert.gigi.IOUtils;
13 import org.cacert.gigi.InitTruststore;
14 import org.junit.Before;
15 import org.junit.Ignore;
16 import org.junit.Test;
17
18 public class RegisterPageTest {
19         private static final URL registerService;
20         static {
21                 URL u = null;
22                 try {
23                         u = new URL("https://localhost/register");
24                 } catch (MalformedURLException e) {
25                         e.printStackTrace();
26                 }
27                 registerService = u;
28                 InitTruststore.run();
29                 HttpURLConnection.setFollowRedirects(false);
30         }
31
32         @Before
33         public void setUp() throws Exception {
34         }
35
36         @Test
37         public void testNoFname() throws IOException {
38                 testFailedForm("lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910");
39         }
40         @Test
41         public void testNoLname() throws IOException {
42                 testFailedForm("fname=a&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910");
43         }
44         @Test
45         public void testNoEmail() throws IOException {
46                 testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910");
47         }
48
49         @Test
50         public void testNoPword() throws IOException {
51                 testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910");
52         }
53
54         @Test
55         public void testDiffPword() throws IOException {
56                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap2&day=1&month=1&year=1910");
57         }
58
59         @Test
60         public void testNoDay() throws IOException {
61                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&month=1&year=1910");
62         }
63         @Test
64         public void testNoMonth() throws IOException {
65                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&year=1910");
66         }
67         @Test
68         public void testNoYear() throws IOException {
69                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1");
70         }
71         @Test
72         public void testInvDay() throws IOException {
73                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=40&month=1&year=1910");
74                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=0&month=1&year=1910");
75                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=a&month=1&year=1910");
76         }
77         @Test
78         public void testInvMonth() throws IOException {
79                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=20&year=1910");
80                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=0&year=1910");
81                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=-1&year=1910");
82                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=a&year=1910");
83         }
84         @Test
85         public void testInvYear() throws IOException {
86                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=0");
87                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=100");
88                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=a");
89                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=-1");
90         }
91
92         @Test
93         public void testDataStays() throws IOException {
94                 long uniq = System.currentTimeMillis();
95                 String run = runRegister("fname=fn" + uniq + "&lname=ln" + uniq
96                                 + "&email=ma" + uniq + "@cacert.org&pword1=pas" + uniq
97                                 + "&pword2=pas2" + uniq + "&day=1&month=1&year=0");
98                 assertTrue(run.contains("fn" + uniq));
99                 assertTrue(run.contains("ln" + uniq));
100                 assertTrue(run.contains("ma" + uniq + "@cacert.org"));
101                 assertTrue(!run.contains("pas" + uniq));
102                 assertTrue(!run.contains("pas2" + uniq));
103
104         }
105
106         @Test
107         public void testCheckboxesStay() throws IOException {
108                 String run2 = runRegister("general=1&country=a&regional=1&radius=0");
109                 assertTrue(run2
110                                 .contains("name=\"general\" value=\"1\" checked=\"checked\">"));
111                 assertTrue(run2.contains("name=\"country\" value=\"1\">"));
112                 assertTrue(run2
113                                 .contains("name=\"regional\" value=\"1\" checked=\"checked\">"));
114                 assertTrue(run2.contains("name=\"radius\" value=\"1\">"));
115                 run2 = runRegister("general=0&country=1&radius=1");
116                 assertTrue(run2.contains("name=\"general\" value=\"1\">"));
117                 assertTrue(run2
118                                 .contains("name=\"country\" value=\"1\" checked=\"checked\">"));
119                 assertTrue(run2.contains("name=\"regional\" value=\"1\">"));
120                 assertTrue(run2
121                                 .contains("name=\"radius\" value=\"1\" checked=\"checked\">"));
122         }
123
124         @Ignore
125         @Test
126         public void testDoubleMail() throws IOException {
127                 long uniq = System.currentTimeMillis();
128                 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org",
129                                 "registerPW");
130                 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org",
131                                 "registerPW");
132         }
133
134         private static void testFailedForm(String query) throws IOException {
135                 String d = runRegister(query);
136                 String formFail = "<div class='formError'>";
137                 int idx = d.indexOf(formFail);
138                 assertNotEquals(-1, idx);
139                 String startError = d.substring(idx + formFail.length(), idx + 100)
140                                 .trim();
141                 assertTrue(startError, !startError.startsWith("</div>"));
142         }
143
144         public static void registerUser(String firstName, String lastName,
145                         String email, String password) {
146                 try {
147                         String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
148                                         + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
149                                         + "&email=" + URLEncoder.encode(firstName, "UTF-8")
150                                         + "&pword1=" + URLEncoder.encode(password, "UTF-8")
151                                         + "&pword2=" + URLEncoder.encode(password, "UTF-8")
152                                         + "&day=1&month=1&year=1910";
153                 } catch (UnsupportedEncodingException e) {
154                         e.printStackTrace();
155                 }
156         }
157
158         private static String runRegister(String param) throws IOException {
159                 HttpURLConnection uc = (HttpURLConnection) registerService
160                                 .openConnection();
161                 uc.setDoOutput(true);
162                 uc.getOutputStream().write(param.getBytes());
163                 String d = IOUtils.readURL(uc);
164                 return d;
165         }
166 }