1 package club.wpia.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.junit.Before;
15 import org.junit.Test;
17 import club.wpia.gigi.dbObjects.User;
18 import club.wpia.gigi.testUtils.InitTruststore;
19 import club.wpia.gigi.testUtils.ManagedTest;
21 public class RegisterPageTest extends ManagedTest {
25 HttpURLConnection.setFollowRedirects(false);
27 p = "&pword1=" + URLEncoder.encode(TEST_PASSWORD, "UTF-8") + "&pword2=" + URLEncoder.encode(TEST_PASSWORD, "UTF-8");
28 } catch (UnsupportedEncodingException e) {
33 public static final String p;
36 public void setUp() throws Exception {
37 clearCaches(); // We do many registers in this test suite.
40 private static String createBase() {
41 return createUniqueName() + "@email.de";
45 public void testSuccess() throws IOException, InterruptedException {
46 long uniq = System.currentTimeMillis();
47 registerUser("ab", "b", "correct" + uniq + "@email.de", TEST_PASSWORD);
48 assertSuccessfullRegMail();
50 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=";
52 String query = defaultSignup + URLEncoder.encode("correct3_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1®ional=1&radius=1&name-type=western";
53 String data = fetchStartErrorMessage(runRegister(query));
55 assertSuccessfullRegMail();
57 getMailReceiver().setEmailCheckError("400 Greylisted");
58 getMailReceiver().setApproveRegex(Pattern.compile("a"));
59 query = defaultSignup + URLEncoder.encode("correct4_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1®ional=1&radius=1";
60 data = fetchStartErrorMessage(runRegister(query));
63 assertNull(getMailReceiver().poll());
67 private void assertSuccessfullRegMail() {
68 String link = getMailReceiver().receive().extractLink();
69 assertTrue(link, link.startsWith("https://"));
73 public void testNoFname() throws IOException {
74 testFailedForm("lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
78 public void testNoLname() throws IOException {
79 testFailedForm("fname=a" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
83 public void testNoEmail() throws IOException {
84 testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910&tos_agree=1");
88 public void testNoPword() throws IOException {
89 testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910&tos_agree=1");
93 public void testDiffPword() throws IOException {
94 testFailedForm("fname=a&lname=b" + createBase() + "2&day=1&month=1&year=1910&tos_agree=1");
98 public void testNoDay() throws IOException {
99 testFailedForm("fname=a&lname=b" + createBase() + "&month=1&year=1910&tos_agree=1");
103 public void testNoMonth() throws IOException {
104 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&year=1910&tos_agree=1");
108 public void testNoYear() throws IOException {
109 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&tos_agree=1");
113 public void testInvDay() throws IOException {
114 testFailedForm("fname=a&lname=b" + createBase() + "&day=40&month=1&year=1910&tos_agree=1");
115 testFailedForm("fname=a&lname=b" + createBase() + "&day=0&month=1&year=1910&tos_agree=1");
116 testFailedForm("fname=a&lname=b" + createBase() + "&day=-1&month=1&year=1910&tos_agree=1");
117 testFailedForm("fname=a&lname=b" + createBase() + "&day=a&month=1&year=1910&tos_agree=1");
121 public void testInvMonth() throws IOException {
122 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=20&year=1910&tos_agree=1");
123 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=0&year=1910&tos_agree=1");
124 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=-1&year=1910&tos_agree=1");
125 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=a&year=1910&tos_agree=1");
129 public void testInvYear() throws IOException {
130 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=0&tos_agree=1");
131 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=100&tos_agree=1");
132 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=a&tos_agree=1");
133 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=-1&tos_agree=1");
137 public void testNoAgree() throws IOException {
138 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=a");
142 public void testTooYoung() throws IOException {
143 Calendar c = GregorianCalendar.getInstance();
144 c.add(Calendar.YEAR, -User.MINIMUM_AGE + 2);
145 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");
149 public void testTooOld() throws IOException {
150 Calendar c = GregorianCalendar.getInstance();
151 c.add(Calendar.YEAR, -User.MAXIMUM_PLAUSIBLE_AGE);
152 c.add(Calendar.DAY_OF_MONTH, -1);
153 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");
157 public void testDataStays() throws IOException {
158 long uniq = System.currentTimeMillis();
159 String run = runRegister("fname=fn" + uniq + "&lname=ln" + uniq + "&email=ma" + uniq + "@example.com&pword1=pas" + uniq + "&pword2=pas2" + uniq + "&day=28&month=10&year=1950");
160 assertThat(run, containsString("fn" + uniq));
161 assertThat(run, containsString("ln" + uniq));
162 assertThat(run, containsString("ma" + uniq + "@example.com"));
163 assertThat(run, not(containsString("pas" + uniq)));
164 assertThat(run, not(containsString("pas2" + uniq)));
166 assertThat(run, containsString("name=\"year\" value=\"1950\""));
168 assertThat(run, containsString("<option value='10' selected=\"selected\">O"));
170 assertThat(run, containsString("<option selected=\"selected\">28</option>"));
174 public void testCheckboxesStay() throws IOException {
175 String run2 = runRegister("general=1&country=a®ional=1&radius=0");
176 assertThat(run2, containsString("name=\"general\" value=\"1\" checked=\"checked\">"));
177 assertThat(run2, containsString("name=\"country\" value=\"1\">"));
178 assertThat(run2, containsString("name=\"regional\" value=\"1\" checked=\"checked\">"));
179 assertThat(run2, containsString("name=\"radius\" value=\"1\">"));
180 run2 = runRegister("general=0&country=1&radius=1");
181 assertThat(run2, containsString("name=\"general\" value=\"1\">"));
182 assertThat(run2, containsString("name=\"country\" value=\"1\" checked=\"checked\">"));
183 assertThat(run2, containsString("name=\"regional\" value=\"1\">"));
184 assertThat(run2, containsString("name=\"radius\" value=\"1\" checked=\"checked\">"));
188 public void testDoubleMail() throws IOException {
189 long uniq = System.currentTimeMillis();
190 registerUser("RegisterTest", "User", "testmail" + uniq + "@example.com", TEST_PASSWORD);
192 registerUser("RegisterTest", "User", "testmail" + uniq + "@example.com", TEST_PASSWORD);
193 throw new Error("Registering a user with the same email needs to fail.");
194 } catch (AssertionError e) {
200 public void testInvalidMailbox() {
201 getMailReceiver().setApproveRegex(Pattern.compile("a"));
202 long uniq = System.currentTimeMillis();
204 registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq + "@example.com", TEST_PASSWORD);
205 throw new Error("Registering a user with invalid mailbox must fail.");
206 } catch (AssertionError e) {
211 private void testFailedForm(String query) throws IOException {
212 String startError = fetchStartErrorMessage(runRegister(query));
213 assertTrue(startError, !startError.startsWith("</div>"));
217 public void testRegisterWithCountry() throws IOException, InterruptedException {
218 long uniq = System.currentTimeMillis();
219 String email = "country" + uniq + "@email.de";
221 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=";
223 String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&general=1&country=1®ional=1&radius=1&name-type=western&residenceCountry=DE";
224 String data = fetchStartErrorMessage(runRegister(query));
226 User u = User.getByEmail(email);
227 assertEquals("DE", u.getResidenceCountry().getCode());
231 public void testRegisterWithoutCountry() throws IOException, InterruptedException {
232 long uniq = System.currentTimeMillis();
233 String email = "countryno" + uniq + "@email.de";
235 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=";
237 String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&general=1&country=1®ional=1&radius=1&name-type=western&residenceCountry=invalid";
238 String data = fetchStartErrorMessage(runRegister(query));
240 User u = User.getByEmail(email);
241 assertEquals(null, u.getResidenceCountry());