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