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