]> WPIA git - gigi.git/blobdiff - tests/org/cacert/gigi/testUtils/ManagedTest.java
UPD: Make some test methods static
[gigi.git] / tests / org / cacert / gigi / testUtils / ManagedTest.java
index d7a5fcac9dba458d5ce7b09a966b2f1c47e88dfe..5d4db1299df473caf6a8e3849f76bcc711ad0b0b 100644 (file)
@@ -36,9 +36,14 @@ import javax.net.ssl.SSLContext;
 import javax.net.ssl.X509KeyManager;
 
 import org.cacert.gigi.DevelLauncher;
+import org.cacert.gigi.EmailAddress;
+import org.cacert.gigi.GigiApiException;
+import org.cacert.gigi.Language;
+import org.cacert.gigi.User;
 import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
 import org.cacert.gigi.util.DatabaseManager;
+import org.cacert.gigi.util.ServerConstants;
 import org.cacert.gigi.util.SimpleSigner;
 import org.junit.After;
 import org.junit.AfterClass;
@@ -76,8 +81,9 @@ public class ManagedTest {
                        System.out.println("... purging Database");
                        DatabaseManager.run(new String[] { testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"),
                                        testProps.getProperty("sql.user"), testProps.getProperty("sql.password") });
-
                        String type = testProps.getProperty("type");
+                       Properties mainProps = generateMainProps();
+                       ServerConstants.init(mainProps);
                        if (type.equals("local")) {
                                url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort");
                                String[] parts = testProps.getProperty("mail").split(":", 2);
@@ -88,19 +94,6 @@ public class ManagedTest {
                        gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
                        DataOutputStream toGigi = new DataOutputStream(gigi.getOutputStream());
                        System.out.println("... starting server");
-                       Properties mainProps = new Properties();
-                       mainProps.setProperty("host", "127.0.0.1");
-                       mainProps.setProperty("name.secure", testProps.getProperty("name.secure"));
-                       mainProps.setProperty("name.www", testProps.getProperty("name.www"));
-                       mainProps.setProperty("name.static", testProps.getProperty("name.static"));
-
-                       mainProps.setProperty("port", testProps.getProperty("serverPort"));
-                       mainProps.setProperty("emailProvider", "org.cacert.gigi.email.TestEmailProvider");
-                       mainProps.setProperty("emailProvider.port", "8473");
-                       mainProps.setProperty("sql.driver", testProps.getProperty("sql.driver"));
-                       mainProps.setProperty("sql.url", testProps.getProperty("sql.url"));
-                       mainProps.setProperty("sql.user", testProps.getProperty("sql.user"));
-                       mainProps.setProperty("sql.password", testProps.getProperty("sql.password"));
 
                        byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks"));
                        byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12"));
@@ -143,6 +136,23 @@ public class ManagedTest {
 
        }
 
+       private static Properties generateMainProps() {
+               Properties mainProps = new Properties();
+               mainProps.setProperty("host", "127.0.0.1");
+               mainProps.setProperty("name.secure", testProps.getProperty("name.secure"));
+               mainProps.setProperty("name.www", testProps.getProperty("name.www"));
+               mainProps.setProperty("name.static", testProps.getProperty("name.static"));
+
+               mainProps.setProperty("port", testProps.getProperty("serverPort"));
+               mainProps.setProperty("emailProvider", "org.cacert.gigi.email.TestEmailProvider");
+               mainProps.setProperty("emailProvider.port", "8473");
+               mainProps.setProperty("sql.driver", testProps.getProperty("sql.driver"));
+               mainProps.setProperty("sql.url", testProps.getProperty("sql.url"));
+               mainProps.setProperty("sql.user", testProps.getProperty("sql.user"));
+               mainProps.setProperty("sql.password", testProps.getProperty("sql.password"));
+               return mainProps;
+       }
+
        @AfterClass
        public static void tearDownServer() {
                String type = testProps.getProperty("type");
@@ -276,7 +286,7 @@ public class ManagedTest {
 
        static int count = 0;
 
-       public String createUniqueName() {
+       public static String createUniqueName() {
                return "test" + System.currentTimeMillis() + "a" + (count++);
        }
 
@@ -365,11 +375,17 @@ public class ManagedTest {
        }
 
        public String getCSRF(URLConnection u) throws IOException {
+               return getCSRF(u, 0);
+       }
+
+       public String getCSRF(URLConnection u, int formIndex) throws IOException {
                String content = IOUtils.readURL(u);
                Pattern p = Pattern.compile("<input type='hidden' name='csrf' value='([^']+)'>");
                Matcher m = p.matcher(content);
-               if (!m.find()) {
-                       throw new Error("No CSRF Token");
+               for (int i = 0; i < formIndex + 1; i++) {
+                       if (!m.find()) {
+                               throw new Error("No CSRF Token");
+                       }
                }
                return m.group(1);
        }
@@ -388,11 +404,16 @@ public class ManagedTest {
                return parts;
        }
 
-       public String executeBasicWebInteraction(String cookie, String path, String query) throws IOException,
-               MalformedURLException, UnsupportedEncodingException {
+       public String executeBasicWebInteraction(String cookie, String path, String query) throws MalformedURLException,
+               UnsupportedEncodingException, IOException {
+               return executeBasicWebInteraction(cookie, path, query, 0);
+       }
+
+       public String executeBasicWebInteraction(String cookie, String path, String query, int formIndex)
+               throws IOException, MalformedURLException, UnsupportedEncodingException {
                URLConnection uc = new URL("https://" + getServerName() + path).openConnection();
                uc.addRequestProperty("Cookie", cookie);
-               String csrf = getCSRF(uc);
+               String csrf = getCSRF(uc, formIndex);
 
                uc = new URL("https://" + getServerName() + path).openConnection();
                uc.addRequestProperty("Cookie", cookie);
@@ -406,4 +427,15 @@ public class ManagedTest {
                return error;
        }
 
+       public static EmailAddress createVerifiedEmail(User u) throws InterruptedException, GigiApiException {
+               EmailAddress adrr = new EmailAddress(createUniqueName() + "test@test.tld", u);
+               adrr.insert(Language.getInstance("en"));
+               TestMail testMail = getMailReciever().recieve();
+               assertTrue(adrr.getAddress().equals(testMail.getTo()));
+               String hash = testMail.extractLink().substring(testMail.extractLink().lastIndexOf('=') + 1);
+               adrr.verify(hash);
+               getMailReciever().clearMails();
+               return adrr;
+       }
+
 }