1 package org.cacert.gigi.pages.main;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.io.UnsupportedEncodingException;
7 import java.net.HttpURLConnection;
8 import java.net.MalformedURLException;
10 import java.net.URLEncoder;
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;
18 public class RegisterPageTest {
19 private static final URL registerService;
23 u = new URL("https://localhost/register");
24 } catch (MalformedURLException e) {
29 HttpURLConnection.setFollowRedirects(false);
33 public void setUp() throws Exception {
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>"));
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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));
116 public void testCheckboxesStay() throws IOException {
117 String run2 = runRegister("general=1&country=a®ional=1&radius=0");
119 .contains("name=\"general\" value=\"1\" checked=\"checked\">"));
120 assertTrue(run2.contains("name=\"country\" value=\"1\">"));
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\">"));
127 .contains("name=\"country\" value=\"1\" checked=\"checked\">"));
128 assertTrue(run2.contains("name=\"regional\" value=\"1\">"));
130 .contains("name=\"radius\" value=\"1\" checked=\"checked\">"));
135 public void testDoubleMail() throws IOException {
136 long uniq = System.currentTimeMillis();
137 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org",
140 registerUser("RegisterTest", "User", "testmail" + uniq
141 + "@cacert.org", "registerPW");
143 "Registering a user with the same email needs to fail.");
144 } catch (AssertionError e) {
149 private static void testFailedForm(String query) throws IOException {
150 String startError = fetchStartErrorMessage(query);
151 assertTrue(startError, !startError.startsWith("</div>"));
153 private static String fetchStartErrorMessage(String query)
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)
164 public static void registerUser(String firstName, String lastName,
165 String email, String password) {
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) {
177 } catch (IOException e) {
181 private static String runRegister(String param) throws IOException {
182 HttpURLConnection uc = (HttpURLConnection) registerService
184 uc.setDoOutput(true);
185 uc.getOutputStream().write(param.getBytes());
186 String d = IOUtils.readURL(uc);