1 package org.cacert.gigi.util;
3 import static org.junit.Assert.*;
5 import java.sql.SQLException;
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;
16 public class TestNotary extends ManagedTest {
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);
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
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) {
36 for (int i = 0; i < result.length; i++) {
37 assertEquals(result[i], assurer.getMaxAssurePoints());
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");
44 assertEquals(35, assurer.getMaxAssurePoints());
46 assertEquals(2 + 60, assurer.getExperiencePoints());
50 private void assuranceFail(User assurer, User user, int i, String location, String date) throws SQLException {
52 Notary.assure(assurer, user, user.getName(), user.getDob(), i, location, date);
53 fail("This shouldn't have passed");
54 } catch (GigiApiException e) {
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);
66 int id = createAssuranceUser("fn", "ln", createUniqueName() + "@email.org", TEST_PASSWORD);
67 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET dob=TIMESTAMPADD(YEAR,-15,NOW()) WHERE id=?");
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");
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));
84 // invalid date format
85 assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "2014-01-blah");
87 assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "");
89 assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", null);
91 assuranceFail(assuranceUser, assuree, 10, null, "2014-01-01");
93 assuranceFail(assuranceUser, assuree, 10, "", "2014-01-01");
95 assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", DateSelector.getDateFormat().format(new Date(System.currentTimeMillis() + 2 * 24 * 60 * 60 * 1000)));
97 assuranceFail(assuranceUser, assuree, 10, "n", "2014-01-01");
99 assuranceFail(assuranceUser, assuree, -1, "notary-junit-test", "2014-01-01");
101 assuranceFail(assuranceUser, assuree, 11, "notary-junit-test", "2014-01-01");
104 assuranceFail(assuranceUser, assuranceUser, 10, "notary-junit-test", "2014-01-01");
106 assuranceFail(assuree, assuranceUser, 10, "notary-junit-test", "2014-01-01");
109 Notary.assure(assuranceUser, assuree, assuree.getName(), assuree.getDob(), 10, "notary-junit-test", "2014-01-01");
112 assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "2014-01-01");