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