]> WPIA git - gigi.git/blobdiff - tests/org/cacert/gigi/testUtils/TestEmailReciever.java
Fix: consolidate TestEmailReveiver interface
[gigi.git] / tests / org / cacert / gigi / testUtils / TestEmailReciever.java
index a68b1847e0c5cdf10881d86b2e543b20c570b074..099bbf1a09c7776e8d1760e46bb63023af023a59 100644 (file)
@@ -5,6 +5,8 @@ import java.io.DataOutputStream;
 import java.io.IOException;
 import java.net.Socket;
 import java.net.SocketAddress;
+import java.net.URL;
+import java.net.URLConnection;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
@@ -12,7 +14,7 @@ import java.util.regex.Pattern;
 
 import org.cacert.gigi.email.EmailProvider;
 
-public class TestEmailReciever extends EmailProvider implements Runnable {
+public final class TestEmailReciever extends EmailProvider implements Runnable {
 
     public class TestMail {
 
@@ -61,6 +63,22 @@ public class TestEmailReciever extends EmailProvider implements Runnable {
             return m.group(0);
         }
 
+        public void verify() throws IOException {
+            String[] parts = extractLink().split("\\?");
+            URL u = new URL("https://" + ManagedTest.getServerName() + "/verify?" + parts[1]);
+
+            URLConnection csrfConn = u.openConnection();
+            String csrf = ManagedTest.getCSRF(csrfConn, 0);
+
+            u = new URL("https://" + ManagedTest.getServerName() + "/verify");
+            URLConnection uc = u.openConnection();
+            ManagedTest.cookie(uc, ManagedTest.stripCookie(csrfConn.getHeaderField("Set-Cookie")));
+            uc.setDoOutput(true);
+            uc.getOutputStream().write((parts[1] + "&csrf=" + csrf).getBytes("UTF-8"));
+            uc.connect();
+            uc.getInputStream().close();
+        }
+
     }
 
     private Socket s;
@@ -76,14 +94,53 @@ public class TestEmailReciever extends EmailProvider implements Runnable {
         s.setSoTimeout(1000 * 60 * 60);
         dis = new DataInputStream(s.getInputStream());
         dos = new DataOutputStream(s.getOutputStream());
-        new Thread(this).start();
         setInstance(this);
     }
 
+    public void start() {
+        new Thread(this, "Mail reciever").start();
+    }
+
     LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
 
-    public TestMail recieve() throws InterruptedException {
-        return mails.poll(5, TimeUnit.SECONDS);
+    /**
+     * Retrieves an outgoing mail from the system. The method will return a
+     * {@link TestMail} or fail.
+     * 
+     * @return The intercepted {@link TestMail}
+     * @see #poll()
+     */
+    public TestMail receive() {
+        TestMail poll;
+
+        try {
+            poll = mails.poll(60, TimeUnit.SECONDS);
+
+        } catch (InterruptedException e) {
+            throw new AssertionError("Interrupted while recieving mails");
+        }
+        if (poll == null) {
+            throw new AssertionError("Mail recieving timed out");
+        }
+
+        return poll;
+    }
+
+    /**
+     * Retrieves an outgoing mail from the system or returns <code>null</code>
+     * if there was no mail sent in 30 seconds.
+     * 
+     * @return The intercepted {@link TestMail} or <code>null</code> if no mail
+     *         has been sent.
+     * @see #receive()
+     */
+    public TestMail poll() {
+        try {
+            return mails.poll(60, TimeUnit.SECONDS);
+
+        } catch (InterruptedException e) {
+            throw new AssertionError("Interrupted while recieving mails");
+        }
     }
 
     @Override