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;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
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;
22 public class RegisterPageTest extends ManagedTest {
23 private final URL registerService;
26 HttpURLConnection.setFollowRedirects(false);
29 public RegisterPageTest() {
32 u = new URL("https://" + getServerName() + "/register");
33 } catch (MalformedURLException e) {
39 public void setUp() throws Exception {
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());
52 System.out.println(tm.getSubject());
53 System.out.println(m.group(0));
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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));
131 public void testCheckboxesStay() throws IOException {
132 String run2 = runRegister("general=1&country=a®ional=1&radius=0");
134 .contains("name=\"general\" value=\"1\" checked=\"checked\">"));
135 assertTrue(run2.contains("name=\"country\" value=\"1\">"));
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\">"));
142 .contains("name=\"country\" value=\"1\" checked=\"checked\">"));
143 assertTrue(run2.contains("name=\"regional\" value=\"1\">"));
145 .contains("name=\"radius\" value=\"1\" checked=\"checked\">"));
150 public void testDoubleMail() throws IOException {
151 long uniq = System.currentTimeMillis();
152 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org",
155 registerUser("RegisterTest", "User", "testmail" + uniq
156 + "@cacert.org", "registerPW");
158 "Registering a user with the same email needs to fail.");
159 } catch (AssertionError e) {
164 private void testFailedForm(String query) throws IOException {
165 String startError = fetchStartErrorMessage(query);
166 assertTrue(startError, !startError.startsWith("</div>"));
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)
178 public void registerUser(String firstName, String lastName, String email,
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) {
191 } catch (IOException e) {
195 private String runRegister(String param) throws IOException {
196 HttpURLConnection uc = (HttpURLConnection) registerService
198 uc.setDoOutput(true);
199 uc.getOutputStream().write(param.getBytes());
200 String d = IOUtils.readURL(uc);