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 {
36 clearCaches(); // We do many registers in this test suite.
39 private static String createBase() {
40 return createUniqueName() + "@email.de";
44 public void testSuccess() throws IOException, InterruptedException {
45 long uniq = System.currentTimeMillis();
46 registerUser("ab", "b", "correct" + uniq + "@email.de", TEST_PASSWORD);
47 assertSuccessfullRegMail();
49 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=";
51 String query = defaultSignup + URLEncoder.encode("correct3_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1®ional=1&radius=1&name-type=western";
52 String data = fetchStartErrorMessage(runRegister(query));
54 assertSuccessfullRegMail();
56 getMailReceiver().setEmailCheckError("400 Greylisted");
57 getMailReceiver().setApproveRegex(Pattern.compile("a"));
58 query = defaultSignup + URLEncoder.encode("correct4_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1®ional=1&radius=1";
59 data = fetchStartErrorMessage(runRegister(query));
62 assertNull(getMailReceiver().poll());
66 private void assertSuccessfullRegMail() {
67 String link = getMailReceiver().receive().extractLink();
68 assertTrue(link, link.startsWith("https://"));
72 public void testNoFname() throws IOException {
73 testFailedForm("lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
77 public void testNoLname() throws IOException {
78 testFailedForm("fname=a" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
82 public void testNoEmail() throws IOException {
83 testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910&tos_agree=1");
87 public void testNoPword() throws IOException {
88 testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910&tos_agree=1");
92 public void testDiffPword() throws IOException {
93 testFailedForm("fname=a&lname=b" + createBase() + "2&day=1&month=1&year=1910&tos_agree=1");
97 public void testNoDay() throws IOException {
98 testFailedForm("fname=a&lname=b" + createBase() + "&month=1&year=1910&tos_agree=1");
102 public void testNoMonth() throws IOException {
103 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&year=1910&tos_agree=1");
107 public void testNoYear() throws IOException {
108 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&tos_agree=1");
112 public void testInvDay() throws IOException {
113 testFailedForm("fname=a&lname=b" + createBase() + "&day=40&month=1&year=1910&tos_agree=1");
114 testFailedForm("fname=a&lname=b" + createBase() + "&day=0&month=1&year=1910&tos_agree=1");
115 testFailedForm("fname=a&lname=b" + createBase() + "&day=-1&month=1&year=1910&tos_agree=1");
116 testFailedForm("fname=a&lname=b" + createBase() + "&day=a&month=1&year=1910&tos_agree=1");
120 public void testInvMonth() throws IOException {
121 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=20&year=1910&tos_agree=1");
122 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=0&year=1910&tos_agree=1");
123 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=-1&year=1910&tos_agree=1");
124 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=a&year=1910&tos_agree=1");
128 public void testInvYear() throws IOException {
129 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=0&tos_agree=1");
130 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=100&tos_agree=1");
131 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=a&tos_agree=1");
132 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=-1&tos_agree=1");
136 public void testNoAgree() throws IOException {
137 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=a");
141 public void testTooYoung() throws IOException {
142 Calendar c = GregorianCalendar.getInstance();
143 c.add(Calendar.YEAR, -User.MINIMUM_AGE + 2);
144 testFailedForm("fname=a&lname=b&email=" + createUniqueName() + "@email.de" + p + "&day=" + c.get(Calendar.DAY_OF_MONTH) + "&month=" + (c.get(Calendar.MONTH) + 1) + "&year=" + c.get(Calendar.YEAR) + "&tos_agree=1");
148 public void testTooOld() throws IOException {
149 Calendar c = GregorianCalendar.getInstance();
150 c.add(Calendar.YEAR, -User.MAXIMUM_PLAUSIBLE_AGE);
151 c.add(Calendar.DAY_OF_MONTH, -1);
152 testFailedForm("fname=a&lname=b&email=" + createUniqueName() + "@email.de" + p + "&day=" + c.get(Calendar.DAY_OF_MONTH) + "&month=" + (c.get(Calendar.MONTH) + 1) + "&year=" + c.get(Calendar.YEAR) + "&tos_agree=1");
156 public void testDataStays() throws IOException {
157 long uniq = System.currentTimeMillis();
158 String run = runRegister("fname=fn" + uniq + "&lname=ln" + uniq + "&email=ma" + uniq + "@cacert.org&pword1=pas" + uniq + "&pword2=pas2" + uniq + "&day=28&month=10&year=1950");
159 assertThat(run, containsString("fn" + uniq));
160 assertThat(run, containsString("ln" + uniq));
161 assertThat(run, containsString("ma" + uniq + "@cacert.org"));
162 assertThat(run, not(containsString("pas" + uniq)));
163 assertThat(run, not(containsString("pas2" + uniq)));
165 assertThat(run, containsString("name=\"year\" value=\"1950\""));
167 assertThat(run, containsString("<option value='10' selected=\"selected\">O"));
169 assertThat(run, containsString("<option selected=\"selected\">28</option>"));
173 public void testCheckboxesStay() throws IOException {
174 String run2 = runRegister("general=1&country=a®ional=1&radius=0");
175 assertThat(run2, containsString("name=\"general\" value=\"1\" checked=\"checked\">"));
176 assertThat(run2, containsString("name=\"country\" value=\"1\">"));
177 assertThat(run2, containsString("name=\"regional\" value=\"1\" checked=\"checked\">"));
178 assertThat(run2, containsString("name=\"radius\" value=\"1\">"));
179 run2 = runRegister("general=0&country=1&radius=1");
180 assertThat(run2, containsString("name=\"general\" value=\"1\">"));
181 assertThat(run2, containsString("name=\"country\" value=\"1\" checked=\"checked\">"));
182 assertThat(run2, containsString("name=\"regional\" value=\"1\">"));
183 assertThat(run2, containsString("name=\"radius\" value=\"1\" checked=\"checked\">"));
187 public void testDoubleMail() throws IOException {
188 long uniq = System.currentTimeMillis();
189 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
191 registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
192 throw new Error("Registering a user with the same email needs to fail.");
193 } catch (AssertionError e) {
199 public void testInvalidMailbox() {
200 getMailReceiver().setApproveRegex(Pattern.compile("a"));
201 long uniq = System.currentTimeMillis();
203 registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq + "@cacert.org", TEST_PASSWORD);
204 throw new Error("Registering a user with invalid mailbox must fail.");
205 } catch (AssertionError e) {
210 private void testFailedForm(String query) throws IOException {
211 String startError = fetchStartErrorMessage(runRegister(query));
212 assertTrue(startError, !startError.startsWith("</div>"));
216 public void testRegisterWithCountry() throws IOException, InterruptedException {
217 long uniq = System.currentTimeMillis();
218 String email = "country" + uniq + "@email.de";
220 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=";
222 String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&general=1&country=1®ional=1&radius=1&name-type=western&residenceCountry=DE";
223 String data = fetchStartErrorMessage(runRegister(query));
225 User u = User.getByEmail(email);
226 assertEquals("DE", u.getResidenceCountry().getCode());
230 public void testRegisterWithoutCountry() throws IOException, InterruptedException {
231 long uniq = System.currentTimeMillis();
232 String email = "countryno" + uniq + "@email.de";
234 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=";
236 String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&general=1&country=1®ional=1&radius=1&name-type=western&residenceCountry=invalid";
237 String data = fetchStartErrorMessage(runRegister(query));
239 User u = User.getByEmail(email);
240 assertEquals(null, u.getResidenceCountry());