]> WPIA git - gigi.git/blobdiff - tests/org/cacert/gigi/pages/main/RegisterPageTest.java
Move 2 test util classes to the "testUtils" package
[gigi.git] / tests / org / cacert / gigi / pages / main / RegisterPageTest.java
index 96395a01caffd7807e05c04af4ef19cc1e3d32b0..06787248d311a6f3639691a4e7c2726dda397bd7 100644 (file)
@@ -3,28 +3,17 @@ package org.cacert.gigi.pages.main;
 import static org.junit.Assert.*;
 
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLEncoder;
+import java.util.regex.Pattern;
 
-import org.cacert.gigi.IOUtils;
-import org.cacert.gigi.InitTruststore;
+import org.cacert.gigi.testUtils.InitTruststore;
+import org.cacert.gigi.testUtils.ManagedTest;
+import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
-public class RegisterPageTest {
-       private static final URL registerService;
+public class RegisterPageTest extends ManagedTest {
        static {
-               URL u = null;
-               try {
-                       u = new URL("https://localhost/register");
-               } catch (MalformedURLException e) {
-                       e.printStackTrace();
-               }
-               registerService = u;
                InitTruststore.run();
                HttpURLConnection.setFollowRedirects(false);
        }
@@ -32,61 +21,72 @@ public class RegisterPageTest {
        @Before
        public void setUp() throws Exception {
        }
-
+       @Test
+       public void testSuccess() throws IOException {
+               long uniq = System.currentTimeMillis();
+               registerUser("ab", "b", "correct" + uniq + "@email.de", "ap12UI.'");
+               TestMail tm = waitForMail();
+               String link = tm.extractLink();
+               assertTrue(link, link.startsWith("http://"));
+       }
        @Test
        public void testNoFname() throws IOException {
-               testFailedForm("lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910");
+               testFailedForm("lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
        }
        @Test
        public void testNoLname() throws IOException {
-               testFailedForm("fname=a&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910");
+               testFailedForm("fname=a&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
        }
        @Test
        public void testNoEmail() throws IOException {
-               testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910");
+               testFailedForm("fname=a&lname=b&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
        }
 
        @Test
        public void testNoPword() throws IOException {
-               testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910");
+               testFailedForm("fname=a&lname=b&email=e&pword2=ap&day=1&month=1&year=1910&cca_agree=1");
        }
 
        @Test
        public void testDiffPword() throws IOException {
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap2&day=1&month=1&year=1910");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap2&day=1&month=1&year=1910&cca_agree=1");
        }
 
        @Test
        public void testNoDay() throws IOException {
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&month=1&year=1910");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&month=1&year=1910&cca_agree=1");
        }
        @Test
        public void testNoMonth() throws IOException {
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&year=1910");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&year=1910&cca_agree=1");
        }
        @Test
        public void testNoYear() throws IOException {
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&cca_agree=1");
        }
        @Test
        public void testInvDay() throws IOException {
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=40&month=1&year=1910");
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=0&month=1&year=1910");
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=a&month=1&year=1910");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=40&month=1&year=1910&cca_agree=1");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=0&month=1&year=1910&cca_agree=1");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=a&month=1&year=1910&cca_agree=1");
        }
        @Test
        public void testInvMonth() throws IOException {
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=20&year=1910");
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=0&year=1910");
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=-1&year=1910");
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=a&year=1910");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=20&year=1910&cca_agree=1");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=0&year=1910&cca_agree=1");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=-1&year=1910&cca_agree=1");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=a&year=1910&cca_agree=1");
        }
        @Test
        public void testInvYear() throws IOException {
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=0");
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=100");
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=a");
-               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=-1");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=0&cca_agree=1");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=100&cca_agree=1");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=a&cca_agree=1");
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=-1&cca_agree=1");
+       }
+       @Test
+       public void testNoAgree() throws IOException {
+               testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&day=1&month=1&year=1910&cca_agree=a");
        }
 
        @Test
@@ -121,46 +121,36 @@ public class RegisterPageTest {
                                .contains("name=\"radius\" value=\"1\" checked=\"checked\">"));
        }
 
-       @Ignore
        @Test
        public void testDoubleMail() throws IOException {
                long uniq = System.currentTimeMillis();
                registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org",
-                               "registerPW");
-               registerUser("RegisterTest", "User", "testmail" + uniq + "@cacert.org",
-                               "registerPW");
-       }
+                               "registerPW'1");
+               try {
+                       registerUser("RegisterTest", "User", "testmail" + uniq
+                                       + "@cacert.org", "registerPW");
+                       throw new Error(
+                                       "Registering a user with the same email needs to fail.");
+               } catch (AssertionError e) {
 
-       private static void testFailedForm(String query) throws IOException {
-               String d = runRegister(query);
-               String formFail = "<div class='formError'>";
-               int idx = d.indexOf(formFail);
-               assertNotEquals(-1, idx);
-               String startError = d.substring(idx + formFail.length(), idx + 100)
-                               .trim();
-               assertTrue(startError, !startError.startsWith("</div>"));
+               }
        }
-
-       public static void registerUser(String firstName, String lastName,
-                       String email, String password) {
+       @Test
+       public void testInvalidMailbox() {
+               getMailReciever().setApproveRegex(Pattern.compile("a"));
+               long uniq = System.currentTimeMillis();
                try {
-                       String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
-                                       + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
-                                       + "&email=" + URLEncoder.encode(firstName, "UTF-8")
-                                       + "&pword1=" + URLEncoder.encode(password, "UTF-8")
-                                       + "&pword2=" + URLEncoder.encode(password, "UTF-8")
-                                       + "&day=1&month=1&year=1910";
-               } catch (UnsupportedEncodingException e) {
-                       e.printStackTrace();
+                       registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq
+                                       + "@cacert.org", "registerPW");
+                       throw new Error(
+                                       "Registering a user with invalid mailbox must fail.");
+               } catch (AssertionError e) {
+
                }
        }
-
-       private static String runRegister(String param) throws IOException {
-               HttpURLConnection uc = (HttpURLConnection) registerService
-                               .openConnection();
-               uc.setDoOutput(true);
-               uc.getOutputStream().write(param.getBytes());
-               String d = IOUtils.readURL(uc);
-               return d;
+       private void testFailedForm(String query) throws IOException {
+               String startError = fetchStartErrorMessage(runRegister(query));
+               assertTrue(startError, !startError.startsWith("</div>"));
        }
+
 }