1 package org.cacert.gigi.pages.main;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
6 import java.io.IOException;
7 import java.io.UnsupportedEncodingException;
8 import java.net.HttpURLConnection;
9 import java.net.URLEncoder;
10 import java.util.Calendar;
11 import java.util.GregorianCalendar;
12 import java.util.regex.Pattern;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.testUtils.InitTruststore;
16 import org.cacert.gigi.testUtils.ManagedTest;
17 import org.junit.Before;
18 import org.junit.Test;
20 public class RegisterPageTest extends ManagedTest {
24 HttpURLConnection.setFollowRedirects(false);
26 p = "&pword1=" + URLEncoder.encode(TEST_PASSWORD, "UTF-8") + "&pword2=" + URLEncoder.encode(TEST_PASSWORD, "UTF-8");
27 } catch (UnsupportedEncodingException e) {
32 public static final String p;
35 public void setUp() throws Exception {}
37 private static String createBase() {
38 return createUniqueName() + "@email.de";
42 public void testSuccess() throws IOException, InterruptedException {
43 long uniq = System.currentTimeMillis();
44 registerUser("ab", "b", "correct" + uniq + "@email.de", TEST_PASSWORD);
45 assertSuccessfullRegMail();
47 String defaultSignup = "fname=" + URLEncoder.encode("ab", "UTF-8") + "&lname=" + URLEncoder.encode("b", "UTF-8") + "&pword1=" + URLEncoder.encode(TEST_PASSWORD, "UTF-8") + "&pword2=" + URLEncoder.encode(TEST_PASSWORD, "UTF-8") + "&day=1&month=1&year=1910&tos_agree=1&mname=mn&suffix=sf&email=";
49 String query = defaultSignup + URLEncoder.encode("correct3_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1®ional=1&radius=1";
50 String data = fetchStartErrorMessage(runRegister(query));
52 assertSuccessfullRegMail();
54 getMailReciever().setEmailCheckError("400 Greylisted");
55 getMailReciever().setApproveRegex(Pattern.compile("a"));
56 query = defaultSignup + URLEncoder.encode("correct4_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1®ional=1&radius=1";
57 data = fetchStartErrorMessage(runRegister(query));
60 assertNull(getMailReciever().poll());
64 private void assertSuccessfullRegMail() {
65 String link = getMailReciever().receive().extractLink();
66 assertTrue(link, link.startsWith("https://"));
70 public void testNoFname() throws IOException {
71 testFailedForm("lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
75 public void testNoLname() throws IOException {
76 testFailedForm("fname=a" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
80 public void testNoEmail() throws IOException {
81 testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910&tos_agree=1");
85 public void testNoPword() throws IOException {
86 testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910&tos_agree=1");
90 public void testDiffPword() throws IOException {
91 testFailedForm("fname=a&lname=b" + createBase() + "2&day=1&month=1&year=1910&tos_agree=1");
95 public void testNoDay() throws IOException {
96 testFailedForm("fname=a&lname=b" + createBase() + "&month=1&year=1910&tos_agree=1");
100 public void testNoMonth() throws IOException {
101 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&year=1910&tos_agree=1");
105 public void testNoYear() throws IOException {
106 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&tos_agree=1");
110 public void testInvDay() throws IOException {
111 testFailedForm("fname=a&lname=b" + createBase() + "&day=40&month=1&year=1910&tos_agree=1");
112 testFailedForm("fname=a&lname=b" + createBase() + "&day=0&month=1&year=1910&tos_agree=1");
113 testFailedForm("fname=a&lname=b" + createBase() + "&day=-1&month=1&year=1910&tos_agree=1");
114 testFailedForm("fname=a&lname=b" + createBase() + "&day=a&month=1&year=1910&tos_agree=1");
118 public void testInvMonth() throws IOException {
119 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=20&year=1910&tos_agree=1");
120 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=0&year=1910&tos_agree=1");
121 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=-1&year=1910&tos_agree=1");
122 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=a&year=1910&tos_agree=1");
126 public void testInvYear() throws IOException {
127 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=0&tos_agree=1");
128 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=100&tos_agree=1");
129 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=a&tos_agree=1");
130 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=-1&tos_agree=1");
134 public void testNoAgree() throws IOException {
135 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=a");
139 public void testTooYoung() throws IOException {
140 Calendar c = GregorianCalendar.getInstance();
141 c.add(Calendar.YEAR, -User.MINIMUM_AGE + 2);
142 testFailedForm("fname=a&lname=b&email=" + createUniqueName() + "@email.de" + p + "&day=1&month=1&year=" + c.get(Calendar.YEAR) + "&tos_agree=1");
146 public void testDataStays() throws IOException {
147 long uniq = System.currentTimeMillis();
148 String run = runRegister("fname=fn" + uniq + "&lname=ln" + uniq + "&email=ma" + uniq + "@cacert.org&pword1=pas" + uniq + "&pword2=pas2" + uniq + "&day=1&month=1&year=0");
149 assertThat(run, containsString("fn" + uniq));
150 assertThat(run, containsString("ln" + uniq));
151 assertThat(run, containsString("ma" + uniq + "@cacert.org"));
152 assertThat(run, not(containsString("pas" + uniq)));
153 assertThat(run, not(containsString("pas2" + uniq)));
158 public void testCheckboxesStay() throws IOException {
159 String run2 = runRegister("general=1&country=a®ional=1&radius=0");
160 assertThat(run2, containsString("name=\"general\" value=\"1\" checked=\"checked\">"));
161 assertThat(run2, containsString("name=\"country\" value=\"1\">"));
162 assertThat(run2, containsString("name=\"regional\" value=\"1\" checked=\"checked\">"));
163 assertThat(run2, containsString("name=\"radius\" value=\"1\">"));
164 run2 = runRegister("general=0&country=1&radius=1");
165 assertThat(run2, containsString("name=\"general\" value=\"1\">"));
166 assertThat(run2, containsString("name=\"country\" value=\"1\" checked=\"checked\">"));
167 assertThat(run2, containsString("name=\"regional\" value=\"1\">"));
168 assertThat(run2, containsString("name=\"radius\" value=\"1\" checked=\"checked\">"));
172 public void testDoubleMail() throws IOException {
173 long uniq = System.currentTimeMillis();
174 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
176 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
177 throw new Error("Registering a user with the same email needs to fail.");
178 } catch (AssertionError e) {
184 public void testInvalidMailbox() {
185 getMailReciever().setApproveRegex(Pattern.compile("a"));
186 long uniq = System.currentTimeMillis();
188 registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq + "@cacert.org", TEST_PASSWORD);
189 throw new Error("Registering a user with invalid mailbox must fail.");
190 } catch (AssertionError e) {
195 private void testFailedForm(String query) throws IOException {
196 String startError = fetchStartErrorMessage(runRegister(query));
197 assertTrue(startError, !startError.startsWith("</div>"));