]> WPIA git - gigi.git/commitdiff
add: external test for password reset
authorFelix Dörre <felix@dogcraft.de>
Sat, 14 Nov 2015 15:18:47 +0000 (16:18 +0100)
committerFelix Dörre <felix@dogcraft.de>
Sat, 14 Nov 2015 15:18:47 +0000 (16:18 +0100)
tests/org/cacert/gigi/pages/account/TestPasswordResetExternal.java [new file with mode: 0644]
tests/org/cacert/gigi/pages/wot/TestAssurance.java
tests/org/cacert/gigi/testUtils/ManagedTest.java

diff --git a/tests/org/cacert/gigi/pages/account/TestPasswordResetExternal.java b/tests/org/cacert/gigi/pages/account/TestPasswordResetExternal.java
new file mode 100644 (file)
index 0000000..8acef6b
--- /dev/null
@@ -0,0 +1,58 @@
+package org.cacert.gigi.pages.account;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLEncoder;
+
+import org.cacert.gigi.dbObjects.User;
+import org.cacert.gigi.pages.wot.TestAssurance;
+import org.cacert.gigi.testUtils.ClientTest;
+import org.cacert.gigi.testUtils.IOUtils;
+import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
+import org.cacert.gigi.util.RandomToken;
+import org.junit.Test;
+
+public class TestPasswordResetExternal extends ClientTest {
+
+    @Test
+    public void testByAssurance() throws IOException {
+        User u = User.getById(createAssuranceUser("fn", "ln", createUniqueName() + "@example.com", TEST_PASSWORD));
+        String cookie2 = login(u.getEmail(), TEST_PASSWORD);
+        URLConnection uc = TestAssurance.buildupAssureFormConnection(cookie2, email, true);
+        String avalue = RandomToken.generateToken(32);
+        uc.getOutputStream().write(("date=1910-01-01&location=testcase&certify=1&rules=1&CCAAgreed=1&assertion=1&points=10&passwordReset=1&passwordResetValue=" + URLEncoder.encode(avalue, "UTF-8")).getBytes("UTF-8"));
+        uc.getOutputStream().flush();
+        String error = fetchStartErrorMessage(IOUtils.readURL(uc));
+        assertNull(error);
+
+        TestMail mail = getMailReciever().receive();
+        System.out.println(mail.getMessage());
+        String link = mail.extractLink();
+        String npw = TEST_PASSWORD + "'";
+        assertNotNull(toPasswordReset(avalue, link, npw, npw + "'"));
+        assertNotNull(toPasswordReset(avalue + "'", link, npw, npw));
+        assertNotNull(toPasswordReset(avalue, link, "a", "a"));
+        assertNull(toPasswordReset(avalue, link, npw, npw));
+        assertNotNull(login(email, npw));
+    }
+
+    private String toPasswordReset(String avalue, String link, String npw, String npw2) throws IOException, MalformedURLException, UnsupportedEncodingException {
+        URLConnection uc2 = new URL(link).openConnection();
+        String csrf = getCSRF(uc2);
+        String headerField = uc2.getHeaderField("Set-Cookie");
+        assertNotNull(headerField);
+        String cookie3 = stripCookie(headerField);
+        uc2 = new URL(link).openConnection();
+        cookie(uc2, cookie3);
+        uc2.setDoOutput(true);
+        OutputStream o = uc2.getOutputStream();
+        o.write(("csrf=" + csrf + "&pword1=" + URLEncoder.encode(npw, "UTF-8") + "&pword2=" + URLEncoder.encode(npw2, "UTF-8") + "&private_token=" + URLEncoder.encode(avalue, "UTF-8")).getBytes("UTF-8"));
+        return fetchStartErrorMessage(IOUtils.readURL(uc2));
+    }
+}
index b6396a17c900d188239f509ce01b0e1b8fd1643b..58a791f372fcc0066a83b00244ffd44e0b9e3884 100644 (file)
@@ -215,11 +215,15 @@ public class TestAssurance extends ManagedTest {
     }
 
     private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
     }
 
     private URLConnection buildupAssureFormConnection(boolean doCSRF) throws MalformedURLException, IOException {
+        return buildupAssureFormConnection(cookie, assureeM, doCSRF);
+    }
+
+    public static URLConnection buildupAssureFormConnection(String cookie, String email, boolean doCSRF) throws MalformedURLException, IOException {
         URL u = new URL("https://" + getServerName() + AssurePage.PATH);
         URLConnection uc = u.openConnection();
         uc.addRequestProperty("Cookie", cookie);
         uc.setDoOutput(true);
         URL u = new URL("https://" + getServerName() + AssurePage.PATH);
         URLConnection uc = u.openConnection();
         uc.addRequestProperty("Cookie", cookie);
         uc.setDoOutput(true);
-        uc.getOutputStream().write(("email=" + URLEncoder.encode(assureeM, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
+        uc.getOutputStream().write(("email=" + URLEncoder.encode(email, "UTF-8") + "&day=1&month=1&year=1910&search").getBytes("UTF-8"));
 
         String csrf = getCSRF(uc);
         uc = u.openConnection();
 
         String csrf = getCSRF(uc);
         uc = u.openConnection();
index 0ae9f9a7b1de8bf4ca777502885509e8aef4428f..bd35e821324dc0eff83b2d23be45f422258ac4f1 100644 (file)
@@ -330,7 +330,7 @@ public class ManagedTest extends ConfiguredTest {
         ps2.execute();
     }
 
         ps2.execute();
     }
 
-    static String stripCookie(String headerField) {
+    protected static String stripCookie(String headerField) {
         return headerField.substring(0, headerField.indexOf(';'));
     }
 
         return headerField.substring(0, headerField.indexOf(';'));
     }