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