]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/main/RegisterPageTest.java
97e6f8182fdb7e1cd6287c2d4ae5231b7d962446
[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         @Test
36         public void testSuccess() throws IOException {
37                 String startError = fetchStartErrorMessage("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
38                 assertTrue(startError, startError.startsWith("</div>"));
39         }
40
41         @Test
42         public void testNoFname() throws IOException {
43                 testFailedForm("lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
44         }
45         @Test
46         public void testNoLname() throws IOException {
47                 testFailedForm("fname=a&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
48         }
49         @Test
50         public void testNoEmail() throws IOException {
51                 testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
52         }
53
54         @Test
55         public void testNoPword() throws IOException {
56                 testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
57         }
58
59         @Test
60         public void testDiffPword() throws IOException {
61                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap2&day=1&month=1&year=1910&cca_agree=1");
62         }
63
64         @Test
65         public void testNoDay() throws IOException {
66                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&month=1&year=1910&cca_agree=1");
67         }
68         @Test
69         public void testNoMonth() throws IOException {
70                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&year=1910&cca_agree=1");
71         }
72         @Test
73         public void testNoYear() throws IOException {
74                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&cca_agree=1");
75         }
76         @Test
77         public void testInvDay() throws IOException {
78                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=40&month=1&year=1910&cca_agree=1");
79                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=0&month=1&year=1910&cca_agree=1");
80                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=a&month=1&year=1910&cca_agree=1");
81         }
82         @Test
83         public void testInvMonth() throws IOException {
84                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=20&year=1910&cca_agree=1");
85                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=0&year=1910&cca_agree=1");
86                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=-1&year=1910&cca_agree=1");
87                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=a&year=1910&cca_agree=1");
88         }
89         @Test
90         public void testInvYear() throws IOException {
91                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=0&cca_agree=1");
92                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=100&cca_agree=1");
93                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=a&cca_agree=1");
94                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=-1&cca_agree=1");
95         }
96         @Test
97         public void testNoAgree() throws IOException {
98                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=a");
99         }
100
101         @Test
102         public void testDataStays() throws IOException {
103                 long uniq = System.currentTimeMillis();
104                 String run = runRegister("fname=fn" + uniq + "&lname=ln" + uniq
105                                 + "&email=ma" + uniq + "@cacert.org&pword1=pas" + uniq
106                                 + "&pword2=pas2" + uniq + "&day=1&month=1&year=0");
107                 assertTrue(run.contains("fn" + uniq));
108                 assertTrue(run.contains("ln" + uniq));
109                 assertTrue(run.contains("ma" + uniq + "@cacert.org"));
110                 assertTrue(!run.contains("pas" + uniq));
111                 assertTrue(!run.contains("pas2" + uniq));
112
113         }
114
115         @Test
116         public void testCheckboxesStay() throws IOException {
117                 String run2 = runRegister("general=1&country=a&regional=1&radius=0");
118                 assertTrue(run2
119                                 .contains("name=\"general\" value=\"1\" checked=\"checked\">"));
120                 assertTrue(run2.contains("name=\"country\" value=\"1\">"));
121                 assertTrue(run2
122                                 .contains("name=\"regional\" value=\"1\" checked=\"checked\">"));
123                 assertTrue(run2.contains("name=\"radius\" value=\"1\">"));
124                 run2 = runRegister("general=0&country=1&radius=1");
125                 assertTrue(run2.contains("name=\"general\" value=\"1\">"));
126                 assertTrue(run2
127                                 .contains("name=\"country\" value=\"1\" checked=\"checked\">"));
128                 assertTrue(run2.contains("name=\"regional\" value=\"1\">"));
129                 assertTrue(run2
130                                 .contains("name=\"radius\" value=\"1\" checked=\"checked\">"));
131         }
132
133         @Ignore
134         @Test
135         public void testDoubleMail() throws IOException {
136                 long uniq = System.currentTimeMillis();
137                 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org",
138                                 "registerPW");
139                 try {
140                         registerUser("RegisterTest", "User", "testmail" + uniq
141                                         + "@cacert.org", "registerPW");
142                         throw new Error(
143                                         "Registering a user with the same email needs to fail.");
144                 } catch (AssertionError e) {
145
146                 }
147         }
148
149         private static void testFailedForm(String query) throws IOException {
150                 String startError = fetchStartErrorMessage(query);
151                 assertTrue(startError, !startError.startsWith("</div>"));
152         }
153         private static String fetchStartErrorMessage(String query)
154                         throws IOException {
155                 String d = runRegister(query);
156                 String formFail = "<div class='formError'>";
157                 int idx = d.indexOf(formFail);
158                 assertNotEquals(-1, idx);
159                 String startError = d.substring(idx + formFail.length(), idx + 100)
160                                 .trim();
161                 return startError;
162         }
163
164         public static void registerUser(String firstName, String lastName,
165                         String email, String password) {
166                 try {
167                         String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
168                                         + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
169                                         + "&email=" + URLEncoder.encode(email, "UTF-8")
170                                         + "&pword1=" + URLEncoder.encode(password, "UTF-8")
171                                         + "&pword2=" + URLEncoder.encode(password, "UTF-8")
172                                         + "&day=1&month=1&year=1910&cca_agree=1";
173                         String data = fetchStartErrorMessage(query);
174                         assertTrue(data, data.startsWith("</div>"));
175                 } catch (UnsupportedEncodingException e) {
176                         throw new Error(e);
177                 } catch (IOException e) {
178                         throw new Error(e);
179                 }
180         }
181         private static String runRegister(String param) throws IOException {
182                 HttpURLConnection uc = (HttpURLConnection) registerService
183                                 .openConnection();
184                 uc.setDoOutput(true);
185                 uc.getOutputStream().write(param.getBytes());
186                 String d = IOUtils.readURL(uc);
187                 return d;
188         }
189 }