1 package org.cacert.gigi.pages.main;
3 import static org.junit.Assert.assertNotEquals;
4 import static org.junit.Assert.assertTrue;
6 import java.io.IOException;
7 import java.io.UnsupportedEncodingException;
8 import java.net.HttpURLConnection;
9 import java.net.MalformedURLException;
11 import java.net.URLEncoder;
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;
19 public class RegisterPageTest {
20 private static final URL registerService;
21 private static final int PORT = 4431;
25 u = new URL("https://localhost:" + PORT + "/register");
26 } catch (MalformedURLException e) {
31 HttpURLConnection.setFollowRedirects(false);
35 public void setUp() throws Exception {
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>"));
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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));
118 public void testCheckboxesStay() throws IOException {
119 String run2 = runRegister("general=1&country=a®ional=1&radius=0");
121 .contains("name=\"general\" value=\"1\" checked=\"checked\">"));
122 assertTrue(run2.contains("name=\"country\" value=\"1\">"));
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\">"));
129 .contains("name=\"country\" value=\"1\" checked=\"checked\">"));
130 assertTrue(run2.contains("name=\"regional\" value=\"1\">"));
132 .contains("name=\"radius\" value=\"1\" checked=\"checked\">"));
137 public void testDoubleMail() throws IOException {
138 long uniq = System.currentTimeMillis();
139 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org",
142 registerUser("RegisterTest", "User", "testmail" + uniq
143 + "@cacert.org", "registerPW");
145 "Registering a user with the same email needs to fail.");
146 } catch (AssertionError e) {
151 private static void testFailedForm(String query) throws IOException {
152 String startError = fetchStartErrorMessage(query);
153 assertTrue(startError, !startError.startsWith("</div>"));
155 private static String fetchStartErrorMessage(String query)
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)
166 public static void registerUser(String firstName, String lastName,
167 String email, String password) {
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) {
179 } catch (IOException e) {
183 private static String runRegister(String param) throws IOException {
184 HttpURLConnection uc = (HttpURLConnection) registerService
186 uc.setDoOutput(true);
187 uc.getOutputStream().write(param.getBytes());
188 String d = IOUtils.readURL(uc);