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