]> WPIA git - gigi.git/blobdiff - util-testing/org/cacert/gigi/pages/Manager.java
upd: move "getRandomCountry" to test Manager
[gigi.git] / util-testing / org / cacert / gigi / pages / Manager.java
index 6889bfbe908ba43aef89a43df2c831c1dac26374..a2435c95612d2a9b4c830ff5efa2738b48428257 100644 (file)
@@ -14,9 +14,11 @@ import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Random;
 import java.util.TreeSet;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -34,6 +36,7 @@ import org.cacert.gigi.dbObjects.CATS.CATSType;
 import org.cacert.gigi.dbObjects.Certificate;
 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
 import org.cacert.gigi.dbObjects.CertificateOwner;
+import org.cacert.gigi.dbObjects.Country;
 import org.cacert.gigi.dbObjects.Digest;
 import org.cacert.gigi.dbObjects.Domain;
 import org.cacert.gigi.dbObjects.DomainPingType;
@@ -42,7 +45,7 @@ import org.cacert.gigi.dbObjects.Group;
 import org.cacert.gigi.dbObjects.NamePart;
 import org.cacert.gigi.dbObjects.NamePart.NamePartType;
 import org.cacert.gigi.dbObjects.User;
-import org.cacert.gigi.email.EmailProvider;
+import org.cacert.gigi.email.DelegateMailProvider;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.output.template.IterableDataset;
 import org.cacert.gigi.output.template.Template;
@@ -52,11 +55,18 @@ import org.cacert.gigi.ping.PingerDaemon;
 import org.cacert.gigi.util.AuthorizationContext;
 import org.cacert.gigi.util.DayDate;
 import org.cacert.gigi.util.Notary;
