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("correct" + uniq + "@email.de");
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&dp_agree=1&mname=mn&suffix=sf&email=";
52 String query = defaultSignup + URLEncoder.encode("correct3_" + uniq + "@email.de", "UTF-8") + "&name-type=western";
53 String data = fetchStartErrorMessage(runRegister(query));
55 assertSuccessfullRegMail("correct3_" + uniq + "@email.de");
57 getMailReceiver().setEmailCheckError("400 Greylisted");
58 getMailReceiver().setApproveRegex(Pattern.compile("a"));
59 query = defaultSignup + URLEncoder.encode("correct4_" + uniq + "@email.de", "UTF-8");
60 data = fetchStartErrorMessage(runRegister(query));
63 assertNull(getMailReceiver().poll(null));
67 private void assertSuccessfullRegMail(String mail) {
68 String link = getMailReceiver().receive(mail).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&dp_agree=1");
78 public void testNoLname() throws IOException {
79 testFailedForm("fname=a" + createBase() + "&day=1&month=1&year=1910&tos_agree=1&dp_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&dp_agree=1");
85 testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&tos_agree=1&dp_agree=1");
86 testFailedForm("fname=a&lname=b&email=e@&pword1=ap&pword2=ap&day=1&month=1&year=1910&tos_agree=1&dp_agree=1");
87 testFailedForm("fname=a&lname=b&email=@d.ef&pword1=ap&pword2=ap&day=1&month=1&year=1910&tos_agree=1&dp_agree=1");
91 public void testNoPword() throws IOException {
92 testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910&tos_agree=1&dp_agree=1");
96 public void testDiffPword() throws IOException {
97 testFailedForm("fname=a&lname=b" + createBase() + "2&day=1&month=1&year=1910&tos_agree=1&dp_agree=1");
101 public void testNoDay() throws IOException {
102 testFailedForm("fname=a&lname=b" + createBase() + "&month=1&year=1910&tos_agree=1&dp_agree=1");
106 public void testNoMonth() throws IOException {
107 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&year=1910&tos_agree=1&dp_agree=1");
111 public void testNoYear() throws IOException {
112 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&tos_agree=1&dp_agree=1");
116 public void testInvDay() throws IOException {
117 testFailedForm("fname=a&lname=b" + createBase() + "&day=40&month=1&year=1910&tos_agree=1&dp_agree=1");
118 testFailedForm("fname=a&lname=b" + createBase() + "&day=0&month=1&year=1910&tos_agree=1&dp_agree=1");
119 testFailedForm("fname=a&lname=b" + createBase() + "&day=-1&month=1&year=1910&tos_agree=1&dp_agree=1");
120 testFailedForm("fname=a&lname=b" + createBase() + "&day=a&month=1&year=1910&tos_agree=1&dp_agree=1");
124 public void testInvMonth() throws IOException {
125 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=20&year=1910&tos_agree=1&dp_agree=1");
126 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=0&year=1910&tos_agree=1&dp_agree=1");
127 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=-1&year=1910&tos_agree=1&dp_agree=1");
128 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=a&year=1910&tos_agree=1&dp_agree=1");
132 public void testInvYear() throws IOException {
133 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=0&tos_agree=1&dp_agree=1");
134 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=100&tos_agree=1&dp_agree=1");
135 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=a&tos_agree=1&dp_agree=1");
136 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=-1&tos_agree=1&dp_agree=1");
140 public void testNoTosAgree() throws IOException {
141 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=a&dp_agree=1");
142 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&dp_agree=1");
146 public void testNoDPAgree() throws IOException {
147 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=1&dp_agree=a");
148 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
152 public void testTooYoung() throws IOException {
153 Calendar c = GregorianCalendar.getInstance();
154 c.add(Calendar.YEAR, -User.MINIMUM_AGE + 2);
155 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&dp_agree=1");
159 public void testTooOld() throws IOException {
160 Calendar c = GregorianCalendar.getInstance();
161 c.add(Calendar.YEAR, -User.MAXIMUM_PLAUSIBLE_AGE);
162 c.add(Calendar.DAY_OF_MONTH, -1);
163 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&dp_agree=1");
167 public void testDataStays() throws IOException {
168 long uniq = System.currentTimeMillis();
169 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");
170 assertThat(run, containsString("fn" + uniq));
171 assertThat(run, containsString("ln" + uniq));
172 assertThat(run, containsString("ma" + uniq + "@example.com"));
173 assertThat(run, not(containsString("pas" + uniq)));
174 assertThat(run, not(containsString("pas2" + uniq)));
176 assertThat(run, containsString("name=\"year\" value=\"1950\""));
178 assertThat(run, containsString("<option value='10' selected=\"selected\">O"));
180 assertThat(run, containsString("<option selected=\"selected\">28</option>"));
184 public void testDoubleMail() throws IOException {
185 long uniq = System.currentTimeMillis();
186 registerUser("RegisterTest", "User", "testmail" + uniq + "@example.com", TEST_PASSWORD);
187 getMailReceiver().receive("testmail" + uniq + "@example.com");
189 registerUser("RegisterTest", "User", "testmail" + uniq + "@example.com", TEST_PASSWORD);
190 throw new Error("Registering a user with the same email needs to fail.");
191 } catch (AssertionError e) {
197 public void testInvalidMailbox() {
198 getMailReceiver().setApproveRegex(Pattern.compile("a"));
199 long uniq = System.currentTimeMillis();
201 registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq + "@example.com", TEST_PASSWORD);
202 throw new Error("Registering a user with invalid mailbox must fail.");
203 } catch (AssertionError e) {
208 private void testFailedForm(String query) throws IOException {
209 String startError = fetchStartErrorMessage(runRegister(query));
210 assertTrue(startError, !startError.startsWith("</div>"));
214 public void testRegisterWithCountry() throws IOException, InterruptedException {
215 long uniq = System.currentTimeMillis();
216 String email = "country" + uniq + "@email.de";
218 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&dp_agree=1&mname=mn&suffix=sf&email=";
220 String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&name-type=western&residenceCountry=DE";
221 String data = fetchStartErrorMessage(runRegister(query));
223 User u = User.getByEmail(email);
224 assertEquals("DE", u.getResidenceCountry().getCode());
225 getMailReceiver().receive(email);
229 public void testRegisterWithoutCountry() throws IOException, InterruptedException {
230 long uniq = System.currentTimeMillis();
231 String email = "countryno" + uniq + "@email.de";
233 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&dp_agree=1&mname=mn&suffix=sf&email=";
235 String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&name-type=western&residenceCountry=invalid";
236 String data = fetchStartErrorMessage(runRegister(query));
238 User u = User.getByEmail(email);
239 assertEquals(null, u.getResidenceCountry());
240 getMailReceiver().receive(email);