]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/main/RegisterPageTest.java
add: new test cases for handling of residence country in user account
[gigi.git] / tests / org / cacert / gigi / pages / main / RegisterPageTest.java
1 package org.cacert.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.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.testUtils.InitTruststore;
16 import org.cacert.gigi.testUtils.ManagedTest;
17 import org.junit.Before;
18 import org.junit.Test;
19
20 public class RegisterPageTest extends ManagedTest {
21
22     static {
23         InitTruststore.run();
24         HttpURLConnection.setFollowRedirects(false);
25         try {
26             p = "&pword1=" + URLEncoder.encode(TEST_PASSWORD, "UTF-8") + "&pword2=" + URLEncoder.encode(TEST_PASSWORD, "UTF-8");
27         } catch (UnsupportedEncodingException e) {
28             throw new Error(e);
29         }
30     }
31
32     public static final String p;
33
34     @Before
35     public void setUp() throws Exception {
36         clearCaches(); // We do many registers in this test suite.
37     }
38
39     private static String createBase() {
40         return createUniqueName() + "@email.de";
41     }
42
43     @Test
44     public void testSuccess() throws IOException, InterruptedException {
45         long uniq = System.currentTimeMillis();
46         registerUser("ab", "b", "correct" + uniq + "@email.de", TEST_PASSWORD);
47         assertSuccessfullRegMail();
48
49         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=";
50
51         String query = defaultSignup + URLEncoder.encode("correct3_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1&regional=1&radius=1&name-type=western";
52         String data = fetchStartErrorMessage(runRegister(query));
53         assertNull(data);
54         assertSuccessfullRegMail();
55
56         getMailReceiver().setEmailCheckError("400 Greylisted");
57         getMailReceiver().setApproveRegex(Pattern.compile("a"));
58         query = defaultSignup + URLEncoder.encode("correct4_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1&regional=1&radius=1";
59         data = fetchStartErrorMessage(runRegister(query));
60         assertNotNull(data);
61
62         assertNull(getMailReceiver().poll());
63
64     }
65
66     private void assertSuccessfullRegMail() {
67         String link = getMailReceiver().receive().extractLink();
68         assertTrue(link, link.startsWith("https://"));
69     }
70
71     @Test
72     public void testNoFname() throws IOException {
73         testFailedForm("lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
74     }
75
76     @Test
77     public void testNoLname() throws IOException {
78         testFailedForm("fname=a" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
79     }
80
81     @Test
82     public void testNoEmail() throws IOException {
83         testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910&tos_agree=1");
84     }
85
86     @Test
87     public void testNoPword() throws IOException {
88         testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910&tos_agree=1");
89     }
90
91     @Test
92     public void testDiffPword() throws IOException {
93         testFailedForm("fname=a&lname=b" + createBase() + "2&day=1&month=1&year=1910&tos_agree=1");
94     }
95
96     @Test
97     public void testNoDay() throws IOException {
98         testFailedForm("fname=a&lname=b" + createBase() + "&month=1&year=1910&tos_agree=1");
99     }
100
101     @Test
102     public void testNoMonth() throws IOException {
103         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&year=1910&tos_agree=1");
104     }
105
106     @Test
107     public void testNoYear() throws IOException {
108         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&tos_agree=1");
109     }
110
111     @Test
112     public void testInvDay() throws IOException {
113         testFailedForm("fname=a&lname=b" + createBase() + "&day=40&month=1&year=1910&tos_agree=1");
114         testFailedForm("fname=a&lname=b" + createBase() + "&day=0&month=1&year=1910&tos_agree=1");
115         testFailedForm("fname=a&lname=b" + createBase() + "&day=-1&month=1&year=1910&tos_agree=1");
116         testFailedForm("fname=a&lname=b" + createBase() + "&day=a&month=1&year=1910&tos_agree=1");
117     }
118
119     @Test
120     public void testInvMonth() throws IOException {
121         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=20&year=1910&tos_agree=1");
122         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=0&year=1910&tos_agree=1");
123         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=-1&year=1910&tos_agree=1");
124         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=a&year=1910&tos_agree=1");
125     }
126
127     @Test
128     public void testInvYear() throws IOException {
129         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=0&tos_agree=1");
130         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=100&tos_agree=1");
131         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=a&tos_agree=1");
132         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=-1&tos_agree=1");
133     }
134
135     @Test
136     public void testNoAgree() throws IOException {
137         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=a");
138     }
139
140     @Test
141     public void testTooYoung() throws IOException {
142         Calendar c = GregorianCalendar.getInstance();
143         c.add(Calendar.YEAR, -User.MINIMUM_AGE + 2);
144         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");
145     }
146
147     @Test
148     public void testTooOld() throws IOException {
149         Calendar c = GregorianCalendar.getInstance();
150         c.add(Calendar.YEAR, -User.MAXIMUM_PLAUSIBLE_AGE);
151         c.add(Calendar.DAY_OF_MONTH, -1);
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");
153     }
154
155     @Test
156     public void testDataStays() throws IOException {
157         long uniq = System.currentTimeMillis();
158         String run = runRegister("fname=fn" + uniq + "&lname=ln" + uniq + "&email=ma" + uniq + "@cacert.org&pword1=pas" + uniq + "&pword2=pas2" + uniq + "&day=28&month=10&year=1950");
159         assertThat(run, containsString("fn" + uniq));
160         assertThat(run, containsString("ln" + uniq));
161         assertThat(run, containsString("ma" + uniq + "@cacert.org"));
162         assertThat(run, not(containsString("pas" + uniq)));
163         assertThat(run, not(containsString("pas2" + uniq)));
164         // test year
165         assertThat(run, containsString("name=\"year\" value=\"1950\""));
166         // test month
167         assertThat(run, containsString("<option value='10' selected=\"selected\">O"));
168         // test day
169         assertThat(run, containsString("<option selected=\"selected\">28</option>"));
170     }
171
172     @Test
173     public void testCheckboxesStay() throws IOException {
174         String run2 = runRegister("general=1&country=a&regional=1&radius=0");
175         assertThat(run2, containsString("name=\"general\" value=\"1\" checked=\"checked\">"));
176         assertThat(run2, containsString("name=\"country\" value=\"1\">"));
177         assertThat(run2, containsString("name=\"regional\" value=\"1\" checked=\"checked\">"));
178         assertThat(run2, containsString("name=\"radius\" value=\"1\">"));
179         run2 = runRegister("general=0&country=1&radius=1");
180         assertThat(run2, containsString("name=\"general\" value=\"1\">"));
181         assertThat(run2, containsString("name=\"country\" value=\"1\" checked=\"checked\">"));
182         assertThat(run2, containsString("name=\"regional\" value=\"1\">"));
183         assertThat(run2, containsString("name=\"radius\" value=\"1\" checked=\"checked\">"));
184     }
185
186     @Test
187     public void testDoubleMail() throws IOException {
188         long uniq = System.currentTimeMillis();
189         registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
190         try {
191             registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
192             throw new Error("Registering a user with the same email needs to fail.");
193         } catch (AssertionError e) {
194
195         }
196     }
197
198     @Test
199     public void testInvalidMailbox() {
200         getMailReceiver().setApproveRegex(Pattern.compile("a"));
201         long uniq = System.currentTimeMillis();
202         try {
203             registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq + "@cacert.org", TEST_PASSWORD);
204             throw new Error("Registering a user with invalid mailbox must fail.");
205         } catch (AssertionError e) {
206
207         }
208     }
209
210     private void testFailedForm(String query) throws IOException {
211         String startError = fetchStartErrorMessage(runRegister(query));
212         assertTrue(startError, !startError.startsWith("</div>"));
213     }
214
215     @Test
216     public void testRegisterWithCountry() throws IOException, InterruptedException {
217         long uniq = System.currentTimeMillis();
218         String email = "country" + uniq + "@email.de";
219
220         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=";
221
222         String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&general=1&country=1&regional=1&radius=1&name-type=western&residenceCountry=DE";
223         String data = fetchStartErrorMessage(runRegister(query));
224         assertNull(data);
225         User u = User.getByEmail(email);
226         assertEquals("DE", u.getResidenceCountry().getCountryCode());
227     }
228
229     @Test
230     public void testRegisterWithoutCountry() throws IOException, InterruptedException {
231         long uniq = System.currentTimeMillis();
232         String email = "countryno" + uniq + "@email.de";
233
234         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=";
235
236         String query = defaultSignup + URLEncoder.encode(email, "UTF-8") + "&general=1&country=1&regional=1&radius=1&name-type=western&residenceCountry=invalid";
237         String data = fetchStartErrorMessage(runRegister(query));
238         assertNull(data);
239         User u = User.getByEmail(email);
240         assertEquals(null, u.getResidenceCountry());
241     }
242 }