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.GigiApiException;
9 import org.cacert.gigi.database.GigiPreparedStatement;
10 import org.cacert.gigi.testUtils.BusinessTest;
11 import org.junit.Test;
13 public class TestAssurance extends BusinessTest {
15 private final Timestamp yesterday = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 1000L);
17 private final Timestamp tomorrow = new Timestamp(System.currentTimeMillis() + 24L * 60 * 60 * 1000L);
20 * at least 39 months ago, so is outside the window of
21 * {@link User#VERIFICATION_MONTHS}
23 private final Timestamp min39month = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 39 * 31 * 1000L);
26 * at least 24 months ago (but less than 39), so is inside the window of
27 * {@link User#VERIFICATION_MONTHS}
29 private final Timestamp min24month = new Timestamp(System.currentTimeMillis() - 24L * 60 * 60 * 24 * 31 * 1000L);
31 private final int agentID;
33 private final int applicantID;
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);
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);
46 ps.setString(4, "test-location");
47 ps.setString(5, "2010-01-01");
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);
58 ps.setString(4, "test-location");
59 ps.setString(5, "2010-01-01");
60 ps.setTimestamp(6, expired);
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);
70 ps.setString(4, "test-location");
71 ps.setString(5, "2010-01-01");
72 ps.setTimestamp(6, when);
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);
82 ps.setString(4, "test-location");
83 ps.setString(5, "2010-01-01");
84 ps.setTimestamp(6, deleted);
90 public void testVerificationYesterday() throws IOException {
91 enterAssuranceWhen(agentID, applicantID, yesterday);
92 assertTrue(User.isInVerificationLimit(applicantID));
96 public void testApprox24MonthAgo() throws IOException {
97 enterAssuranceWhen(agentID, applicantID, min24month);
98 assertTrue(User.isInVerificationLimit(applicantID));
102 public void testApprox39MonthAgo() throws IOException {
103 enterAssuranceWhen(agentID, applicantID, min39month);
104 assertFalse(User.isInVerificationLimit(applicantID));
108 public void testTomorrowExpired() throws IOException {
109 enterAssuranceExpired(agentID, applicantID, tomorrow);
110 assertTrue(User.isInVerificationLimit(applicantID));
114 public void testYesterdayExpired() throws IOException {
115 enterAssuranceExpired(agentID, applicantID, yesterday);
116 assertFalse(User.isInVerificationLimit(applicantID));
120 public void testNormal() throws IOException {
121 enterAssurance(agentID, applicantID);
122 assertTrue(User.isInVerificationLimit(applicantID));
126 public void testDeletedYesterday() throws IOException {
127 enterAssuranceDeleted(agentID, applicantID, yesterday);
128 assertFalse(User.isInVerificationLimit(applicantID));