]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/util/TestNotary.java
03f157f33b15d8d3de1e6fb08ba5ecd07244336c
[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.DatabaseConnection;
10 import org.cacert.gigi.database.GigiPreparedStatement;
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");
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");
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);
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         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `users` SET dob=NOW() - interval '15 years' WHERE id=?");
68         ps.setInt(1, id);
69         ps.execute();
70         User assurer = User.getById(id);
71         for (int i = 0; i < users.length; i++) {
72             assuranceFail(assurer, users[i], -1, "test-notary", "2014-01-01");
73             assuranceFail(assurer, users[i], 11, "test-notary", "2014-01-01");
74             Notary.assure(assurer, users[i], users[i].getName(), users[i].getDoB(), 10, "test-notary", "2014-01-01");
75             assuranceFail(assurer, users[i], 10, "test-notary", "2014-01-01");
76         }
77     }
78
79     @Test
80     public void testFail() throws SQLException, GigiApiException {
81         User assuranceUser = User.getById(createAssuranceUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
82         User assuree = User.getById(createVerifiedUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
83
84         // invalid date format
85         assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "2014-01-blah");
86         // empty date
87         assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "");
88         // null date
89         assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", null);
90         // null location
91         assuranceFail(assuranceUser, assuree, 10, null, "2014-01-01");
92         // empty location
93         assuranceFail(assuranceUser, assuree, 10, "", "2014-01-01");
94         // date in the future
95         assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", DateSelector.getDateFormat().format(new Date(System.currentTimeMillis() + 2 * 24 * 60 * 60 * 1000)));
96         // location too short
97         assuranceFail(assuranceUser, assuree, 10, "n", "2014-01-01");
98         // points too low
99         assuranceFail(assuranceUser, assuree, -1, "notary-junit-test", "2014-01-01");
100         // points too high
101         assuranceFail(assuranceUser, assuree, 11, "notary-junit-test", "2014-01-01");
102
103         // assure oneself
104         assuranceFail(assuranceUser, assuranceUser, 10, "notary-junit-test", "2014-01-01");
105         // not an assurer
106         assuranceFail(assuree, assuranceUser, 10, "notary-junit-test", "2014-01-01");
107
108         // valid
109         Notary.assure(assuranceUser, assuree, assuree.getName(), assuree.getDoB(), 10, "notary-junit-test", "2014-01-01");
110
111         // assure double
112         assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "2014-01-01");
113
114     }
115 }