]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/dbObjects/TestAssurance.java
add: handling of multiple verifications by the same RA Agent
[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.cacert.gigi.util.DayDate;
12 import org.cacert.gigi.util.Notary;
13 import org.junit.Before;
14 import org.junit.Test;
15
16 public class TestAssurance extends BusinessTest {
17
18     private final Timestamp yesterday = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY);
19
20     private final Timestamp tomorrow = new Timestamp(System.currentTimeMillis() + DayDate.MILLI_DAY);
21
22     /**
23      * at least 39 months ago, so is outside the window of
24      * {@link User#VERIFICATION_MONTHS}
25      */
26     private final Timestamp min39month = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 39 * 31);
27
28     /**
29      * at least 24 months ago (but less than 39), so is inside the window of
30      * {@link User#VERIFICATION_MONTHS}
31      */
32     private final Timestamp min24month = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 24 * 31);
33
34     private int agentID;
35
36     private int agent2ID;
37
38     private int applicantID;
39
40     private int applicantMultID;
41
42     public TestAssurance() throws GigiApiException {
43
44     }
45
46     // test for verification in 39 month period
47     private void enterAssurance(int agentID, int applicantID) {
48         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
49             ps.setInt(1, agentID);
50             ps.setInt(2, applicantID);
51             ps.setInt(3, 10);
52             ps.setString(4, "test-location");
53             ps.setString(5, "2010-01-01");
54
55             ps.execute();
56         }
57     }
58
59     private void enterAssuranceExpired(int agentID, int applicantID, Timestamp expired) {
60         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `expire`=? ")) {
61             ps.setInt(1, agentID);
62             ps.setInt(2, applicantID);
63             ps.setInt(3, 10);
64             ps.setString(4, "test-location");
65             ps.setString(5, "2010-01-01");
66             ps.setTimestamp(6, expired);
67             ps.execute();
68         }
69     }
70
71     private void enterAssuranceWhen(int agentID, int applicantID, Timestamp when) {
72         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
73             ps.setInt(1, agentID);
74             ps.setInt(2, applicantID);
75             ps.setInt(3, 10);
76             ps.setString(4, "test-location");
77             ps.setString(5, "2010-01-01");
78             ps.setTimestamp(6, when);
79             ps.execute();
80         }
81     }
82
83     private void enterAssuranceWhen(int agentID, int applicantID, Timestamp when, int points) {
84         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
85             ps.setInt(1, agentID);
86             ps.setInt(2, applicantID);
87             ps.setInt(3, points);
88             ps.setString(4, "test-location");
89             ps.setString(5, "2010-01-01");
90             ps.setTimestamp(6, when);
91             ps.execute();
92         }
93     }
94
95     private void enterAssuranceDeleted(int agentID, int applicantID, Timestamp deleted) {
96         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `deleted`=? ")) {
97             ps.setInt(1, agentID);
98             ps.setInt(2, applicantID);
99             ps.setInt(3, 10);
100             ps.setString(4, "test-location");
101             ps.setString(5, "2010-01-01");
102             ps.setTimestamp(6, deleted);
103             ps.execute();
104         }
105     }
106
107     @Before
108     public void initTest() throws GigiApiException {
109         agentID = createAssuranceUser("a", "b", createUniqueName() + "@example.com", TEST_PASSWORD);
110         agent2ID = createAssuranceUser("a", "d", createUniqueName() + "@example.com", TEST_PASSWORD);
111         applicantID = createVerifiedUser("a", "c", createUniqueName() + "@example.com", TEST_PASSWORD);
112         applicantMultID = createVerifiedUser("a", "e", createUniqueName() + "@example.com", TEST_PASSWORD);
113     }
114
115     @Test
116     public void testVerificationYesterday() throws IOException {
117         enterAssuranceWhen(agentID, applicantID, yesterday);
118         assertTrue(User.isInVerificationLimit(applicantID));
119     }
120
121     @Test
122     public void testApprox24MonthAgo() throws IOException {
123         enterAssuranceWhen(agentID, applicantID, min24month);
124         assertTrue(User.isInVerificationLimit(applicantID));
125     }
126
127     @Test
128     public void testApprox39MonthAgo() throws IOException {
129         enterAssuranceWhen(agentID, applicantID, min39month);
130         assertFalse(User.isInVerificationLimit(applicantID));
131     }
132
133     @Test
134     public void testTomorrowExpired() throws IOException {
135         enterAssuranceExpired(agentID, applicantID, tomorrow);
136         assertTrue(User.isInVerificationLimit(applicantID));
137     }
138
139     @Test
140     public void testYesterdayExpired() throws IOException {
141         enterAssuranceExpired(agentID, applicantID, yesterday);
142         assertFalse(User.isInVerificationLimit(applicantID));
143     }
144
145     @Test
146     public void testNormal() throws IOException {
147         enterAssurance(agentID, applicantID);
148         assertTrue(User.isInVerificationLimit(applicantID));
149     }
150
151     @Test
152     public void testDeletedYesterday() throws IOException {
153         enterAssuranceDeleted(agentID, applicantID, yesterday);
154         assertFalse(User.isInVerificationLimit(applicantID));
155     }
156
157     @Test
158     public void testMultipleAssurancePossible() throws IOException {
159
160         User agent = User.getById(agentID);
161         User applicantMult = User.getById(applicantMultID);
162
163         enterAssuranceWhen(agentID, applicantMultID, min39month);
164
165         // test that new entry would be possible
166         try {
167             Notary.checkAssuranceIsPossible(agent, applicantMult);
168         } catch (GigiApiException e) {
169             assertTrue(false);
170         }
171
172         // enter new entry
173         enterAssuranceWhen(agentID, applicantMultID, yesterday);
174
175         // test that new entry is not possible
176         try {
177             Notary.checkAssuranceIsPossible(agent, applicantMult);
178         } catch (GigiApiException e) {
179             assertTrue(true);
180         }
181
182     }
183
184     @Test
185     public void testMultipleAssurancePointsCalculation() throws IOException {
186
187         User agent = User.getById(agentID);
188         User applicantMult = User.getById(applicantMultID);
189
190         enterAssuranceWhen(agentID, applicantMultID, min39month);
191
192         int xPoints = agent.getExperiencePoints();
193
194         // test that VP after first entry
195
196         assertEquals(applicantMult.getAssurancePoints(), 10);
197
198         // enter second entry to check correct calculation with larger points
199         enterAssuranceWhen(agentID, applicantMultID, min24month, 20);
200         assertEquals(applicantMult.getAssurancePoints(), 20);
201
202         // test correct XP calculation
203         assertEquals(agent.getExperiencePoints(), xPoints);
204
205         // enter third entry to check correct calculation with less points
206         enterAssuranceWhen(agentID, applicantMultID, yesterday, 15);
207         assertEquals(applicantMult.getAssurancePoints(), 15);
208
209         // test correct XP calculation
210         assertEquals(agent.getExperiencePoints(), xPoints);
211
212         // enter expired entry
213         enterAssuranceExpired(agentID, applicantMultID, yesterday);
214         assertEquals(applicantMult.getAssurancePoints(), 15);
215
216         // enter deleted entry same agent
217         enterAssuranceDeleted(agentID, applicantMultID, yesterday);
218         assertEquals(applicantMult.getAssurancePoints(), 15);
219
220         // enter expired entry future
221         enterAssuranceExpired(agentID, applicantMultID, tomorrow);
222         assertEquals(applicantMult.getAssurancePoints(), 10);
223
224         // test correct XP calculation
225         assertEquals(agent.getExperiencePoints(), xPoints);
226
227         // enter entry from different agent
228         enterAssuranceWhen(agent2ID, applicantMultID, yesterday);
229         assertEquals(applicantMult.getAssurancePoints(), 20);
230
231         // enter entry for second applicant
232         enterAssuranceWhen(agentID, applicantID, yesterday);
233         assertEquals(agent.getExperiencePoints(), xPoints + 2);
234
235     }
236 }