]> WPIA git - gigi.git/blobdiff - tests/org/cacert/gigi/ping/TestHTTP.java
Merge "Suggestions to enhance the SQL call pattern."
[gigi.git] / tests / org / cacert / gigi / ping / TestHTTP.java
index d6718241f33479b1a3d8b1de414d3f50a2029ebd..31fecd942bb53e2ba4055aad9a182d77b953cbff 100644 (file)
@@ -1,6 +1,8 @@
 package org.cacert.gigi.ping;
 
+import static org.hamcrest.CoreMatchers.*;
 import static org.junit.Assert.*;
+import static org.junit.Assume.*;
 
 import java.io.IOException;
 import java.io.InputStreamReader;
@@ -12,10 +14,13 @@ import java.util.regex.Pattern;
 
 import javax.naming.NamingException;
 
-import org.cacert.gigi.pages.account.DomainOverview;
+import org.cacert.gigi.GigiApiException;
+import org.cacert.gigi.dbObjects.Domain;
+import org.cacert.gigi.dbObjects.DomainPingConfiguration;
+import org.cacert.gigi.dbObjects.DomainPingType;
 import org.cacert.gigi.testUtils.IOUtils;
 import org.cacert.gigi.testUtils.PingTest;
-import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
+import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
 import org.cacert.gigi.util.RandomToken;
 import org.junit.Test;
 
@@ -33,26 +38,26 @@ public class TestHTTP extends PingTest {
     }
 
     @Test
-    public void httpAndMailSuccess() throws IOException, InterruptedException, SQLException {
+    public void httpAndMailSuccess() throws Exception {
         testEmailAndHTTP(0, 0, true, true);
     }
 
     @Test
-    public void httpFailKeyAndMailSuccess() throws IOException, InterruptedException, SQLException {
+    public void httpFailKeyAndMailSuccess() throws Exception {
         testEmailAndHTTP(1, 0, false, true);
     }
 
     @Test
-    public void httpFailValAndMailFail() throws IOException, InterruptedException, SQLException {
+    public void httpFailValAndMailFail() throws Exception {
         testEmailAndHTTP(2, 1, false, false);
     }
 
-    public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException {
+    public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException, GigiApiException {
 
         String test = getTestProps().getProperty("domain.http");
+        assumeNotNull(test);
 
-        URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
-        Matcher m = initailizeDomainForm(u);
+        Matcher m = initailizeDomainForm();
         updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
 
         String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
@@ -62,25 +67,43 @@ public class TestHTTP extends PingTest {
                 "&ssl-type-2=direct&ssl-port-2=" + //
                 "&ssl-type-3=direct&ssl-port-3=" + //
                 "&adddomain&csrf=" + csrf;
-        URL u2 = sendDomainForm(u, content);
+        String p2 = sendDomainForm(content);
 
-        TestMail mail = getMailReciever().recieve();
+        TestMail mail = getMailReciever().receive();
         if (emailVariant == 0) {
-            String link = mail.extractLink();
-            new URL(link).openConnection().getHeaderField("");
+            mail.verify();
         }
         waitForPings(2);
 
-        String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
+        String newcontent = IOUtils.readURL(get(p2));
         Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
         assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
         pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
         assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
+
+        if (successHTTP) { // give it a second try
+            int id = Integer.parseInt(p2.replaceFirst("^.*/([0-9]+)$", "$1"));
+            Domain d = Domain.getById(id);
+            DomainPingConfiguration dpc = null;
+            for (DomainPingConfiguration conf : d.getConfiguredPings()) {
+                if (conf.getType() == DomainPingType.HTTP) {
+                    dpc = conf;
+                    break;
+                }
+            }
+            if (dpc == null) {
+                fail("Http config not found");
+            }
+            String res = executeBasicWebInteraction(cookie, p2, "configId=" + dpc.getId());
+            assertThat(res, containsString("only allowed after"));
+        }
     }
 
     private String readHTTP(String token) throws IOException {
-        URL u = new URL("http://" + getTestProps().getProperty("domain.http") + "/cacert-" + token + ".txt");
-        return IOUtils.readURL(new InputStreamReader(u.openStream())).trim();
+        String httpDom = getTestProps().getProperty("domain.http");
+        assumeNotNull(httpDom);
+        URL u = new URL("http://" + httpDom + "/cacert-" + token + ".txt");
+        return IOUtils.readURL(new InputStreamReader(u.openStream(), "UTF-8")).trim();
 
     }
 }