]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/util/TestNotary.java
Merge branch 'libs/jetty/local'
[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.User;
13 import org.cacert.gigi.output.DateSelector;
14 import org.cacert.gigi.testUtils.ManagedTest;
15 import org.junit.Test;
16
17 public class TestNotary extends ManagedTest {
18
19     // These tests create a lot of users and therefore require resetting of the
20     // registering-rate-limit.
21     @Test
22     public void testNormalAssurance() throws SQLException, GigiApiException {
23         try {
24             clearCaches();
25         } catch (IOException e) {
26             throw new Error(e);
27         }
28         User[] users = new User[30];
29         for (int i = 0; i < users.length; i++) {
30             int id = createVerifiedUser("fn" + i, "ln" + i, createUniqueName() + "@email.org", TEST_PASSWORD);
31             users[i] = User.getById(id);
32         }
33         User assurer = User.getById(createAssuranceUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD));
34         int[] result = new int[] {
35                 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
36         };
37
38         try {
39             Notary.assure(assurer, users[0], users[0].getName(), users[0].getDoB(), -1, "test-notary", "2014-01-01", AssuranceType.FACE_TO_FACE);
40             fail("This shouldn't have passed");
41         } catch (GigiApiException e) {
42             // expected
43         }
44         for (int i = 0; i < result.length; i++) {
45             assertEquals(result[i], assurer.getMaxAssurePoints());
46
47             assuranceFail(assurer, users[i], result[i] + 1, "test-notary", "2014-01-01");
48             Notary.assure(assurer, users[i], users[i].getName(), users[i].getDoB(), result[i], "test-notary", "2014-01-01", AssuranceType.FACE_TO_FACE);
49             assuranceFail(assurer, users[i], result[i], "test-notary", "2014-01-01");
50         }
51
52         assertEquals(35, assurer.getMaxAssurePoints());
53
54         assertEquals(2 + 60, assurer.getExperiencePoints());
55
56     }
57
58     private void assuranceFail(User assurer, User user, int i, String location, String date) throws SQLException {
59         try {
60             Notary.assure(assurer, user, user.getName(), user.getDoB(), i, location, date, AssuranceType.FACE_TO_FACE);
61             fail("This shouldn't have passed");
62         } catch (GigiApiException e) {
63             // expected
64         }
65     }
66
67     @Test
68     public void testPoJam() throws SQLException, GigiApiException {
69         try {
70             clearCaches();
71         } catch (IOException e) {
72             throw new Error(e);
73         }
74         User[] users = new User[30];
75         for (int i = 0; i < users.length; i++) {
76             int id = createVerifiedUser("fn" + i, "ln" + i, createUniqueName() + "@email.org", TEST_PASSWORD);
77             users[i] = User.getById(id);
78         }
79         int id = createAssuranceUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD);
80         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `users` SET dob=NOW() - interval '15 years' WHERE id=?")) {
81             ps.setInt(1, id);
82             ps.execute();
83         }
84         User assurer = User.getById(id);
85         for (int i = 0; i < users.length; i++) {
86             assuranceFail(assurer, users[i], -1, "test-notary", "2014-01-01");
87             assuranceFail(assurer, users[i], 11, "test-notary", "2014-01-01");
88             if (User.POJAM_ENABLED) {
89                 Notary.assure(assurer, users[i], users[i].getName(), users[i].getDoB(), 10, "test-notary", "2014-01-01", AssuranceType.FACE_TO_FACE);
90             }
91             assuranceFail(assurer, users[i], 10, "test-notary", "2014-01-01");
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, "2014-01-01");
108         // empty location
109         assuranceFail(assuranceUser, assuree, 10, "", "2014-01-01");
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", "2014-01-01");
114         // points too low
115         assuranceFail(assuranceUser, assuree, -1, "notary-junit-test", "2014-01-01");
116         // points too high
117         assuranceFail(assuranceUser, assuree, 11, "notary-junit-test", "2014-01-01");
118
119         // assure oneself
120         assuranceFail(assuranceUser, assuranceUser, 10, "notary-junit-test", "2014-01-01");
121         // not an assurer
122         assuranceFail(assuree, assuranceUser, 10, "notary-junit-test", "2014-01-01");
123
124         // valid
125         Notary.assure(assuranceUser, assuree, assuree.getName(), assuree.getDoB(), 10, "notary-junit-test", "2014-01-01", AssuranceType.FACE_TO_FACE);
126
127         // assure double
128         assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "2014-01-01");
129
130     }
131 }