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