]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/dbObjects/TestAssurance.java
add: check if the last valid verification is within the last 39 month
[gigi.git] / tests / org / cacert / gigi / dbObjects / TestAssurance.java
1 package org.cacert.gigi.dbObjects;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.sql.Timestamp;
7
8 import org.cacert.gigi.database.GigiPreparedStatement;
9 import org.cacert.gigi.testUtils.ManagedTest;
10 import org.junit.Test;
11
12 public class TestAssurance extends ManagedTest {
13
14     private final Timestamp yesterday = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 1000L);
15
16     private final Timestamp tomorrow = new Timestamp(System.currentTimeMillis() + 24L * 60 * 60 * 1000L);
17
18     /**
19      * at least 39 months ago, so is outside the window of
20      * {@link User#VERIFICATION_MONTHS}
21      */
22     private final Timestamp min39month = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 39 * 31 * 1000L);
23
24     /**
25      * at least 24 months ago (but less than 39), so is inside the window of
26      * {@link User#VERIFICATION_MONTHS}
27      */
28     private final Timestamp min24month = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 24 * 31 * 1000L);
29
30     private final int agentID;
31
32     private final int applicantID;
33
34     public TestAssurance() {
35         agentID = createAssuranceUser("a", "b", createUniqueName() + "@example.com", TEST_PASSWORD);
36         applicantID = createVerifiedUser("a", "c", createUniqueName() + "@example.com", TEST_PASSWORD);
37     }
38
39     // test for verification in 39 month period
40     private void enterAssurance(int agentID, int applicantID) {
41         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
42             ps.setInt(1, agentID);
43             ps.setInt(2, applicantID);
44             ps.setInt(3, 10);
45             ps.setString(4, "test-location");
46             ps.setString(5, "2010-01-01");
47
48             ps.execute();
49         }
50     }
51
52     private void enterAssuranceExpired(int agentID, int applicantID, Timestamp expired) {
53         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `expire`=? ")) {
54             ps.setInt(1, agentID);
55             ps.setInt(2, applicantID);
56             ps.setInt(3, 10);
57             ps.setString(4, "test-location");
58             ps.setString(5, "2010-01-01");
59             ps.setTimestamp(6, expired);
60             ps.execute();
61         }
62     }
63
64     private void enterAssuranceWhen(int agentID, int applicantID, Timestamp when) {
65         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
66             ps.setInt(1, agentID);
67             ps.setInt(2, applicantID);
68             ps.setInt(3, 10);
69             ps.setString(4, "test-location");
70             ps.setString(5, "2010-01-01");
71             ps.setTimestamp(6, when);
72             ps.execute();
73         }
74     }
75
76     private void enterAssuranceDeleted(int agentID, int applicantID, Timestamp deleted) {
77         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `deleted`=? ")) {
78             ps.setInt(1, agentID);
79             ps.setInt(2, applicantID);
80             ps.setInt(3, 10);
81             ps.setString(4, "test-location");
82             ps.setString(5, "2010-01-01");
83             ps.setTimestamp(6, deleted);
84             ps.execute();
85         }
86     }
87
88     @Test
89     public void testVerificationYesterday() throws IOException {
90         enterAssuranceWhen(agentID, applicantID, yesterday);
91         assertTrue(User.isInVerificationLimit(applicantID));
92     }
93
94     @Test
95     public void testApprox24MonthAgo() throws IOException {
96         enterAssuranceWhen(agentID, applicantID, min24month);
97         assertTrue(User.isInVerificationLimit(applicantID));
98     }
99
100     @Test
101     public void testApprox39MonthAgo() throws IOException {
102         enterAssuranceWhen(agentID, applicantID, min39month);
103         assertFalse(User.isInVerificationLimit(applicantID));
104     }
105
106     @Test
107     public void testTomorrowExpired() throws IOException {
108         enterAssuranceExpired(agentID, applicantID, tomorrow);
109         assertTrue(User.isInVerificationLimit(applicantID));
110     }
111
112     @Test
113     public void testYesterdayExpired() throws IOException {
114         enterAssuranceExpired(agentID, applicantID, yesterday);
115         assertFalse(User.isInVerificationLimit(applicantID));
116     }
117
118     @Test
119     public void testNormal() throws IOException {
120         enterAssurance(agentID, applicantID);
121         assertTrue(User.isInVerificationLimit(applicantID));
122     }
123
124     @Test
125     public void testDeletedYesterday() throws IOException {
126         enterAssuranceDeleted(agentID, applicantID, yesterday);
127         assertFalse(User.isInVerificationLimit(applicantID));
128     }
129 }