diff PythonTrustStore-0.2.0/py_ts/testKMS.py @ 1:ff126718bdc5

Uploaded
author cathywise
date Wed, 11 Dec 2013 21:05:12 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PythonTrustStore-0.2.0/py_ts/testKMS.py	Wed Dec 11 21:05:12 2013 -0500
@@ -0,0 +1,131 @@
+from py_ts import TrustStoreClient, ts_utils, parts
+import argparse
+import json
+import sys
+import psycopg2
+import unittest
+
+username = "foo4@test.com"
+password = "password"
+provider_file = "nectar.json"
+
+class TestTruststoreAPI(unittest.TestCase):
+
+    def setUp(self):
+        self.ts = TrustStoreClient.TrustStoreClient(True, None, None)
+        self.ts.authenticate(username=username, password=password)
+
+        providers = None
+        with open(provider_file) as f:
+            providers = json.load(f)
+            for provider in providers:
+                self.ts.addProvider(provider)
+
+        keyFile = "truststore.pem"
+        keyFile = self.ts.getPrivateKey("truststore.pem")
+        self.ts.keyFile = keyFile
+        if not keyFile:
+            print "No key file could be found! Check for errors above."
+            sys.exit(1)
+
+        db = psycopg2.connect("dbname=kmscolab user=kmscolab password=kms1243")
+        self.cur = db.cursor()
+
+
+    def test_empty_store(self):
+        """Testing an empty Store."""
+        blankStore = TrustStoreClient.Store()
+        blankStoreReturned = self.ts.createStore(store=blankStore)
+        self.assertIsNone(blankStoreReturned)
+
+    def test_no_owner_no_admin(self):
+        """Testing a store with no admin or owner."""
+        partialStore = TrustStoreClient.Store(friendly_name="test_no_owner_no_admin")
+        partialStoreReturned = self.ts.createStore(store=partialStore)
+        self.assertIsNone(partialStoreReturned)
+
+    def test_no_admin(self):
+        """Testing a store with no admin."""
+        partialStore2 = TrustStoreClient.Store(owner=username, friendly_name="test_no_admin")
+        partialStore2Returned = self.ts.createStore(store=partialStore2)
+        self.assertIsNone(partialStore2Returned)
+
+    def test_no_owner(self):
+        """Testing a store with no owner."""
+        partialStore3 = TrustStoreClient.Store(friendly_name="test_no_owner")
+        partialStore3.administrators.append(username)
+        partialStore3Returned = self.ts.createStore(store=partialStore3)
+        self.assertIsNone(partialStore3Returned)
+
+    def test_id_insert(self):
+        """Testing a store where we try and sneak in an ID"""
+        idStore = TrustStoreClient.Store(owner=username, friendly_name="test_id_insert")
+        idStore.administrators.append(username)
+        idStore.id = 10
+        idStoreReturned = self.ts.createStore(store=idStore)
+        self.assertIsNotNone(idStoreReturned)
+        self.assertNotEqual(idStore.id, idStoreReturned.id)
+
+        print "Tests in database."
+        self.cur.execute("select friendly_name from stores where friendly_name ilike 'test\\_%';")
+        self.assertEqual(len(self.cur.fetchall()), 1)
+
+        self.ts.deleteStore(idStoreReturned)
+
+        print "Tests in database (after deleting id test)."
+        self.cur.execute("select friendly_name from stores where friendly_name ilike 'test\\_%';")
+        self.assertEqual(len(self.cur.fetchall()), 0)
+
+    def test_bad_user(self):
+        """Test making a store where a user isn't valid."""
+        self.cur.execute("select count(*) from acl_entry;")
+        aclCount = self.cur.fetchall()
+        badUserStore = TrustStoreClient.Store(owner=username, friendly_name="test_bad_user")
+        badUserStore.administrators.append(username)
+        badUserStore.readers.append("someone_not_real@gmail.com")
+        badUserStoreReturned = self.ts.createStore(store=badUserStore)
+        self.assertIsNone(badUserStoreReturned)
+        self.cur.execute("select friendly_name from stores where friendly_name ilike 'test\\_%';")
+        testStores = self.cur.fetchall()
+        self.assertEqual(len(testStores), 0)
+        self.cur.execute("select count(*) from acl_entry;")
+        self.assertEqual(self.cur.fetchall(), aclCount)
+
+    def test_rights_savekey(self):
+        """Test if we can set a key we (probably) don't own."""
+        self.ts._setKeyForFragment("test-key", self.ts._generateKey(), 1)
+        self.cur.execute("select codename, key, id from colabkeys where id = 1;")
+        self.assertEqual(len(self.cur.fetchall()), 0)
+
+    def test_nostore_savekey(self):
+        """Test if we can set a key for a deleted store."""
+        self.ts._setKeyForFragment("test-key-nostore", self.ts._generateKey(), 10)
+        self.cur.execute("select codename, key, id from colabkeys where id = %s;", [10])
+        self.assertEqual(len(self.cur.fetchall()), 0)
+
+    def test_rights_getkey(self):
+        """Test if we can get a key we don't own."""
+        key = self.ts._getKeyForFragment("d32fe69b-60c4-49c7-a160-44a5edaaf855", 49)
+        self.assertIsNone(key)
+
+    def test_good_savekey(self):
+        """Test if we can save & delete a key."""
+        storeid = self.ts.listStores()[0].id
+        frag = parts.Fragment()
+        frag.name = "test-key"
+        print "savekey"
+        self.ts._setKeyForFragment(frag.name, self.ts._generateKey(), storeid)
+        self.ts._deleteKeysForFragments([frag], storeid)
+        print "deleted key"
+
+
+if __name__ == '__main__':
+    # parser = argparse.ArgumentParser(description="TrustStore Command Line Client (pyts)")
+    # parser.add_argument("user", help="TrustStore kms username")
+    # parser.add_argument("password", help="TrustStore kms password")
+    # parser.add_argument("provider", help="json file describing cloud service(s), including credentials")
+    # args = parser.parse_args()
+    # username = args.user
+    # password = args.password
+    # provider = args.provider
+    unittest.main()