]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/pages/main/RegisterPageTest.java
c21d7a263c84eaa29d87d4babb48ac9de781f272
[gigi.git] / tests / org / cacert / gigi / pages / main / RegisterPageTest.java
1 package org.cacert.gigi.pages.main;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.net.HttpURLConnection;
7 import java.net.URLEncoder;
8 import java.util.regex.Pattern;
9
10 import org.cacert.gigi.testUtils.InitTruststore;
11 import org.cacert.gigi.testUtils.ManagedTest;
12 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
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().recieve());
46
47     }
48
49     private void assertSuccessfullRegMail() {
50         TestMail tm = waitForMail();
51         String link = tm.extractLink();
52         assertTrue(link, link.startsWith("https://"));
53     }
54
55     @Test
56     public void testNoFname() throws IOException {
57         testFailedForm("lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
58     }
59
60     @Test
61     public void testNoLname() throws IOException {
62         testFailedForm("fname=a&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
63     }
64
65     @Test
66     public void testNoEmail() throws IOException {
67         testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
68     }
69
70     @Test
71     public void testNoPword() throws IOException {
72         testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
73     }
74
75     @Test
76     public void testDiffPword() throws IOException {
77         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap2&day=1&month=1&year=1910&cca_agree=1");
78     }
79
80     @Test
81     public void testNoDay() throws IOException {
82         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&month=1&year=1910&cca_agree=1");
83     }
84
85     @Test
86     public void testNoMonth() throws IOException {
87         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&year=1910&cca_agree=1");
88     }
89
90     @Test
91     public void testNoYear() throws IOException {
92         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&cca_agree=1");
93     }
94
95     @Test
96     public void testInvDay() throws IOException {
97         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=40&month=1&year=1910&cca_agree=1");
98         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=0&month=1&year=1910&cca_agree=1");
99         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=a&month=1&year=1910&cca_agree=1");
100     }
101
102     @Test
103     public void testInvMonth() throws IOException {
104         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=20&year=1910&cca_agree=1");
105         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=0&year=1910&cca_agree=1");
106         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=-1&year=1910&cca_agree=1");
107         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=a&year=1910&cca_agree=1");
108     }
109
110     @Test
111     public void testInvYear() throws IOException {
112         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=0&cca_agree=1");
113         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=100&cca_agree=1");
114         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=a&cca_agree=1");
115         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=-1&cca_agree=1");
116     }
117
118     @Test
119     public void testNoAgree() throws IOException {
120         testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=a");
121     }
122
123     @Test
124     public void testDataStays() throws IOException {
125         long uniq = System.currentTimeMillis();
126         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");
127         assertTrue(run.contains("fn" + uniq));
128         assertTrue(run.contains("ln" + uniq));
129         assertTrue(run.contains("ma" + uniq + "@cacert.org"));
130         assertTrue( !run.contains("pas" + uniq));
131         assertTrue( !run.contains("pas2" + uniq));
132
133     }
134
135     @Test
136     public void testCheckboxesStay() throws IOException {
137         String run2 = runRegister("general=1&country=a&regional=1&radius=0");
138         assertTrue(run2.contains("name=\"general\" value=\"1\" checked=\"checked\">"));
139         assertTrue(run2.contains("name=\"country\" value=\"1\">"));
140         assertTrue(run2.contains("name=\"regional\" value=\"1\" checked=\"checked\">"));
141         assertTrue(run2.contains("name=\"radius\" value=\"1\">"));
142         run2 = runRegister("general=0&country=1&radius=1");
143         assertTrue(run2.contains("name=\"general\" value=\"1\">"));
144         assertTrue(run2.contains("name=\"country\" value=\"1\" checked=\"checked\">"));
145         assertTrue(run2.contains("name=\"regional\" value=\"1\">"));
146         assertTrue(run2.contains("name=\"radius\" value=\"1\" checked=\"checked\">"));
147     }
148
149     @Test
150     public void testDoubleMail() throws IOException {
151         long uniq = System.currentTimeMillis();
152         registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
153         try {
154             registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org", TEST_PASSWORD);
155             throw new Error("Registering a user with the same email needs to fail.");
156         } catch (AssertionError e) {
157
158         }
159     }
160
161     @Test
162     public void testInvalidMailbox() {
163         getMailReciever().setApproveRegex(Pattern.compile("a"));
164         long uniq = System.currentTimeMillis();
165         try {
166             registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq + "@cacert.org", TEST_PASSWORD);
167             throw new Error("Registering a user with invalid mailbox must fail.");
168         } catch (AssertionError e) {
169
170         }
171     }
172
173     private void testFailedForm(String query) throws IOException {
174         String startError = fetchStartErrorMessage(runRegister(query));
175         assertTrue(startError, !startError.startsWith("</div>"));
176     }
177
178 }