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");
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&dp_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&dp_agree=1");
98 public void testNoDay() throws IOException {
99 testFailedForm("fname=a&lname=b" + createBase() + "&month=1&year=1910&tos_agree=1&dp_agree=1");
103 public void testNoMonth() throws IOException {
104 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&year=1910&tos_agree=1&dp_agree=1");
108 public void testNoYear() throws IOException {
109 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&tos_agree=1&dp_agree=1");
113 public void testInvDay() throws IOException {
114 testFailedForm("fname=a&lname=b" + createBase() + "&day=40&month=1&year=1910&tos_agree=1&dp_agree=1");
115 testFailedForm("fname=a&lname=b" + createBase() + "&day=0&month=1&year=1910&tos_agree=1&dp_agree=1");
116 testFailedForm("fname=a&lname=b" + createBase() + "&day=-1&month=1&year=1910&tos_agree=1&dp_agree=1");
117 testFailedForm("fname=a&lname=b" + createBase() + "&day=a&month=1&year=1910&tos_agree=1&dp_agree=1");
121 public void testInvMonth() throws IOException {
122 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=20&year=1910&tos_agree=1&dp_agree=1");
123 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=0&year=1910&tos_agree=1&dp_agree=1");
124 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=-1&year=1910&tos_agree=1&dp_agree=1");
125 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=a&year=1910&tos_agree=1&dp_agree=1");
129 public void testInvYear() throws IOException {
130 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=0&tos_agree=1&dp_agree=1");
131 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=100&tos_agree=1&dp_agree=1");
132 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=a&tos_agree=1&dp_agree=1");
133 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=-1&tos_agree=1&dp_agree=1");
137 public void testNoTosAgree() throws IOException {
138 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=a&dp_agree=1");
139 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&dp_agree=1");
143 public void testNoDPAgree() throws IOException {
144 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=1&dp_agree=a");
145 testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
149 public void testTooYoung() throws IOException {
150 Calendar c = GregorianCalendar.getInstance();
151 c.add(Calendar.YEAR, -User.MINIMUM_AGE + 2);
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&dp_agree=1");
156 public void testTooOld() throws IOException {
157 Calendar c = GregorianCalendar.getInstance();
158 c.add(Calendar.YEAR, -User.MAXIMUM_PLAUSIBLE_AGE);
159 c.add(Calendar.DAY_OF_MONTH, -1);
160 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");
164 public void testDataStays() throws IOException {
165 long uniq = System.currentTimeMillis();
166 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");
167 assertThat(run, containsString("fn" + uniq));
168 assertThat(run, containsString("ln" + uniq));
169 assertThat(run, containsString("ma" + uniq + "@example.com"));
170 assertThat(run, not(containsString("pas" + uniq)));
171 assertThat(run, not(containsString("pas2" + uniq)));
173 assertThat(run, containsString("name=\"year\" value=\"1950\""));
175 assertThat(run, containsString("<option value='10' selected=\"selected\">O"));
177 assertThat(run, containsString("<option selected=\"selected\">28</option>"));
181 public void testDoubleMail() throws IOException {
182 long uniq = System.currentTimeMillis();
183 registerUser("RegisterTest", "User", "testmail" + uniq + "@example.com", TEST_PASSWORD);
184 getMailReceiver().receive("testmail" + uniq + "@example.com");
186 registerUser("RegisterTest", "User", "testmail" + uniq + "@example.com", TEST_PASSWORD);
187 throw new Error("Registering a user with the same email needs to fail.");
188 } catch (AssertionError e) {
194 public void testInvalidMailbox() {
195 getMailReceiver().setApproveRegex(Pattern.compile("a"));
196 long uniq = System.currentTimeMillis();
198 registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq + "@example.com", TEST_PASSWORD);
199 throw new Error("Registering a user with invalid mailbox must fail.");
200 } catch (AssertionError e) {
205 private void testFailedForm(String query) throws IOException {
206 String startError = fetchStartErrorMessage(runRegister(query));
207 assertTrue(startError, !startError.startsWith("</div>"));
211 public void testRegisterWithCountry() throws IOException, InterruptedException {
212 long uniq = System.currentTimeMillis();
213 String email = "country" + uniq + "@email.de";
215 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=";
217 String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&name-type=western&residenceCountry=DE";
218 String data = fetchStartErrorMessage(runRegister(query));
220 User u = User.getByEmail(email);
221 assertEquals("DE", u.getResidenceCountry().getCode());
222 getMailReceiver().receive(email);
226 public void testRegisterWithoutCountry() throws IOException, InterruptedException {
227 long uniq = System.currentTimeMillis();
228 String email = "countryno" + uniq + "@email.de";
230 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=";
232 String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&name-type=western&residenceCountry=invalid";
233 String data = fetchStartErrorMessage(runRegister(query));
235 User u = User.getByEmail(email);
236 assertEquals(null, u.getResidenceCountry());
237 getMailReceiver().receive(email);