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