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