]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/pages/main/RegisterPageTest.java
upd: remove alert settings from register process
[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") + "&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");
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 testDoubleMail() throws IOException {
175         long uniq = System.currentTimeMillis();
176         registerUser("RegisterTest", "User", "testmail" + uniq + "@example.com", TEST_PASSWORD);
177         getMailReceiver().receive("testmail" + uniq + "@example.com");
178         try {
179             registerUser("RegisterTest", "User", "testmail" + uniq + "@example.com", TEST_PASSWORD);
180             throw new Error("Registering a user with the same email needs to fail.");
181         } catch (AssertionError e) {
182
183         }
184     }
185
186     @Test
187     public void testInvalidMailbox() {
188         getMailReceiver().setApproveRegex(Pattern.compile("a"));
189         long uniq = System.currentTimeMillis();
190         try {
191             registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq + "@example.com", TEST_PASSWORD);
192             throw new Error("Registering a user with invalid mailbox must fail.");
193         } catch (AssertionError e) {
194
195         }
196     }
197
198     private void testFailedForm(String query) throws IOException {
199         String startError = fetchStartErrorMessage(runRegister(query));
200         assertTrue(startError, !startError.startsWith("</div>"));
201     }
202
203     @Test
204     public void testRegisterWithCountry() throws IOException, InterruptedException {
205         long uniq = System.currentTimeMillis();
206         String email = "country" + uniq + "@email.de";
207
208         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=";
209
210         String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&name-type=western&residenceCountry=DE";
211         String data = fetchStartErrorMessage(runRegister(query));
212         assertNull(data);
213         User u = User.getByEmail(email);
214         assertEquals("DE", u.getResidenceCountry().getCode());
215         getMailReceiver().receive(email);
216     }
217
218     @Test
219     public void testRegisterWithoutCountry() throws IOException, InterruptedException {
220         long uniq = System.currentTimeMillis();
221         String email = "countryno" + uniq + "@email.de";
222
223         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=";
224
225         String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&name-type=western&residenceCountry=invalid";
226         String data = fetchStartErrorMessage(runRegister(query));
227         assertNull(data);
228         User u = User.getByEmail(email);
229         assertEquals(null, u.getResidenceCountry());
230         getMailReceiver().receive(email);
231     }
232 }