4028ebdaea80451673e7b7f8ca732048c16462be
[gigi.git] / tests / club / wpia / gigi / util / TestNotary.java
1 package club.wpia.gigi.util;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.sql.SQLException;
7 import java.util.Date;
8
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.CATS;
14 import club.wpia.gigi.dbObjects.CATS.CATSType;
15 import club.wpia.gigi.dbObjects.Country;
16 import club.wpia.gigi.dbObjects.Country.CountryCodeType;
17 import club.wpia.gigi.dbObjects.Group;
18 import club.wpia.gigi.dbObjects.Name;
19 import club.wpia.gigi.dbObjects.NamePart;
20 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
21 import club.wpia.gigi.dbObjects.ObjectCache;
22 import club.wpia.gigi.dbObjects.User;
23 import club.wpia.gigi.dbObjects.Verification.VerificationType;
24 import club.wpia.gigi.output.DateSelector;
25 import club.wpia.gigi.testUtils.BusinessTest;
26
27 public class TestNotary extends BusinessTest {
28
29     public final Country DE = Country.getCountryByCode("DE", CountryCodeType.CODE_2_CHARS);
30
31     public TestNotary() throws GigiApiException {}
32
33     @Test
34     public void testNormalVerification() throws SQLException, GigiApiException {
35         User[] users = new User[30];
36         for (int i = 0; i < users.length; i++) {
37             int id = createVerifiedUser("fn" + i, "ln" + i, createUniqueName() + "@email.org", TEST_PASSWORD);
38             users[i] = User.getById(id);
39         }
40         User agent = User.getById(createVerificationUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD));
41         int[] result = new int[] {
42                 10, 10, 10, 10, 15, 15, 15, 15, 15, 20, 20, 20, 20, 20, 25, 25, 25, 25, 25, 30, 30, 30, 30, 30, 35, 35, 35, 35, 35, 35
43         };
44
45         try {
46             Notary.verify(agent, users[0], users[0].getPreferredName(), users[0].getDoB(), -1, "test-notary", validVerificationDateString(), VerificationType.FACE_TO_FACE, DE);
47             fail("This shouldn't have passed");
48         } catch (GigiApiException e) {
49             // expected
50         }
51         for (int i = 0; i < result.length; i++) {
52             assertEquals(result[i], agent.getMaxVerifyPoints());
53
54             verificationFail(agent, users[i], result[i] + 1, "test-notary", validVerificationDateString());
55             Notary.verify(agent, users[i], users[i].getPreferredName(), users[i].getDoB(), result[i], "test-notary", validVerificationDateString(), VerificationType.FACE_TO_FACE, DE);
56             verificationFail(agent, users[i], result[i], "test-notary", validVerificationDateString());
57         }
58
59         assertEquals(35, agent.getMaxVerifyPoints());
60
61         assertEquals(User.EXPERIENCE_POINTS + (30 * User.EXPERIENCE_POINTS), agent.getExperiencePoints());
62
63     }
64
65     private void verificationFail(User agent, User applicant, int i, String location, String date) throws SQLException {
66         try {
67             Notary.verify(agent, applicant, applicant.getPreferredName(), applicant.getDoB(), i, location, date, VerificationType.FACE_TO_FACE, DE);
68             fail("This shouldn't have passed");
69         } catch (GigiApiException e) {
70             // expected
71         }
72     }
73
74     @Test
75     public void testPoJam() throws SQLException, GigiApiException {
76         User[] users = new User[30];
77         for (int i = 0; i < users.length; i++) {
78             int id = createVerifiedUser("fn" + i, "ln" + i, createUniqueName() + "@email.org", TEST_PASSWORD);
79             users[i] = User.getById(id);
80         }
81         int id = createVerificationUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD);
82         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `users` SET dob=NOW() - interval '15 years' WHERE id=?")) {
83             ps.setInt(1, id);
84             ps.execute();
85         }
86         ObjectCache.clearAllCaches(); // reload values from db
87         User agent = User.getById(id);
88         for (int i = 0; i < users.length; i++) {
89             verificationFail(agent, users[i], -1, "test-notary", validVerificationDateString());
90             verificationFail(agent, users[i], 11, "test-notary", validVerificationDateString());
91             if (User.POJAM_ENABLED) {
92                 Notary.verify(agent, users[i], users[i].getPreferredName(), users[i].getDoB(), 10, "test-notary", validVerificationDateString(), VerificationType.FACE_TO_FACE, DE);
93             }
94             verificationFail(agent, users[i], 10, "test-notary", validVerificationDateString());
95         }
96     }
97
98     @Test
99     public void testFail() throws SQLException, GigiApiException {
100         User agent = User.getById(createVerificationUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
101         User applicant = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
102
103         // invalid date format
104         verificationFail(agent, applicant, 10, "notary-junit-test", "2014-01-blah");
105         // empty date
106         verificationFail(agent, applicant, 10, "notary-junit-test", "");
107         // null date
108         verificationFail(agent, applicant, 10, "notary-junit-test", null);
109         // null location
110         verificationFail(agent, applicant, 10, null, validVerificationDateString());
111         // empty location
112         verificationFail(agent, applicant, 10, "", validVerificationDateString());
113         // date in the future
114         verificationFail(agent, applicant, 10, "notary-junit-test", DateSelector.getDateFormat().format(new Date(System.currentTimeMillis() + 2 * 24 * 60 * 60 * 1000)));
115         // location too short
116         verificationFail(agent, applicant, 10, "n", validVerificationDateString());
117         // points too low
118         verificationFail(agent, applicant, -1, "notary-junit-test", validVerificationDateString());
119         // points too high
120         verificationFail(agent, applicant, 11, "notary-junit-test", validVerificationDateString());
121
122         // verify oneself
123         verificationFail(agent, agent, 10, "notary-junit-test", validVerificationDateString());
124         // not an agent
125         verificationFail(applicant, agent, 10, "notary-junit-test", validVerificationDateString());
126
127         // valid
128         Notary.verify(agent, applicant, applicant.getPreferredName(), applicant.getDoB(), 10, "notary-junit-test", validVerificationDateString(), VerificationType.FACE_TO_FACE, DE);
129
130         // verify double
131         verificationFail(agent, applicant, 10, "notary-junit-test", validVerificationDateString());
132
133     }
134
135     @Test
136     public void testNucleus() throws SQLException, GigiApiException, IOException {
137         User agent = User.getById(createVerificationUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
138         agent.grantGroup(getSupporter(), Group.NUCLEUS_AGENT);
139         User applicant = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
140         Name n1 = applicant.getPreferredName();
141         Name n2 = new Name(applicant, new NamePart(NamePartType.FIRST_NAME, "F2"), new NamePart(NamePartType.LAST_NAME, "L2"));
142
143         assertEquals(0, applicant.getExperiencePoints());
144         assertEquals(User.EXPERIENCE_POINTS, agent.getExperiencePoints());
145         assertEquals(0, applicant.getVerificationPoints());
146         assertEquals(0, n2.getVerificationPoints());
147         Notary.verifyAll(agent, applicant, applicant.getDoB(), 50, "notary-junit-test", validVerificationDateString(), VerificationType.NUCLEUS, new Name[] {
148                 n1, n2
149         }, DE);
150         assertEquals(0, applicant.getExperiencePoints());
151         assertEquals(2 * User.EXPERIENCE_POINTS, agent.getExperiencePoints());
152         assertEquals(50, applicant.getVerificationPoints());
153         assertEquals(50, n1.getVerificationPoints());
154         assertEquals(50, n2.getVerificationPoints());
155     }
156
157     @Test
158     public void testNucleusProcess() throws SQLException, GigiApiException, IOException {
159         User agent1 = User.getById(createVerificationUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
160         agent1.grantGroup(getSupporter(), Group.NUCLEUS_AGENT);
161         User agent2 = User.getById(createVerificationUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
162         agent2.grantGroup(getSupporter(), Group.NUCLEUS_AGENT);
163         User applicant = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
164         Notary.verify(agent1, applicant, applicant.getPreferredName(), applicant.getDoB(), 50, "test", validVerificationDateString(), VerificationType.NUCLEUS, DE);
165         Notary.verify(agent2, applicant, applicant.getPreferredName(), applicant.getDoB(), 50, "test", validVerificationDateString(), VerificationType.NUCLEUS, DE);
166
167         assertEquals(100, applicant.getVerificationPoints());
168         assertFalse(applicant.canVerify());
169         CATS.enterResult(applicant, CATSType.AGENT_CHALLENGE, new Date(), "de", "1");
170         assertTrue(applicant.canVerify());
171     }
172 }