]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/main/RegisterPageTest.java
Adding automated system bootup 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 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13
14 import org.cacert.gigi.IOUtils;
15 import org.cacert.gigi.InitTruststore;
16 import org.cacert.gigi.testUtils.ManagedTest;
17 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
18 import org.junit.Before;
19 import org.junit.Ignore;
20 import org.junit.Test;
21
22 public class RegisterPageTest extends ManagedTest {
23         private final URL registerService;
24         static {
25                 InitTruststore.run();
26                 HttpURLConnection.setFollowRedirects(false);
27         }
28
29         public RegisterPageTest() {
30                 URL u = null;
31                 try {
32                         u = new URL("https://" + getServerName() + "/register");
33                 } catch (MalformedURLException e) {
34                         e.printStackTrace();
35                 }
36                 registerService = u;
37         }
38         @Before
39         public void setUp() throws Exception {
40         }
41         @Test
42         public void testSuccess() throws IOException {
43                 String startError = fetchStartErrorMessage("fname=ab&lname=b&email="
44                                 + URLEncoder.encode("felix+" + System.currentTimeMillis()
45                                                 + "@dogcraft.de", "UTF-8")
46                                 + "&pword1=ap12UI.a'&pword2=ap12UI.a'&day=1&month=1&year=1910&cca_agree=1");
47                 assertTrue(startError, startError.startsWith("</div>"));
48                 TestMail tm = waitForMail();
49                 Pattern link = Pattern.compile("http://[^\\s]+(?=\\s)");
50                 Matcher m = link.matcher(tm.getMessage());
51                 m.find();
52                 System.out.println(tm.getSubject());
53                 System.out.println(m.group(0));
54         }
55         @Test
56         public void testNoFname() throws IOException {
57                 testFailedForm("lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
58         }
59         @Test
60         public void testNoLname() throws IOException {
61                 testFailedForm("fname=a&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
62         }
63         @Test
64         public void testNoEmail() throws IOException {
65                 testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
66         }
67
68         @Test
69         public void testNoPword() throws IOException {
70                 testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
71         }
72
73         @Test
74         public void testDiffPword() throws IOException {
75                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap2&day=1&month=1&year=1910&cca_agree=1");
76         }
77
78         @Test
79         public void testNoDay() throws IOException {
80                 System.out.println(registerService);
81                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&month=1&year=1910&cca_agree=1");
82         }
83         @Test
84         public void testNoMonth() throws IOException {
85                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&year=1910&cca_agree=1");
86         }
87         @Test
88         public void testNoYear() throws IOException {
89                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&cca_agree=1");
90         }
91         @Test
92         public void testInvDay() throws IOException {
93                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=40&month=1&year=1910&cca_agree=1");
94                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=0&month=1&year=1910&cca_agree=1");
95                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=a&month=1&year=1910&cca_agree=1");
96         }
97         @Test
98         public void testInvMonth() throws IOException {
99                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=20&year=1910&cca_agree=1");
100                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=0&year=1910&cca_agree=1");
101                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=-1&year=1910&cca_agree=1");
102                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=a&year=1910&cca_agree=1");
103         }
104         @Test
105         public void testInvYear() throws IOException {
106                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=0&cca_agree=1");
107                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=100&cca_agree=1");
108                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=a&cca_agree=1");
109                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=-1&cca_agree=1");
110         }
111         @Test
112         public void testNoAgree() throws IOException {
113                 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=a");
114         }
115
116         @Test
117         public void testDataStays() throws IOException {
118                 long uniq = System.currentTimeMillis();
119                 String run = runRegister("fname=fn" + uniq + "&lname=ln" + uniq
120                                 + "&email=ma" + uniq + "@cacert.org&pword1=pas" + uniq
121                                 + "&pword2=pas2" + uniq + "&day=1&month=1&year=0");
122                 assertTrue(run.contains("fn" + uniq));
123                 assertTrue(run.contains("ln" + uniq));
124                 assertTrue(run.contains("ma" + uniq + "@cacert.org"));
125                 assertTrue(!run.contains("pas" + uniq));
126                 assertTrue(!run.contains("pas2" + uniq));
127
128         }
129
130         @Test
131         public void testCheckboxesStay() throws IOException {
132                 String run2 = runRegister("general=1&country=a&regional=1&radius=0");
133                 assertTrue(run2
134                                 .contains("name=\"general\" value=\"1\" checked=\"checked\">"));
135                 assertTrue(run2.contains("name=\"country\" value=\"1\">"));
136                 assertTrue(run2
137                                 .contains("name=\"regional\" value=\"1\" checked=\"checked\">"));
138                 assertTrue(run2.contains("name=\"radius\" value=\"1\">"));
139                 run2 = runRegister("general=0&country=1&radius=1");
140                 assertTrue(run2.contains("name=\"general\" value=\"1\">"));
141                 assertTrue(run2
142                                 .contains("name=\"country\" value=\"1\" checked=\"checked\">"));
143                 assertTrue(run2.contains("name=\"regional\" value=\"1\">"));
144                 assertTrue(run2
145                                 .contains("name=\"radius\" value=\"1\" checked=\"checked\">"));
146         }
147
148         @Ignore
149         @Test
150         public void testDoubleMail() throws IOException {
151                 long uniq = System.currentTimeMillis();
152                 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org",
153                                 "registerPW");
154                 try {
155                         registerUser("RegisterTest", "User", "testmail" + uniq
156                                         + "@cacert.org", "registerPW");
157                         throw new Error(
158                                         "Registering a user with the same email needs to fail.");
159                 } catch (AssertionError e) {
160
161                 }
162         }
163
164         private void testFailedForm(String query) throws IOException {
165                 String startError = fetchStartErrorMessage(query);
166                 assertTrue(startError, !startError.startsWith("</div>"));
167         }
168         private String fetchStartErrorMessage(String query) throws IOException {
169                 String d = runRegister(query);
170                 String formFail = "<div class='formError'>";
171                 int idx = d.indexOf(formFail);
172                 assertNotEquals(-1, idx);
173                 String startError = d.substring(idx + formFail.length(), idx + 100)
174                                 .trim();
175                 return startError;
176         }
177
178         public void registerUser(String firstName, String lastName, String email,
179                         String password) {
180                 try {
181                         String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
182                                         + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
183                                         + "&email=" + URLEncoder.encode(email, "UTF-8")
184                                         + "&pword1=" + URLEncoder.encode(password, "UTF-8")
185                                         + "&pword2=" + URLEncoder.encode(password, "UTF-8")
186                                         + "&day=1&month=1&year=1910&cca_agree=1";
187                         String data = fetchStartErrorMessage(query);
188                         assertTrue(data, data.startsWith("</div>"));
189                 } catch (UnsupportedEncodingException e) {
190                         throw new Error(e);
191                 } catch (IOException e) {
192                         throw new Error(e);
193                 }
194         }
195         private String runRegister(String param) throws IOException {
196                 HttpURLConnection uc = (HttpURLConnection) registerService
197                                 .openConnection();
198                 uc.setDoOutput(true);
199                 uc.getOutputStream().write(param.getBytes());
200                 String d = IOUtils.readURL(uc);
201                 return d;
202         }
203 }