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