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 {
37 public void testNoFname() throws IOException {
38 testFailedForm("lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910");
41 public void testNoLname() throws IOException {
42 testFailedForm("fname=a&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910");
45 public void testNoEmail() throws IOException {
46 testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910");
50 public void testNoPword() throws IOException {
51 testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910");
55 public void testDiffPword() throws IOException {
56 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap2&day=1&month=1&year=1910");
60 public void testNoDay() throws IOException {
61 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&month=1&year=1910");
64 public void testNoMonth() throws IOException {
65 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&year=1910");
68 public void testNoYear() throws IOException {
69 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1");
72 public void testInvDay() throws IOException {
73 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=40&month=1&year=1910");
74 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=0&month=1&year=1910");
75 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=a&month=1&year=1910");
78 public void testInvMonth() throws IOException {
79 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=20&year=1910");
80 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=0&year=1910");
81 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=-1&year=1910");
82 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=a&year=1910");
85 public void testInvYear() throws IOException {
86 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=0");
87 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=100");
88 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=a");
89 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=-1");
93 public void testDataStays() throws IOException {
94 long uniq = System.currentTimeMillis();
95 String run = runRegister("fname=fn" + uniq + "&lname=ln" + uniq
96 + "&email=ma" + uniq + "@cacert.org&pword1=pas" + uniq
97 + "&pword2=pas2" + uniq + "&day=1&month=1&year=0");
98 assertTrue(run.contains("fn" + uniq));
99 assertTrue(run.contains("ln" + uniq));
100 assertTrue(run.contains("ma" + uniq + "@cacert.org"));
101 assertTrue(!run.contains("pas" + uniq));
102 assertTrue(!run.contains("pas2" + uniq));
107 public void testCheckboxesStay() throws IOException {
108 String run2 = runRegister("general=1&country=a®ional=1&radius=0");
110 .contains("name=\"general\" value=\"1\" checked=\"checked\">"));
111 assertTrue(run2.contains("name=\"country\" value=\"1\">"));
113 .contains("name=\"regional\" value=\"1\" checked=\"checked\">"));
114 assertTrue(run2.contains("name=\"radius\" value=\"1\">"));
115 run2 = runRegister("general=0&country=1&radius=1");
116 assertTrue(run2.contains("name=\"general\" value=\"1\">"));
118 .contains("name=\"country\" value=\"1\" checked=\"checked\">"));
119 assertTrue(run2.contains("name=\"regional\" value=\"1\">"));
121 .contains("name=\"radius\" value=\"1\" checked=\"checked\">"));
126 public void testDoubleMail() throws IOException {
127 long uniq = System.currentTimeMillis();
128 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org",
130 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org",
134 private static void testFailedForm(String query) throws IOException {
135 String d = runRegister(query);
136 String formFail = "<div class='formError'>";
137 int idx = d.indexOf(formFail);
138 assertNotEquals(-1, idx);
139 String startError = d.substring(idx + formFail.length(), idx + 100)
141 assertTrue(startError, !startError.startsWith("</div>"));
144 public static void registerUser(String firstName, String lastName,
145 String email, String password) {
147 String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
148 + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
149 + "&email=" + URLEncoder.encode(firstName, "UTF-8")
150 + "&pword1=" + URLEncoder.encode(password, "UTF-8")
151 + "&pword2=" + URLEncoder.encode(password, "UTF-8")
152 + "&day=1&month=1&year=1910";
153 } catch (UnsupportedEncodingException e) {
158 private static String runRegister(String param) throws IOException {
159 HttpURLConnection uc = (HttpURLConnection) registerService
161 uc.setDoOutput(true);
162 uc.getOutputStream().write(param.getBytes());
163 String d = IOUtils.readURL(uc);