+import org.cacert.gigi.util.TimeConditions;
 
 import sun.security.x509.X509Key;
 
 public class Manager extends Page {
 
+    public static Country getRandomCountry() {
+        List<Country> cc = Country.getCountries();
+        int rnd = new Random().nextInt(cc.size());
+        return cc.get(rnd);
+    }
+
     public static final String PATH = "/manager";
 
     private static HashMap<DomainPingType, DomainPinger> dps;
@@ -101,19 +111,20 @@ public class Manager extends Page {
     }
 
     private User createAssurer(int i) throws GigiApiException, IllegalAccessException {
-        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `country`=?")) {
             String mail = "test-assurer" + i + "@example.com";
             User u = User.getByEmail(mail);
             if (u == null) {
-                System.out.println("Creating assurer");
+                System.out.println("Creating RA-Agent");
                 createUser(mail);
                 u = User.getByEmail(mail);
                 passCATS(u, CATSType.ASSURER_CHALLENGE);
                 ps.setInt(1, u.getId());
-                ps.setInt(2, u.getId());
+                ps.setInt(2, u.getPreferredName().getId());
                 ps.setInt(3, 100);
                 ps.setString(4, "Manager init code");
                 ps.setString(5, "1990-01-01");
+                ps.setString(6, getRandomCountry().getCode());
                 ps.execute();
             }
             return u;
@@ -139,9 +150,23 @@ public class Manager extends Page {
         return instance;
     }
 
-    public static class MailFetcher extends EmailProvider {
+    public static class MailFetcher extends DelegateMailProvider {
 
-        public MailFetcher(Properties p) {}
+        Pattern[] toForward;
+
+        public MailFetcher(Properties props) {
+            super(props, props.getProperty("emailProvider.manager.target"));
+            String str = props.getProperty("emailProvider.manager.filter");
+            if (str == null) {
+                toForward = new Pattern[0];
+            } else {
+                String[] parts = str.split(" ");
+                toForward = new Pattern[parts.length];
+                for (int i = 0; i < parts.length; i++) {
+                    toForward[i] = Pattern.compile(parts[i]);
+                }
+            }
+        }
 
         @Override
         public String checkEmailServer(int forUid, String address) throws IOException {
@@ -149,13 +174,19 @@ public class Manager extends Page {
         }
 
         @Override
-        public synchronized void sendMail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
+        public synchronized void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
             HashMap<String, LinkedList<String>> mails = Manager.getInstance().emails;
             LinkedList<String> hismails = mails.get(to);
             if (hismails == null) {
                 mails.put(to, hismails = new LinkedList<>());
             }
             hismails.addFirst(subject + "\n" + message);
+            for (int i = 0; i < toForward.length; i++) {
+                if (toForward[i].matcher(to).matches()) {
+                    super.sendMail(to, subject, message, replyto, toname, fromname, errorsto, extra);
+                    return;
+                }
+            }
         }
 
     }
@@ -204,8 +235,11 @@ public class Manager extends Page {
         Calendar gc = GregorianCalendar.getInstance();
         gc.setTimeInMillis(0);
         gc.set(1990, 0, 1);
-        User u = new User(email, "xvXV12°§", new DayDate(gc.getTime().getTime()), Locale.ENGLISH, //
-                new NamePart(NamePartType.FIRST_NAME, "Först"), new NamePart(NamePartType.FIRST_NAME, "Müddle"),//
+
+        Country country = getRandomCountry();
+
+        User u = new User(email, "xvXV12°§", new DayDate(gc.getTime().getTime()), Locale.ENGLISH, country, //
+                new NamePart(NamePartType.FIRST_NAME, "Först"), new NamePart(NamePartType.FIRST_NAME, "Müddle"), //
                 new NamePart(NamePartType.LAST_NAME, "Läst"), new NamePart(NamePartType.SUFFIX, "Süffix"));
         EmailAddress ea = u.getEmails()[0];
         verify(email, ea);
@@ -265,26 +299,51 @@ public class Manager extends Page {
             resp.getWriter().println("Test '" + test.getDisplayName() + "' was added to user account.");
         } else if (req.getParameter("assure") != null) {
             String mail = req.getParameter("assureEmail");
+            String verificationPoints = req.getParameter("verificationPoints");
             User byEmail = User.getByEmail(mail);
+
             if (byEmail == null) {
                 resp.getWriter().println("User not found.");
                 return;
             }
+
+            int vp = 0;
+            int agentNumber = 0;
+
             try {
-                for (int i = 0; i < 10; i++) {
-                    Notary.assure(getAssurer(i), byEmail, byEmail.getPreferredName(), byEmail.getDoB(), 10, "Testmanager Assure up code", "2014-11-06", AssuranceType.FACE_TO_FACE);
+                try {
+                    vp = Integer.parseInt(verificationPoints);
+                } catch (NumberFormatException e) {
+                    throw new GigiApiException("No valid Verification Points entered.");
+                }
+
+                if (vp > 100) { // only allow max 100 Verification points
+                    vp = 100;
+                }
+
+                while (vp > 0) {
+                    int currentVP = 10;
+                    if (vp < 10) {
+                        currentVP = vp;
+                    }
+                    Notary.assure(getAssurer(agentNumber), byEmail, byEmail.getPreferredName(), byEmail.getDoB(), currentVP, "Testmanager Verify up code", "2014-11-06", AssuranceType.FACE_TO_FACE, getRandomCountry());
+                    agentNumber += 1;
+                    vp -= currentVP;
                 }
+
             } catch (GigiApiException e) {
                 throw new Error(e);
             }
-            resp.getWriter().println("User has been assured.");
+
+            resp.getWriter().println("User has been assured " + agentNumber + " times.");
+
         } else if (req.getParameter("letassure") != null) {
             String mail = req.getParameter("letassureEmail");
             User byEmail = User.getByEmail(mail);
             try {
                 for (int i = 0; i < 25; i++) {
                     User a = getAssurer(i);
-                    Notary.assure(byEmail, a, a.getNames()[0], a.getDoB(), 10, "Testmanager exp up code", "2014-11-06", AssuranceType.FACE_TO_FACE);
+                    Notary.assure(byEmail, a, a.getNames()[0], a.getDoB(), 10, "Testmanager exp up code", "2014-11-06", AssuranceType.FACE_TO_FACE, getRandomCountry());
                 }
             } catch (GigiApiException e) {
                 throw new Error(e);
@@ -326,9 +385,6 @@ public class Manager extends Page {
                 resp.getWriter().println("error");
             } catch (GigiApiException e) {
                 e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH));
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-                resp.getWriter().println("interrupted");
             }
 
         } else if (req.getParameter("addExDom") != null) {
@@ -403,6 +459,12 @@ public class Manager extends Page {
                 return true;
             }
         });
+
+        vars.put("testValidMonths", TimeConditions.getInstance().getTestMonths());
+        vars.put("reverificationDays", TimeConditions.getInstance().getVerificationLimitDays());
+        vars.put("verificationFreshMonths", TimeConditions.getInstance().getVerificationMonths());
+        vars.put("verificationMaxAgeMonths", TimeConditions.getInstance().getVerificationMaxAgeMonths());
+
         form.output(resp.getWriter(), getLanguage(req), vars);
     }
 }