]> WPIA git - gigi.git/blobdiff - tests/club/wpia/gigi/testUtils/ManagedTest.java
add: ensure that for support actions certificate login is used
[gigi.git] / tests / club / wpia / gigi / testUtils / ManagedTest.java
index 3eeda2ca705b7df263a4843f45c41c04e51be71d..25df2725b22b324ae3692a8e73311b5a13dc77d2 100644 (file)
@@ -17,6 +17,7 @@ import java.net.URLConnection;
 import java.net.URLEncoder;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.security.GeneralSecurityException;
 import java.security.KeyManagementException;
 import java.security.NoSuchAlgorithmException;
 import java.security.Principal;
@@ -42,6 +43,7 @@ import club.wpia.gigi.DevelLauncher;
 import club.wpia.gigi.GigiApiException;
 import club.wpia.gigi.database.GigiPreparedStatement;
 import club.wpia.gigi.database.GigiResultSet;
+import club.wpia.gigi.dbObjects.Certificate;
 import club.wpia.gigi.dbObjects.EmailAddress;
 import club.wpia.gigi.dbObjects.Group;
 import club.wpia.gigi.dbObjects.Job;
@@ -70,6 +72,10 @@ public class ManagedTest extends ConfiguredTest {
 
     private static String acceptLanguage = null;
 
+    protected static Certificate loginCertificate;
+
+    protected static PrivateKey loginPrivateKey;
+
     public static void setAcceptLanguage(String acceptLanguage) {
         ManagedTest.acceptLanguage = acceptLanguage;
     }
@@ -278,7 +284,7 @@ public class ManagedTest extends ConfiguredTest {
 
     public static void registerUser(String firstName, String lastName, String email, String password) {
         try {
-            String query = "name-type=western&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&tos_agree=1";
+            String query = "name-type=western&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&tos_agree=1&dp_agree=1";
             String data = fetchStartErrorMessage(runRegister(query));
             assertNull(data);
         } catch (UnsupportedEncodingException e) {
@@ -291,7 +297,7 @@ public class ManagedTest extends ConfiguredTest {
     public static int createVerifiedUser(String firstName, String lastName, String email, String password) {
         registerUser(firstName, lastName, email, password);
         try {
-            ter.receive().verify();
+            ter.receive(email).verify();
 
             try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `users` WHERE `email`=?")) {
                 ps.setString(1, email);
@@ -469,12 +475,16 @@ public class ManagedTest extends ConfiguredTest {
     }
 
     public static HttpURLConnection post(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 server = getServerName();
+        if (loginCertificate != null) {
+            server = getSecureServerName();
+        }
+        URLConnection uc = new URL("https://" + server + path).openConnection();
+        authenticate((HttpURLConnection) uc, cookie);
         String csrf = getCSRF(uc, formIndex);
 
-        uc = new URL("https://" + getServerName() + path).openConnection();
-        uc.addRequestProperty("Cookie", cookie);
+        uc = new URL("https://" + server + path).openConnection();
+        authenticate((HttpURLConnection) uc, cookie);
         uc.setDoOutput(true);
         OutputStream os = uc.getOutputStream();
         os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
@@ -485,8 +495,12 @@ public class ManagedTest extends ConfiguredTest {
     }
 
     public static HttpURLConnection get(String cookie, String path) throws IOException {
-        URLConnection uc = new URL("https://" + getServerName() + path).openConnection();
-        uc.addRequestProperty("Cookie", cookie);
+        String server = getServerName();
+        if (loginCertificate != null) {
+            server = getSecureServerName();
+        }
+        URLConnection uc = new URL("https://" + server + path).openConnection();
+        authenticate((HttpURLConnection) uc, cookie);
         return (HttpURLConnection) uc;
     }
 
@@ -496,11 +510,10 @@ public class ManagedTest extends ConfiguredTest {
 
     public EmailAddress createVerifiedEmail(User u, String email) throws InterruptedException, GigiApiException {
         EmailAddress addr = new EmailAddress(u, email, Locale.ENGLISH);
-        TestMail testMail = getMailReceiver().receive();
-        assertEquals(addr.getAddress(), testMail.getTo());
+        TestMail testMail = getMailReceiver().receive(addr.getAddress());
         String hash = testMail.extractLink().substring(testMail.extractLink().lastIndexOf('=') + 1);
         addr.verify(hash);
-        getMailReceiver().clearMails();
+        getMailReceiver().assertEmpty();
         return addr;
     }
 
@@ -526,4 +539,15 @@ public class ManagedTest extends ConfiguredTest {
         supporter = User.getById(i);
         return supporter;
     }
+
+    protected static void authenticate(HttpURLConnection uc, String cookie) throws IOException {
+        uc.addRequestProperty("Cookie", cookie);
+        if (loginCertificate != null) {
+            try {
+                authenticateClientCert(loginPrivateKey, loginCertificate.cert(), uc);
+            } catch (GeneralSecurityException | GigiApiException e) {
+                throw new IOException(e);
+            }
+        }
+    }
 }