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