]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/util/TestNotary.java
general cleanup
[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.sql.SQLException;
6 import java.util.Date;
7
8 import org.cacert.gigi.GigiApiException;
9 import org.cacert.gigi.database.GigiPreparedStatement;
10 import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
11 import org.cacert.gigi.dbObjects.User;
12 import org.cacert.gigi.output.DateSelector;
13 import org.cacert.gigi.testUtils.ManagedTest;
14 import org.junit.Test;
15
16 public class TestNotary extends ManagedTest {
17
18     @Test
19     public void testNormalAssurance() throws SQLException, GigiApiException {
20         User[] users = new User[30];
21         for (int i = 0; i < users.length; i++) {
22             int id = createVerifiedUser("fn" + i, "ln" + i, createUniqueName() + "@email.org", TEST_PASSWORD);
23             users[i] = User.getById(id);
24         }
25         User assurer = User.getById(createAssuranceUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD));
26         int[] result = new int[] {
27                 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
28         };
29
30         try {
31             Notary.assure(assurer, users[0], users[0].getName(), users[0].getDoB(), -1, "test-notary", "2014-01-01", AssuranceType.FACE_TO_FACE);
32             fail("This shouldn't have passed");
33         } catch (GigiApiException e) {
34             // expected
35         }
36         for (int i = 0; i < result.length; i++) {
37             assertEquals(result[i], assurer.getMaxAssurePoints());
38
39             assuranceFail(assurer, users[i], result[i] + 1, "test-notary", "2014-01-01");
40             Notary.assure(assurer, users[i], users[i].getName(), users[i].getDoB(), result[i], "test-notary", "2014-01-01", AssuranceType.FACE_TO_FACE);
41             assuranceFail(assurer, users[i], result[i], "test-notary", "2014-01-01");
42         }
43
44         assertEquals(35, assurer.getMaxAssurePoints());
45
46         assertEquals(2 + 60, assurer.getExperiencePoints());
47
48     }
49
50     private void assuranceFail(User assurer, User user, int i, String location, String date) throws SQLException {
51         try {
52             Notary.assure(assurer, user, user.getName(), user.getDoB(), i, location, date, AssuranceType.FACE_TO_FACE);
53             fail("This shouldn't have passed");
54         } catch (GigiApiException e) {
55             // expected
56         }
57     }
58
59     @Test
60     public void testPoJam() throws SQLException, GigiApiException {
61         User[] users = new User[30];
62         for (int i = 0; i < users.length; i++) {
63             int id = createVerifiedUser("fn" + i, "ln" + i, createUniqueName() + "@email.org", TEST_PASSWORD);
64             users[i] = User.getById(id);
65         }
66         int id = createAssuranceUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD);
67         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `users` SET dob=NOW() - interval '15 years' WHERE id=?")) {
68             ps.setInt(1, id);
69             ps.execute();
70         }
71         User assurer = User.getById(id);
72         for (int i = 0; i < users.length; i++) {
73             assuranceFail(assurer, users[i], -1, "test-notary", "2014-01-01");
74             assuranceFail(assurer, users[i], 11, "test-notary", "2014-01-01");
75             Notary.assure(assurer, users[i], users[i].getName(), users[i].getDoB(), 10, "test-notary", "2014-01-01", AssuranceType.FACE_TO_FACE);
76             assuranceFail(assurer, users[i], 10, "test-notary", "2014-01-01");
77         }
78     }
79
80     @Test
81     public void testFail() throws SQLException, GigiApiException {
82         User assuranceUser = User.getById(createAssuranceUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
83         User assuree = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
84
85         // invalid date format
86         assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "2014-01-blah");
87         // empty date
88         assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "");
89         // null date
90         assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", null);
91         // null location
92         assuranceFail(assuranceUser, assuree, 10, null, "2014-01-01");
93         // empty location
94         assuranceFail(assuranceUser, assuree, 10, "", "2014-01-01");
95         // date in the future
96         assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", DateSelector.getDateFormat().format(new Date(System.currentTimeMillis() + 2 * 24 * 60 * 60 * 1000)));
97         // location too short
98         assuranceFail(assuranceUser, assuree, 10, "n", "2014-01-01");
99         // points too low
100         assuranceFail(assuranceUser, assuree, -1, "notary-junit-test", "2014-01-01");
101         // points too high
102         assuranceFail(assuranceUser, assuree, 11, "notary-junit-test", "2014-01-01");
103
104         // assure oneself
105         assuranceFail(assuranceUser, assuranceUser, 10, "notary-junit-test", "2014-01-01");
106         // not an assurer
107         assuranceFail(assuree, assuranceUser, 10, "notary-junit-test", "2014-01-01");
108
109         // valid
110         Notary.assure(assuranceUser, assuree, assuree.getName(), assuree.getDoB(), 10, "notary-junit-test", "2014-01-01", AssuranceType.FACE_TO_FACE);
111
112         // assure double
113         assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "2014-01-01");
114
115     }
116 }