]> WPIA git - gigi.git/commitdiff
add: check if the last valid verification is within the last 39 month
authorINOPIAE <m.maengel@inopiae.de>
Sat, 9 Jul 2016 13:54:57 +0000 (15:54 +0200)
committerFelix Dörre <felix@dogcraft.de>
Sun, 10 Jul 2016 12:05:11 +0000 (14:05 +0200)
Change-Id: I1f6e01b65043a883a178588fb71913b3c68e85f1

src/org/cacert/gigi/dbObjects/User.java
tests/org/cacert/gigi/dbObjects/TestAssurance.java [new file with mode: 0644]

index 7c45661e8a13dab11a72c3392ea1b82610b7d93e..8e9bc762b227d88844edf05c0028b41df1488224 100644 (file)
@@ -49,6 +49,11 @@ public class User extends CertificateOwner {
 
     public static final boolean POJAM_ENABLED = false;
 
 
     public static final boolean POJAM_ENABLED = false;
 
+    /**
+     * Time in months a verification is considered "recent".
+     */
+    public static final int VERIFICATION_MONTHS = 39;
+
     protected User(GigiResultSet rs) {
         super(rs.getInt("id"));
         updateName(rs);
     protected User(GigiResultSet rs) {
         super(rs.getInt("id"));
         updateName(rs);
@@ -568,4 +573,14 @@ public class User extends CertificateOwner {
     private Assurance assuranceByRes(GigiResultSet res) {
         return new Assurance(res.getInt("id"), User.getById(res.getInt("from")), User.getById(res.getInt("to")), res.getString("location"), res.getString("method"), res.getInt("points"), res.getString("date"));
     }
     private Assurance assuranceByRes(GigiResultSet res) {
         return new Assurance(res.getInt("id"), User.getById(res.getInt("from")), User.getById(res.getInt("to")), res.getString("location"), res.getString("method"), res.getInt("points"), res.getString("date"));
     }
+
+    public static boolean isInVerificationLimit(int id) {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `notary` WHERE `to` = ? AND `when` > (now() - (interval '1 month' * ?)) AND (`expire` IS NULL OR `expire` > now()) AND `deleted` IS NULL;")) {
+            ps.setInt(1, id);
+            ps.setInt(2, VERIFICATION_MONTHS);
+
+            GigiResultSet rs = ps.executeQuery();
+            return rs.next();
+        }
+    }
 }
 }
diff --git a/tests/org/cacert/gigi/dbObjects/TestAssurance.java b/tests/org/cacert/gigi/dbObjects/TestAssurance.java
new file mode 100644 (file)
index 0000000..e7ea35e
--- /dev/null
@@ -0,0 +1,129 @@
+package org.cacert.gigi.dbObjects;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.sql.Timestamp;
+
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.testUtils.ManagedTest;
+import org.junit.Test;
+
+public class TestAssurance extends ManagedTest {
+
+    private final Timestamp yesterday = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 1000L);
+
+    private final Timestamp tomorrow = new Timestamp(System.currentTimeMillis() + 24L * 60 * 60 * 1000L);
+
+    /**
+     * at least 39 months ago, so is outside the window of
+     * {@link User#VERIFICATION_MONTHS}
+     */
+    private final Timestamp min39month = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 39 * 31 * 1000L);
+
+    /**
+     * at least 24 months ago (but less than 39), so is inside the window of
+     * {@link User#VERIFICATION_MONTHS}
+     */
+    private final Timestamp min24month = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 24 * 31 * 1000L);
+
+    private final int agentID;
+
+    private final int applicantID;
+
+    public TestAssurance() {
+        agentID = createAssuranceUser("a", "b", createUniqueName() + "@example.com", TEST_PASSWORD);
+        applicantID = createVerifiedUser("a", "c", createUniqueName() + "@example.com", TEST_PASSWORD);
+    }
+
+    // test for verification in 39 month period
+    private void enterAssurance(int agentID, int applicantID) {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
+            ps.setInt(1, agentID);
+            ps.setInt(2, applicantID);
+            ps.setInt(3, 10);
+            ps.setString(4, "test-location");
+            ps.setString(5, "2010-01-01");
+
+            ps.execute();
+        }
+    }
+
+    private void enterAssuranceExpired(int agentID, int applicantID, Timestamp expired) {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `expire`=? ")) {
+            ps.setInt(1, agentID);
+            ps.setInt(2, applicantID);
+            ps.setInt(3, 10);
+            ps.setString(4, "test-location");
+            ps.setString(5, "2010-01-01");
+            ps.setTimestamp(6, expired);
+            ps.execute();
+        }
+    }
+
+    private void enterAssuranceWhen(int agentID, int applicantID, Timestamp when) {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
+            ps.setInt(1, agentID);
+            ps.setInt(2, applicantID);
+            ps.setInt(3, 10);
+            ps.setString(4, "test-location");
+            ps.setString(5, "2010-01-01");
+            ps.setTimestamp(6, when);
+            ps.execute();
+        }
+    }
+
+    private void enterAssuranceDeleted(int agentID, int applicantID, Timestamp deleted) {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `deleted`=? ")) {
+            ps.setInt(1, agentID);
+            ps.setInt(2, applicantID);
+            ps.setInt(3, 10);
+            ps.setString(4, "test-location");
+            ps.setString(5, "2010-01-01");
+            ps.setTimestamp(6, deleted);
+            ps.execute();
+        }
+    }
+
+    @Test
+    public void testVerificationYesterday() throws IOException {
+        enterAssuranceWhen(agentID, applicantID, yesterday);
+        assertTrue(User.isInVerificationLimit(applicantID));
+    }
+
+    @Test
+    public void testApprox24MonthAgo() throws IOException {
+        enterAssuranceWhen(agentID, applicantID, min24month);
+        assertTrue(User.isInVerificationLimit(applicantID));
+    }
+
+    @Test
+    public void testApprox39MonthAgo() throws IOException {
+        enterAssuranceWhen(agentID, applicantID, min39month);
+        assertFalse(User.isInVerificationLimit(applicantID));
+    }
+
+    @Test
+    public void testTomorrowExpired() throws IOException {
+        enterAssuranceExpired(agentID, applicantID, tomorrow);
+        assertTrue(User.isInVerificationLimit(applicantID));
+    }
+
+    @Test
+    public void testYesterdayExpired() throws IOException {
+        enterAssuranceExpired(agentID, applicantID, yesterday);
+        assertFalse(User.isInVerificationLimit(applicantID));
+    }
+
+    @Test
+    public void testNormal() throws IOException {
+        enterAssurance(agentID, applicantID);
+        assertTrue(User.isInVerificationLimit(applicantID));
+    }
+
+    @Test
+    public void testDeletedYesterday() throws IOException {
+        enterAssuranceDeleted(agentID, applicantID, yesterday);
+        assertFalse(User.isInVerificationLimit(applicantID));
+    }
+}