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