1 package org.cacert.gigi.dbObjects;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.sql.Timestamp;
8 import org.cacert.gigi.database.GigiPreparedStatement;
9 import org.cacert.gigi.testUtils.ManagedTest;
10 import org.junit.Test;
12 public class TestAssurance extends ManagedTest {
14 private final Timestamp yesterday = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 1000L);
16 private final Timestamp tomorrow = new Timestamp(System.currentTimeMillis() + 24L * 60 * 60 * 1000L);
19 * at least 39 months ago, so is outside the window of
20 * {@link User#VERIFICATION_MONTHS}
22 private final Timestamp min39month = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 39 * 31 * 1000L);
25 * at least 24 months ago (but less than 39), so is inside the window of
26 * {@link User#VERIFICATION_MONTHS}
28 private final Timestamp min24month = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 24 * 31 * 1000L);
30 private final int agentID;
32 private final int applicantID;
34 public TestAssurance() {
35 agentID = createAssuranceUser("a", "b", createUniqueName() + "@example.com", TEST_PASSWORD);
36 applicantID = createVerifiedUser("a", "c", createUniqueName() + "@example.com", TEST_PASSWORD);
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);
45 ps.setString(4, "test-location");
46 ps.setString(5, "2010-01-01");
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);
57 ps.setString(4, "test-location");
58 ps.setString(5, "2010-01-01");
59 ps.setTimestamp(6, expired);
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);
69 ps.setString(4, "test-location");
70 ps.setString(5, "2010-01-01");
71 ps.setTimestamp(6, when);
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);
81 ps.setString(4, "test-location");
82 ps.setString(5, "2010-01-01");
83 ps.setTimestamp(6, deleted);
89 public void testVerificationYesterday() throws IOException {
90 enterAssuranceWhen(agentID, applicantID, yesterday);
91 assertTrue(User.isInVerificationLimit(applicantID));
95 public void testApprox24MonthAgo() throws IOException {
96 enterAssuranceWhen(agentID, applicantID, min24month);
97 assertTrue(User.isInVerificationLimit(applicantID));
101 public void testApprox39MonthAgo() throws IOException {
102 enterAssuranceWhen(agentID, applicantID, min39month);
103 assertFalse(User.isInVerificationLimit(applicantID));
107 public void testTomorrowExpired() throws IOException {
108 enterAssuranceExpired(agentID, applicantID, tomorrow);
109 assertTrue(User.isInVerificationLimit(applicantID));
113 public void testYesterdayExpired() throws IOException {
114 enterAssuranceExpired(agentID, applicantID, yesterday);
115 assertFalse(User.isInVerificationLimit(applicantID));
119 public void testNormal() throws IOException {
120 enterAssurance(agentID, applicantID);
121 assertTrue(User.isInVerificationLimit(applicantID));
125 public void testDeletedYesterday() throws IOException {
126 enterAssuranceDeleted(agentID, applicantID, yesterday);
127 assertFalse(User.isInVerificationLimit(applicantID));