]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/main/RegisterPageTest.java
695598ae7d0225d03473570bb65acd3a1d58c034
[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.net.HttpURLConnection;
8 import java.net.URLEncoder;
9 import java.util.regex.Pattern;
10
11 import org.cacert.gigi.testUtils.InitTruststore;
12 import org.cacert.gigi.testUtils.ManagedTest;
13 import org.junit.Before;
14 import org.junit.Test;
15
16 public class RegisterPageTest extends ManagedTest {
17
18     static {
19         InitTruststore.run();
20         HttpURLConnection.setFollowRedirects(false);
21     }
22
23     @Before
24     public void setUp() throws Exception {}
25
26     @Test
27     public void testSuccess() throws IOException, InterruptedException {
28         long uniq = System.currentTimeMillis();
29         registerUser("ab", "b", "correct" + uniq + "@email.de", TEST_PASSWORD);
30         assertSuccessfullRegMail();
31
32         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&cca_agree=1&mname=mn&suffix=sf&email=";
33
34         String query = defaultSignup + URLEncoder.encode("correct3_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1&regional=1&radius=1";
35         String data = fetchStartErrorMessage(runRegister(query));
36         assertNull(data);
37         assertSuccessfullRegMail();
38
39         getMailReciever().setEmailCheckError("400 Greylisted");
40         getMailReciever().setApproveRegex(Pattern.compile("a"));
41         query = defaultSignup + URLEncoder.encode("correct4_" + uniq + "@email.de", "UTF-8") + "&general=1&country=1&regional=1&radius=1";
42         data = fetchStartErrorMessage(runRegister(query));
43         assertNotNull(data);
44
45         assertNull(getMailReciever().poll());
46
47     }
48
49     private void assertSuccessfullRegMail() {
50         String link = getMailReciever().receive().extractLink();
51         assertTrue(link, link.startsWith("https://"));
52     }
53
54     @Test
55     public void testNoFname() throws IOException {
56         testFailedForm("lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
57     }
58
59     @Test
60     public void testNoLname() throws IOException {
61         testFailedForm("fname=a&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
62     }
63
64     @Test
65     public void testNoEmail() throws IOException {
66         testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
67     }
68
69     @Test
70     public void testNoPword() throws IOException {
71         testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
72     }
73
74     @Test
75     public void testDiffPword() throws IOException {
76         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap2&day=1&month=1&year=1910&cca_agree=1");
77     }
78
79     @Test
80     public void testNoDay() throws IOException {
81         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&month=1&year=1910&cca_agree=1");
82     }
83
84     @Test
85     public void testNoMonth() throws IOException {
86         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&year=1910&cca_agree=1");
87     }
88
89     @Test
90     public void testNoYear() throws IOException {
91         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&cca_agree=1");
92     }
93
94     @Test
95     public void testInvDay() throws IOException {
96         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=40&month=1&year=1910&cca_agree=1");
97         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=0&month=1&year=1910&cca_agree=1");
98         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=a&month=1&year=1910&cca_agree=1");
99     }
100
101     @Test
102     public void testInvMonth() throws IOException {
103         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=20&year=1910&cca_agree=1");
104         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=0&year=1910&cca_agree=1");
105         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=-1&year=1910&cca_agree=1");
106         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=a&year=1910&cca_agree=1");
107     }
108
109     @Test
110     public void testInvYear() throws IOException {
111         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=0&cca_agree=1");
112         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=100&cca_agree=1");
113         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=a&cca_agree=1");
114         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=-1&cca_agree=1");
115     }
116
117     @Test
118     public void testNoAgree() throws IOException {
119         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=a");
120     }
121
122     @Test
123     public void testDataStays() throws IOException {
124         long uniq = System.currentTimeMillis();
125         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");
126         assertThat(run, containsString("fn" + uniq));
127         assertThat(run, containsString("ln" + uniq));
128         assertThat(run, containsString("ma" + uniq + "@cacert.org"));
129         assertThat(run, not(containsString("pas" + uniq)));
130         assertThat(run, not(containsString("pas2" + uniq)));
131
132     }
133
134     @Test
135     public void testCheckboxesStay() throws IOException {
136         String run2 = runRegister("general=1&country=a&regional=1&radius=0");
137         assertThat(run2, containsString("name=\"general\" value=\"1\" checked=\"checked\">"));
138         assertThat(run2, containsString("name=\"country\" value=\"1\">"));
139         assertThat(run2, containsString("name=\"regional\" value=\"1\" checked=\"checked\">"));
140         assertThat(run2, containsString("name=\"radius\" value=\"1\">"));
141         run2 = runRegister("general=0&country=1&radius=1");
142         assertThat(run2, containsString("name=\"general\" value=\"1\">"));
143         assertThat(run2, containsString("name=\"country\" value=\"1\" checked=\"checked\">"));
144         assertThat(run2, containsString("name=\"regional\" value=\"1\">"));
145         assertThat(run2, containsString("name=\"radius\" value=\"1\" checked=\"checked\">"));
146     }
147
148     @Test
149     public void testDoubleMail() throws IOException {
150         long uniq = System.currentTimeMillis();
151         registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
152         try {
153             registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
154             throw new Error("Registering a user with the same email needs to fail.");
155         } catch (AssertionError e) {
156
157         }
158     }
159
160     @Test
161     public void testInvalidMailbox() {
162         getMailReciever().setApproveRegex(Pattern.compile("a"));
163         long uniq = System.currentTimeMillis();
164         try {
165             registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq + "@cacert.org", TEST_PASSWORD);
166             throw new Error("Registering a user with invalid mailbox must fail.");
167         } catch (AssertionError e) {
168
169         }
170     }
171
172     private void testFailedForm(String query) throws IOException {
173         String startError = fetchStartErrorMessage(runRegister(query));
174         assertTrue(startError, !startError.startsWith("</div>"));
175     }
176
177 }