]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/main/RegisterPageTest.java
fix register page tests and add a new one (for the minimum-age)
[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
37     private static String createBase() {
38         return createUniqueName() + "@email.de";
39     }
40
41     @Test
42     public void testSuccess() throws IOException, InterruptedException {
43         long uniq = System.currentTimeMillis();
44         registerUser("ab", "b", "correct" + uniq + "@email.de", TEST_PASSWORD);
45         assertSuccessfullRegMail();
46
47         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=";
48
49         String query = defaultSignup + URLEncoder.encode("correct3_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1&regional=1&radius=1";
50         String data = fetchStartErrorMessage(runRegister(query));
51         assertNull(data);
52         assertSuccessfullRegMail();
53
54         getMailReciever().setEmailCheckError("400 Greylisted");
55         getMailReciever().setApproveRegex(Pattern.compile("a"));
56         query = defaultSignup + URLEncoder.encode("correct4_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1&regional=1&radius=1";
57         data = fetchStartErrorMessage(runRegister(query));
58         assertNotNull(data);
59
60         assertNull(getMailReciever().poll());
61
62     }
63
64     private void assertSuccessfullRegMail() {
65         String link = getMailReciever().receive().extractLink();
66         assertTrue(link, link.startsWith("https://"));
67     }
68
69     @Test
70     public void testNoFname() throws IOException {
71         testFailedForm("lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
72     }
73
74     @Test
75     public void testNoLname() throws IOException {
76         testFailedForm("fname=a" + createBase() + "&day=1&month=1&year=1910&tos_agree=1");
77     }
78
79     @Test
80     public void testNoEmail() throws IOException {
81         testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910&tos_agree=1");
82     }
83
84     @Test
85     public void testNoPword() throws IOException {
86         testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910&tos_agree=1");
87     }
88
89     @Test
90     public void testDiffPword() throws IOException {
91         testFailedForm("fname=a&lname=b" + createBase() + "2&day=1&month=1&year=1910&tos_agree=1");
92     }
93
94     @Test
95     public void testNoDay() throws IOException {
96         testFailedForm("fname=a&lname=b" + createBase() + "&month=1&year=1910&tos_agree=1");
97     }
98
99     @Test
100     public void testNoMonth() throws IOException {
101         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&year=1910&tos_agree=1");
102     }
103
104     @Test
105     public void testNoYear() throws IOException {
106         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&tos_agree=1");
107     }
108
109     @Test
110     public void testInvDay() throws IOException {
111         testFailedForm("fname=a&lname=b" + createBase() + "&day=40&month=1&year=1910&tos_agree=1");
112         testFailedForm("fname=a&lname=b" + createBase() + "&day=0&month=1&year=1910&tos_agree=1");
113         testFailedForm("fname=a&lname=b" + createBase() + "&day=-1&month=1&year=1910&tos_agree=1");
114         testFailedForm("fname=a&lname=b" + createBase() + "&day=a&month=1&year=1910&tos_agree=1");
115     }
116
117     @Test
118     public void testInvMonth() throws IOException {
119         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=20&year=1910&tos_agree=1");
120         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=0&year=1910&tos_agree=1");
121         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=-1&year=1910&tos_agree=1");
122         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=a&year=1910&tos_agree=1");
123     }
124
125     @Test
126     public void testInvYear() throws IOException {
127         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=0&tos_agree=1");
128         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=100&tos_agree=1");
129         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=a&tos_agree=1");
130         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=-1&tos_agree=1");
131     }
132
133     @Test
134     public void testNoAgree() throws IOException {
135         testFailedForm("fname=a&lname=b" + createBase() + "&day=1&month=1&year=1910&tos_agree=a");
136     }
137
138     @Test
139     public void testTooYoung() throws IOException {
140         Calendar c = GregorianCalendar.getInstance();
141         c.add(Calendar.YEAR, -User.MINIMUM_AGE + 2);
142         testFailedForm("fname=a&lname=b&email=" + createUniqueName() + "@email.de" + p + "&day=1&month=1&year=" + c.get(Calendar.YEAR) + "&tos_agree=1");
143     }
144
145     @Test
146     public void testDataStays() throws IOException {
147         long uniq = System.currentTimeMillis();
148         String run = runRegister("fname=fn" + uniq + "&lname=ln" + uniq + "&email=ma" + uniq + "@cacert.org&pword1=pas" + uniq + "&pword2=pas2" + uniq + "&day=1&month=1&year=0");
149         assertThat(run, containsString("fn" + uniq));
150         assertThat(run, containsString("ln" + uniq));
151         assertThat(run, containsString("ma" + uniq + "@cacert.org"));
152         assertThat(run, not(containsString("pas" + uniq)));
153         assertThat(run, not(containsString("pas2" + uniq)));
154
155     }
156
157     @Test
158     public void testCheckboxesStay() throws IOException {
159         String run2 = runRegister("general=1&country=a&regional=1&radius=0");
160         assertThat(run2, containsString("name=\"general\" value=\"1\" checked=\"checked\">"));
161         assertThat(run2, containsString("name=\"country\" value=\"1\">"));
162         assertThat(run2, containsString("name=\"regional\" value=\"1\" checked=\"checked\">"));
163         assertThat(run2, containsString("name=\"radius\" value=\"1\">"));
164         run2 = runRegister("general=0&country=1&radius=1");
165         assertThat(run2, containsString("name=\"general\" value=\"1\">"));
166         assertThat(run2, containsString("name=\"country\" value=\"1\" checked=\"checked\">"));
167         assertThat(run2, containsString("name=\"regional\" value=\"1\">"));
168         assertThat(run2, containsString("name=\"radius\" value=\"1\" checked=\"checked\">"));
169     }
170
171     @Test
172     public void testDoubleMail() throws IOException {
173         long uniq = System.currentTimeMillis();
174         registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
175         try {
176             registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
177             throw new Error("Registering a user with the same email needs to fail.");
178         } catch (AssertionError e) {
179
180         }
181     }
182
183     @Test
184     public void testInvalidMailbox() {
185         getMailReciever().setApproveRegex(Pattern.compile("a"));
186         long uniq = System.currentTimeMillis();
187         try {
188             registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq + "@cacert.org", TEST_PASSWORD);
189             throw new Error("Registering a user with invalid mailbox must fail.");
190         } catch (AssertionError e) {
191
192         }
193     }
194
195     private void testFailedForm(String query) throws IOException {
196         String startError = fetchStartErrorMessage(runRegister(query));
197         assertTrue(startError, !startError.startsWith("</div>"));
198     }
199
200 }