]> WPIA git - gigi.git/commitdiff
Additional testcases for failing assurances.
authorFelix Dörre <felix@dogcraft.de>
Thu, 4 Sep 2014 17:34:56 +0000 (19:34 +0200)
committerFelix Dörre <felix@dogcraft.de>
Thu, 4 Sep 2014 17:34:56 +0000 (19:34 +0200)
tests/org/cacert/gigi/util/TestNotary.java

index 5191b07d2109d808e815a7d37e6b706a3409ddcc..2f99840d5fa9a3eef76b91f441849db482af7335 100644 (file)
@@ -2,11 +2,13 @@ package org.cacert.gigi.util;
 
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
+import java.util.Date;
 
+import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.dbObjects.User;
+import org.cacert.gigi.output.DateSelector;
 import org.cacert.gigi.testUtils.ManagedTest;
-import org.cacert.gigi.util.Notary.AssuranceResult;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -14,7 +16,7 @@ import static org.junit.Assert.*;
 public class TestNotary extends ManagedTest {
 
     @Test
-    public void testNormalAssurance() throws SQLException {
+    public void testNormalAssurance() throws SQLException, GigiApiException {
         User[] users = new User[30];
         for (int i = 0; i < users.length; i++) {
             int id = createVerifiedUser("fn" + i, "ln" + i, createUniqueName() + "@email.org", TEST_PASSWORD);
@@ -25,12 +27,18 @@ public class TestNotary extends ManagedTest {
                 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
         };
 
-        assertNotEquals(AssuranceResult.ASSURANCE_SUCCEDED, Notary.assure(assurer, users[0], -1, "test-notary", "2014-01-01"));
+        try {
+            Notary.assure(assurer, users[0], -1, "test-notary", "2014-01-01");
+            fail("This shouldn't have passed");
+        } catch (GigiApiException e) {
+            // expected
+        }
         for (int i = 0; i < result.length; i++) {
             assertEquals(result[i], assurer.getMaxAssurePoints());
-            assertNotEquals(AssuranceResult.ASSURANCE_SUCCEDED, Notary.assure(assurer, users[i], result[i] + 1, "test-notary", "2014-01-01"));
-            assertEquals(AssuranceResult.ASSURANCE_SUCCEDED, Notary.assure(assurer, users[i], result[i], "test-notary", "2014-01-01"));
-            assertNotEquals(AssuranceResult.ASSURANCE_SUCCEDED, Notary.assure(assurer, users[i], result[i], "test-notary", "2014-01-01"));
+
+            assuranceFail(assurer, users[i], result[i] + 1, "test-notary", "2014-01-01");
+            Notary.assure(assurer, users[i], result[i], "test-notary", "2014-01-01");
+            assuranceFail(assurer, users[i], result[i], "test-notary", "2014-01-01");
         }
 
         assertEquals(35, assurer.getMaxAssurePoints());
@@ -39,8 +47,17 @@ public class TestNotary extends ManagedTest {
 
     }
 
+    private void assuranceFail(User assurer, User user, int i, String location, String date) throws SQLException {
+        try {
+            Notary.assure(assurer, user, i, location, date);
+            fail("This shouldn't have passed");
+        } catch (GigiApiException e) {
+            // expected
+        }
+    }
+
     @Test
-    public void testPoJam() throws SQLException {
+    public void testPoJam() throws SQLException, GigiApiException {
         User[] users = new User[30];
         for (int i = 0; i < users.length; i++) {
             int id = createVerifiedUser("fn" + i, "ln" + i, createUniqueName() + "@email.org", TEST_PASSWORD);
@@ -52,10 +69,47 @@ public class TestNotary extends ManagedTest {
         ps.execute();
         User assurer = new User(id);
         for (int i = 0; i < users.length; i++) {
-            assertNotEquals(AssuranceResult.ASSURANCE_SUCCEDED, Notary.assure(assurer, users[i], -1, "test-notary", "2014-01-01"));
-            assertNotEquals(AssuranceResult.ASSURANCE_SUCCEDED, Notary.assure(assurer, users[i], 11, "test-notary", "2014-01-01"));
-            assertEquals(AssuranceResult.ASSURANCE_SUCCEDED, Notary.assure(assurer, users[i], 10, "test-notary", "2014-01-01"));
-            assertNotEquals(AssuranceResult.ASSURANCE_SUCCEDED, Notary.assure(assurer, users[i], 10, "test-notary", "2014-01-01"));
+            assuranceFail(assurer, users[i], -1, "test-notary", "2014-01-01");
+            assuranceFail(assurer, users[i], 11, "test-notary", "2014-01-01");
+            Notary.assure(assurer, users[i], 10, "test-notary", "2014-01-01");
+            assuranceFail(assurer, users[i], 10, "test-notary", "2014-01-01");
         }
     }
+
+    @Test
+    public void testFail() throws SQLException, GigiApiException {
+        User assuranceUser = new User(createAssuranceUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
+        User assuree = new User(createVerifiedUser("fn", "ln", createUniqueName() + "@example.org", TEST_PASSWORD));
+
+        // invalid date format
+        assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "2014-01-blah");
+        // empty date
+        assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "");
+        // null date
+        assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", null);
+        // null location
+        assuranceFail(assuranceUser, assuree, 10, null, "2014-01-01");
+        // empty location
+        assuranceFail(assuranceUser, assuree, 10, "", "2014-01-01");
+        // date in the future
+        assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", DateSelector.getDateFormat().format(new Date(System.currentTimeMillis() + 2 * 24 * 60 * 60 * 1000)));
+        // location too short
+        assuranceFail(assuranceUser, assuree, 10, "n", "2014-01-01");
+        // points too low
+        assuranceFail(assuranceUser, assuree, -1, "notary-junit-test", "2014-01-01");
+        // points too high
+        assuranceFail(assuranceUser, assuree, 11, "notary-junit-test", "2014-01-01");
+
+        // assure oneself
+        assuranceFail(assuranceUser, assuranceUser, 10, "notary-junit-test", "2014-01-01");
+        // not an assurer
+        assuranceFail(assuree, assuranceUser, 10, "notary-junit-test", "2014-01-01");
+
+        // valid
+        Notary.assure(assuranceUser, assuree, 10, "notary-junit-test", "2014-01-01");
+
+        // assure double
+        assuranceFail(assuranceUser, assuree, 10, "notary-junit-test", "2014-01-01");
+
+    }
 }