]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/main/RegisterPageTest.java
Merge "upd: remove 'browser install'"
[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         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");
88     }
89
90     @Test
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");
93     }
94
95     @Test
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");
98     }
99
100     @Test
101     public void testNoDay() throws IOException {
102         testFailedForm("fname=a&lname=b" + createBase() + "&month=1&year=1910&tos_agree=1&dp_agree=1");
103     }
104
105     @Test
106     public void testNoMonth() throws IOException {
107         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&year=1910&tos_agree=1&dp_agree=1");
108     }
109
110     @Test
111     public void testNoYear() throws IOException {
112         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&tos_agree=1&dp_agree=1");
113     }
114
115     @Test
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");
121     }
122
123     @Test
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");
129     }
130
131     @Test
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");
137     }
138
139     @Test
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");
143     }
144
145     @Test
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");
149     }
150
151     @Test
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");
156     }
157
158     @Test
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");
164     }
165
166     @Test
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)));
175         // test year
176         assertThat(run, containsString("name=\"year\" value=\"1950\""));
177         // test month
178         assertThat(run, containsString("<option value='10' selected=\"selected\">O"));
179         // test day
180         assertThat(run, containsString("<option selected=\"selected\">28</option>"));
181     }
182
183     @Test
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");
188         try {
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) {
192
193         }
194     }
195
196     @Test
197     public void testInvalidMailbox() {
198         getMailReceiver().setApproveRegex(Pattern.compile("a"));
199         long uniq = System.currentTimeMillis();
200         try {
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) {
204
205         }
206     }
207
208     private void testFailedForm(String query) throws IOException {
209         String startError = fetchStartErrorMessage(runRegister(query));
210         assertTrue(startError, !startError.startsWith("</div>"));
211     }
212
213     @Test
214     public void testRegisterWithCountry() throws IOException, InterruptedException {
215         long uniq = System.currentTimeMillis();
216         String email = "country" + uniq + "@email.de";
217
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=";
219
220         String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&name-type=western&residenceCountry=DE";
221         String data = fetchStartErrorMessage(runRegister(query));
222         assertNull(data);
223         User u = User.getByEmail(email);
224         assertEquals("DE", u.getResidenceCountry().getCode());
225         getMailReceiver().receive(email);
226     }
227
228     @Test
229     public void testRegisterWithoutCountry() throws IOException, InterruptedException {
230         long uniq = System.currentTimeMillis();
231         String email = "countryno" + uniq + "@email.de";
232
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=";
234
235         String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&name-type=western&residenceCountry=invalid";
236         String data = fetchStartErrorMessage(runRegister(query));
237         assertNull(data);
238         User u = User.getByEmail(email);
239         assertEquals(null, u.getResidenceCountry());
240         getMailReceiver().receive(email);
241     }
242 }