add: accept data protection policy during registration
[gigi.git] / tests / club / wpia / gigi / pages / main / RegisterPageTest.java
1 package club.wpia.gigi.pages.main;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5
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;
13
14 import org.junit.Before;
15 import org.junit.Test;
16
17 import club.wpia.gigi.dbObjects.User;
18 import club.wpia.gigi.testUtils.InitTruststore;
19 import club.wpia.gigi.testUtils.ManagedTest;
20
21 public class RegisterPageTest extends ManagedTest {
22
23     static {
24         InitTruststore.run();
25         HttpURLConnection.setFollowRedirects(false);
26         try {
27             p = "&pword1=" + URLEncoder.encode(TEST_PASSWORD, "UTF-8") + "&pword2=" + URLEncoder.encode(TEST_PASSWORD, "UTF-8");
28         } catch (UnsupportedEncodingException e) {
29             throw new Error(e);
30         }
31     }
32
33     public static final String p;
34
35     @Before
36     public void setUp() throws Exception {
37         clearCaches(); // We do many registers in this test suite.
38     }
39
40     private static String createBase() {
41         return createUniqueName() + "@email.de";
42     }
43
44     @Test
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");
49
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=";
51
52         String query = defaultSignup + URLEncoder.encode("correct3_" + uniq + "@email.de", "UTF-8") + "&name-type=western";
53         String data = fetchStartErrorMessage(runRegister(query));
54         assertNull(data);
55         assertSuccessfullRegMail("correct3_" + uniq + "@email.de");
56
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));
61         assertNotNull(data);
62
63         assertNull(getMailReceiver().poll(null));
64
65     }
66
67     private void assertSuccessfullRegMail(String mail) {
68         String link = getMailReceiver().receive(mail).extractLink();
69         assertTrue(link, link.startsWith("https://"));
70     }
71
72     @Test
73     public void testNoFname() throws IOException {
74         testFailedForm("lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=1&dp_agree=1");
75     }
76
77     @Test
78     public void testNoLname() throws IOException {
79         testFailedForm("fname=a" + createBase() + "&day=1&month=1&year=1910&tos_agree=1&dp_agree=1");
80     }
81
82     @Test
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     }
86
87     @Test
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");
90     }
91
92     @Test
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");
95     }
96
97     @Test
98     public void testNoDay() throws IOException {
99         testFailedForm("fname=a&lname=b" + createBase() + "&month=1&year=1910&tos_agree=1&dp_agree=1");
100     }
101
102     @Test
103     public void testNoMonth() throws IOException {
104         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&year=1910&tos_agree=1&dp_agree=1");
105     }
106
107     @Test
108     public void testNoYear() throws IOException {
109         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&tos_agree=1&dp_agree=1");
110     }
111
112     @Test
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");
118     }
119
120     @Test
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");
126     }
127
128     @Test
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");
134     }
135
136     @Test
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");
140     }
141
142     @Test
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");
146     }
147
148     @Test
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");
153     }
154
155     @Test
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");
161     }
162
163     @Test
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)));
172         // test year
173         assertThat(run, containsString("name=\"year\" value=\"1950\""));
174         // test month
175         assertThat(run, containsString("<option value='10' selected=\"selected\">O"));
176         // test day
177         assertThat(run, containsString("<option selected=\"selected\">28</option>"));
178     }
179
180     @Test
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");
185         try {
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) {
189
190         }
191     }
192
193     @Test
194     public void testInvalidMailbox() {
195         getMailReceiver().setApproveRegex(Pattern.compile("a"));
196         long uniq = System.currentTimeMillis();
197         try {
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) {
201
202         }
203     }
204
205     private void testFailedForm(String query) throws IOException {
206         String startError = fetchStartErrorMessage(runRegister(query));
207         assertTrue(startError, !startError.startsWith("</div>"));
208     }
209
210     @Test
211     public void testRegisterWithCountry() throws IOException, InterruptedException {
212         long uniq = System.currentTimeMillis();
213         String email = "country" + uniq + "@email.de";
214
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=";
216
217         String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&name-type=western&residenceCountry=DE";
218         String data = fetchStartErrorMessage(runRegister(query));
219         assertNull(data);
220         User u = User.getByEmail(email);
221         assertEquals("DE", u.getResidenceCountry().getCode());
222         getMailReceiver().receive(email);
223     }
224
225     @Test
226     public void testRegisterWithoutCountry() throws IOException, InterruptedException {
227         long uniq = System.currentTimeMillis();
228         String email = "countryno" + uniq + "@email.de";
229
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=";
231
232         String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&name-type=western&residenceCountry=invalid";
233         String data = fetchStartErrorMessage(runRegister(query));
234         assertNull(data);
235         User u = User.getByEmail(email);
236         assertEquals(null, u.getResidenceCountry());
237         getMailReceiver().receive(email);
238     }
239 }