]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/util/TestNotary.java
740c12c6ae10f947dc853ca3db6d0142d5902fd1
[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.User;
11 import org.cacert.gigi.output.DateSelector;
12 import org.cacert.gigi.testUtils.ManagedTest;
13 import org.junit.Test;
14
15 public class TestNotary extends ManagedTest {
16
17     @Test
18     public void testNormalAssurance() throws SQLException, GigiApiException {
19         User[] users = new User[30];
20         for (int i = 0; i < users.length; i++) {
21             int id = createVerifiedUser("fn" + i, "ln" + i, createUniqueName() + "@email.org", TEST_PASSWORD);
22             users[i] = User.getById(id);
23         }
24         User assurer = User.getById(createAssuranceUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD));
25         int[] result = new int[] {
26                 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
27         };
28
29         try {
30             Notary.assure(assurer, users[0], users[0].getName(), users[0].getDoB(), -1, "test-notary", "2014-01-01");
31             fail("This shouldn't have passed");
32         } catch (GigiApiException e) {
33             // expected
34         }
35         for (int i = 0; i < result.length; i++) {
36             assertEquals(result[i], assurer.getMaxAssurePoints());
37
38             assuranceFail(assurer, users[i], result[i] + 1, "test-notary", "2014-01-01");
39             Notary.assure(assurer, users[i], users[i].getName(), users[i].getDoB(), result[i], "test-notary", "2014-01-01");
40             assuranceFail(assurer, users[i], result[i], "test-notary", "2014-01-01");
41         }
42
43         assertEquals(35, assurer.getMaxAssurePoints());
44
45         assertEquals(2 + 60, assurer.getExperiencePoints());
46
47     }
48
49     private void assuranceFail(User assurer, User user, int i, String location, String date) throws SQLException {
50         try {
51             Notary.assure(assurer, user, user.getName(), user.getDoB(), i, location, date);
52             fail("This shouldn't have passed");
53         } catch (GigiApiException e) {
54             // expected
55         }
56     }
57
58     @Test
59     public void testPoJam() throws SQLException, GigiApiException {
60         User[] users = new User[30];
61         for (int i = 0; i < users.length; i++) {
62             int id = createVerifiedUser("fn" + i, "ln" + i, createUniqueName() + "@email.org", TEST_PASSWORD);
63             users[i] = User.getById(id);
64         }
65         int id = createAssuranceUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD);
66         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `users` SET dob=NOW() - interval '15 years' WHERE id=?")) {
67             ps.setInt(1, id);
68             ps.execute();
69         }
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 }