]> WPIA git - gigi.git/commitdiff
Extend test to mbox verifications
authorFelix Dörre <felix@dogcraft.de>
Wed, 25 Jun 2014 13:18:05 +0000 (15:18 +0200)
committerFelix Dörre <felix@dogcraft.de>
Wed, 25 Jun 2014 13:18:05 +0000 (15:18 +0200)
tests/org/cacert/gigi/pages/main/RegisterPageTest.java
tests/org/cacert/gigi/testUtils/ManagedTest.java
tests/org/cacert/gigi/testUtils/TestEmailReciever.java

index 51ca686735dd1a618efd87fb53ebe2bf4406fc2a..fa2e080df53474b5b7cd6d7305e4137bb0608af9 100644 (file)
@@ -3,54 +3,31 @@ 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.Matcher;
 import java.util.regex.Pattern;
 
-import org.cacert.gigi.IOUtils;
 import org.cacert.gigi.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 extends ManagedTest {
-       private final URL registerService;
        static {
                InitTruststore.run();
                HttpURLConnection.setFollowRedirects(false);
        }
 
-       public RegisterPageTest() {
-               URL u = null;
-               try {
-                       u = new URL("https://" + getServerName() + "/register");
-               } catch (MalformedURLException e) {
-                       e.printStackTrace();
-               }
-               registerService = u;
-       }
        @Before
        public void setUp() throws Exception {
        }
        @Test
        public void testSuccess() throws IOException {
-               String startError = fetchStartErrorMessage("fname=ab&lname=b&email="
-                               + URLEncoder.encode("felix+" + System.currentTimeMillis()
-                                               + "@dogcraft.de", "UTF-8")
-                               + "&pword1=ap12UI.a'&pword2=ap12UI.a'&day=1&month=1&year=1910&cca_agree=1");
-               assertTrue(startError, startError.startsWith("</div>"));
+               long uniq = System.currentTimeMillis();
+               registerUser("ab", "b", "correct" + uniq + "@email.de", "ap12UI.'");
                TestMail tm = waitForMail();
-               Pattern link = Pattern.compile("http://[^\\s]+(?=\\s)");
-               Matcher m = link.matcher(tm.getMessage());
-               m.find();
-               System.out.println(tm.getSubject());
-               System.out.println(m.group(0));
+               String link = tm.extractLink();
+               assertTrue(link, link.startsWith("http://"));
        }
        @Test
        public void testNoFname() throws IOException {
@@ -77,7 +54,6 @@ public class RegisterPageTest extends ManagedTest {
 
        @Test
        public void testNoDay() throws IOException {
-               System.out.println(registerService);
                testFailedForm("fname=a&lname=b&email=e&pword1=ap&pword2=ap&month=1&year=1910&cca_agree=1");
        }
        @Test
@@ -145,12 +121,11 @@ public class RegisterPageTest extends ManagedTest {
                                .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");
+                               "registerPW'1");
                try {
                        registerUser("RegisterTest", "User", "testmail" + uniq
                                        + "@cacert.org", "registerPW");
@@ -160,44 +135,22 @@ public class RegisterPageTest extends ManagedTest {
 
                }
        }
+       @Test
+       public void testInvalidMailbox() {
+               getMailReciever().setApproveRegex(Pattern.compile("a"));
+               long uniq = System.currentTimeMillis();
+               try {
+                       registerUser("RegisterTest", "User", "testInvalidMailbox" + uniq
+                                       + "@cacert.org", "registerPW");
+                       throw new Error(
+                                       "Registering a user with invalid mailbox must fail.");
+               } catch (AssertionError e) {
 
+               }
+       }
        private void testFailedForm(String query) throws IOException {
                String startError = fetchStartErrorMessage(query);
                assertTrue(startError, !startError.startsWith("</div>"));
        }
-       private String fetchStartErrorMessage(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();
-               return startError;
-       }
 
-       public void registerUser(String firstName, String lastName, String email,
-                       String password) {
-               try {
-                       String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
-                                       + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
-                                       + "&email=" + URLEncoder.encode(email, "UTF-8")
-                                       + "&pword1=" + URLEncoder.encode(password, "UTF-8")
-                                       + "&pword2=" + URLEncoder.encode(password, "UTF-8")
-                                       + "&day=1&month=1&year=1910&cca_agree=1";
-                       String data = fetchStartErrorMessage(query);
-                       assertTrue(data, data.startsWith("</div>"));
-               } catch (UnsupportedEncodingException e) {
-                       throw new Error(e);
-               } catch (IOException e) {
-                       throw new Error(e);
-               }
-       }
-       private 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;
-       }
 }
index d59690d7350af05f6c1676ac05ec17943be85b6a..f591e77259e0f6a1a6d9fd3cf576f0f92469fa45 100644 (file)
@@ -1,22 +1,32 @@
 package org.cacert.gigi.testUtils;
 
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
 import java.io.BufferedReader;
 import java.io.DataOutputStream;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.net.HttpURLConnection;
 import java.net.InetSocketAddress;
+import java.net.URL;
+import java.net.URLEncoder;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.Properties;
 
 import org.cacert.gigi.DevelLauncher;
+import org.cacert.gigi.IOUtils;
 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
 import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 
 public class ManagedTest {
+       private final String registerService = "/register";
+
        private static TestEmailReciever ter;
        private static Process gigi;
        private static String url = "localhost:4443";
@@ -106,7 +116,7 @@ public class ManagedTest {
 
        @After
        public void removeMails() {
-               ter.clearMails();
+               ter.reset();
        }
 
        public TestMail waitForMail() {
@@ -116,4 +126,57 @@ public class ManagedTest {
                        throw new Error(e);
                }
        }
+       public static TestEmailReciever getMailReciever() {
+               return ter;
+       }
+       public String runRegister(String param) throws IOException {
+               HttpURLConnection uc = (HttpURLConnection) new URL("https://"
+                               + getServerName() + registerService).openConnection();
+               uc.setDoOutput(true);
+               uc.getOutputStream().write(param.getBytes());
+               String d = IOUtils.readURL(uc);
+               return d;
+       }
+       public String fetchStartErrorMessage(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();
+               return startError;
+       }
+
+       public void registerUser(String firstName, String lastName, String email,
+                       String password) {
+               try {
+                       String query = "fname=" + URLEncoder.encode(firstName, "UTF-8")
+                                       + "&lname=" + URLEncoder.encode(lastName, "UTF-8")
+                                       + "&email=" + URLEncoder.encode(email, "UTF-8")
+                                       + "&pword1=" + URLEncoder.encode(password, "UTF-8")
+                                       + "&pword2=" + URLEncoder.encode(password, "UTF-8")
+                                       + "&day=1&month=1&year=1910&cca_agree=1";
+                       String data = fetchStartErrorMessage(query);
+                       assertTrue(data, data.startsWith("</div>"));
+               } catch (UnsupportedEncodingException e) {
+                       throw new Error(e);
+               } catch (IOException e) {
+                       throw new Error(e);
+               }
+       }
+       public void createVerifiedUser(String firstName, String lastName,
+                       String email, String password) {
+               registerUser(firstName, lastName, email, password);
+               try {
+                       TestMail tm = ter.recieve();
+                       String verifyLink = tm.extractLink();
+                       String[] parts = verifyLink.split("\\?");
+                       URL u = new URL("https://" + getServerName() + "/verify" + parts[1]);
+                       u.openStream().close();;
+               } catch (InterruptedException e) {
+                       throw new Error(e);
+               } catch (IOException e) {
+                       throw new Error(e);
+               }
+       }
 }
index 53e461625eea2ccfd9783e32925f9a00731cd7ee..446d2aad769d59fe5efd55b71f3ebde148eaa23a 100644 (file)
@@ -1,11 +1,14 @@
 package org.cacert.gigi.testUtils;
 
 import java.io.DataInputStream;
+import java.io.DataOutputStream;
 import java.io.IOException;
 import java.net.SocketAddress;
 import java.net.Socket;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 public class TestEmailReciever implements Runnable {
        public class TestMail {
@@ -37,10 +40,17 @@ public class TestEmailReciever implements Runnable {
                public String getReplyto() {
                        return replyto;
                }
+               public String extractLink() {
+                       Pattern link = Pattern.compile("http://[^\\s]+(?=\\s)");
+                       Matcher m = link.matcher(getMessage());
+                       m.find();
+                       return m.group(0);
+               }
 
        }
        private Socket s;
        private DataInputStream dis;
+       private DataOutputStream dos;
 
        public TestEmailReciever(SocketAddress target) throws IOException {
                s = new Socket();
@@ -48,6 +58,7 @@ public class TestEmailReciever implements Runnable {
                s.setKeepAlive(true);
                s.setSoTimeout(1000 * 60 * 60);
                dis = new DataInputStream(s.getInputStream());
+               dos = new DataOutputStream(s.getOutputStream());
                new Thread(this).start();
        }
        LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
@@ -59,21 +70,44 @@ public class TestEmailReciever implements Runnable {
        public void run() {
                try {
                        while (true) {
-                               String to = dis.readUTF();
-                               String subject = dis.readUTF();
-                               String message = dis.readUTF();
-                               String from = dis.readUTF();
-                               String replyto = dis.readUTF();
-                               mails.add(new TestMail(to, subject, message, from, replyto));
+                               String type = dis.readUTF();
+                               if (type.equals("mail")) {
+                                       String to = dis.readUTF();
+                                       String subject = dis.readUTF();
+                                       String message = dis.readUTF();
+                                       String from = dis.readUTF();
+                                       String replyto = dis.readUTF();
+                                       mails.add(new TestMail(to, subject, message, from, replyto));
+                               } else if (type.equals("challengeAddrBox")) {
+                                       String email = dis.readUTF();
+                                       if (approveRegex.matcher(email).matches()) {
+                                               System.out.println("approving mbox: " + email);
+                                               dos.writeUTF("OK");
+                                       } else {
+                                               System.out.println("rejecting mbox: " + email);
+                                               dos.writeUTF("FAIL");
+                                       }
+                                       dos.flush();
+                               } else {
+                                       System.err.println("Unknown type: " + type);
+                               }
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }
 
        }
+       Pattern approveRegex = Pattern.compile(".*");
+       public void setApproveRegex(Pattern approveRegex) {
+               this.approveRegex = approveRegex;
+       }
 
        public void clearMails() {
                mails.clear();
        }
+       public void reset() {
+               clearMails();
+               approveRegex = Pattern.compile(".*");
+       }
 
 }