]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/main/RegisterPageTest.java
upd: enforce that test cases receive all mails explicitly
[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&mname=mn&suffix=sf&email=";
51
52         String query = defaultSignup + URLEncoder.encode("correct3_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1&regional=1&radius=1&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") + "&general=1&country=1&regional=1&radius=1";
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");
75     }
76
77     @Test
78     public void testNoLname() throws IOException {
79         testFailedForm("fname=a" + createBase() + "&day=1&month=1&year=1910&tos_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");
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");
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");
95     }
96
97     @Test
98     public void testNoDay() throws IOException {
99         testFailedForm("fname=a&lname=b" + createBase() + "&month=1&year=1910&tos_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");
105     }
106
107     @Test
108     public void testNoYear() throws IOException {
109         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&tos_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");
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");
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");
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");
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");
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");
134     }
135
136     @Test
137     public void testNoAgree() throws IOException {
138         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=a");
139     }
140
141     @Test
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");
146     }
147
148     @Test
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");
154     }
155
156     @Test
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)));
165         // test year
166         assertThat(run, containsString("name=\"year\" value=\"1950\""));
167         // test month
168         assertThat(run, containsString("<option value='10' selected=\"selected\">O"));
169         // test day
170         assertThat(run, containsString("<option selected=\"selected\">28</option>"));
171     }
172
173     @Test
174     public void testCheckboxesStay() throws IOException {
175         String run2 = runRegister("general=1&country=a&regional=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\">"));
185     }
186
187     @Test
188     public void testDoubleMail() throws IOException {
189         long uniq = System.currentTimeMillis();
190         registerUser("RegisterTest", "User", "testmail" + uniq + "@example.com", TEST_PASSWORD);
191         getMailReceiver().receive("testmail" + uniq + "@example.com");
192         try {
193             registerUser("RegisterTest", "User", "testmail" + uniq + "@example.com", TEST_PASSWORD);
194             throw new Error("Registering a user with the same email needs to fail.");
195         } catch (AssertionError e) {
196
197         }
198     }
199
200     @Test
201     public void testInvalidMailbox() {
202         getMailReceiver().setApproveRegex(Pattern.compile("a"));
203         long uniq = System.currentTimeMillis();
204         try {
205             registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq + "@example.com", TEST_PASSWORD);
206             throw new Error("Registering a user with invalid mailbox must fail.");
207         } catch (AssertionError e) {
208
209         }
210     }
211
212     private void testFailedForm(String query) throws IOException {
213         String startError = fetchStartErrorMessage(runRegister(query));
214         assertTrue(startError, !startError.startsWith("</div>"));
215     }
216
217     @Test
218     public void testRegisterWithCountry() throws IOException, InterruptedException {
219         long uniq = System.currentTimeMillis();
220         String email = "country" + uniq + "@email.de";
221
222         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
224         String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&general=1&country=1&regional=1&radius=1&name-type=western&residenceCountry=DE";
225         String data = fetchStartErrorMessage(runRegister(query));
226         assertNull(data);
227         User u = User.getByEmail(email);
228         assertEquals("DE", u.getResidenceCountry().getCode());
229         getMailReceiver().receive(email);
230     }
231
232     @Test
233     public void testRegisterWithoutCountry() throws IOException, InterruptedException {
234         long uniq = System.currentTimeMillis();
235         String email = "countryno" + uniq + "@email.de";
236
237         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=";
238
239         String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&general=1&country=1&regional=1&radius=1&name-type=western&residenceCountry=invalid";
240         String data = fetchStartErrorMessage(runRegister(query));
241         assertNull(data);
242         User u = User.getByEmail(email);
243         assertEquals(null, u.getResidenceCountry());
244         getMailReceiver().receive(email);
245     }
246 }