1
|
1 from py_ts import TrustStoreClient, ts_utils, parts
|
|
2 import argparse
|
|
3 import json
|
|
4 import sys
|
|
5 import psycopg2
|
|
6 import unittest
|
|
7
|
|
8 username = "foo4@test.com"
|
|
9 password = "password"
|
|
10 provider_file = "nectar.json"
|
|
11
|
|
12 class TestTruststoreAPI(unittest.TestCase):
|
|
13
|
|
14 def setUp(self):
|
|
15 self.ts = TrustStoreClient.TrustStoreClient(True, None, None)
|
|
16 self.ts.authenticate(username=username, password=password)
|
|
17
|
|
18 providers = None
|
|
19 with open(provider_file) as f:
|
|
20 providers = json.load(f)
|
|
21 for provider in providers:
|
|
22 self.ts.addProvider(provider)
|
|
23
|
|
24 keyFile = "truststore.pem"
|
|
25 keyFile = self.ts.getPrivateKey("truststore.pem")
|
|
26 self.ts.keyFile = keyFile
|
|
27 if not keyFile:
|
|
28 print "No key file could be found! Check for errors above."
|
|
29 sys.exit(1)
|
|
30
|
|
31 db = psycopg2.connect("dbname=kmscolab user=kmscolab password=kms1243")
|
|
32 self.cur = db.cursor()
|
|
33
|
|
34
|
|
35 def test_empty_store(self):
|
|
36 """Testing an empty Store."""
|
|
37 blankStore = TrustStoreClient.Store()
|
|
38 blankStoreReturned = self.ts.createStore(store=blankStore)
|
|
39 self.assertIsNone(blankStoreReturned)
|
|
40
|
|
41 def test_no_owner_no_admin(self):
|
|
42 """Testing a store with no admin or owner."""
|
|
43 partialStore = TrustStoreClient.Store(friendly_name="test_no_owner_no_admin")
|
|
44 partialStoreReturned = self.ts.createStore(store=partialStore)
|
|
45 self.assertIsNone(partialStoreReturned)
|
|
46
|
|
47 def test_no_admin(self):
|
|
48 """Testing a store with no admin."""
|
|
49 partialStore2 = TrustStoreClient.Store(owner=username, friendly_name="test_no_admin")
|
|
50 partialStore2Returned = self.ts.createStore(store=partialStore2)
|
|
51 self.assertIsNone(partialStore2Returned)
|
|
52
|
|
53 def test_no_owner(self):
|
|
54 """Testing a store with no owner."""
|
|
55 partialStore3 = TrustStoreClient.Store(friendly_name="test_no_owner")
|
|
56 partialStore3.administrators.append(username)
|
|
57 partialStore3Returned = self.ts.createStore(store=partialStore3)
|
|
58 self.assertIsNone(partialStore3Returned)
|
|
59
|
|
60 def test_id_insert(self):
|
|
61 """Testing a store where we try and sneak in an ID"""
|
|
62 idStore = TrustStoreClient.Store(owner=username, friendly_name="test_id_insert")
|
|
63 idStore.administrators.append(username)
|
|
64 idStore.id = 10
|
|
65 idStoreReturned = self.ts.createStore(store=idStore)
|
|
66 self.assertIsNotNone(idStoreReturned)
|
|
67 self.assertNotEqual(idStore.id, idStoreReturned.id)
|
|
68
|
|
69 print "Tests in database."
|
|
70 self.cur.execute("select friendly_name from stores where friendly_name ilike 'test\\_%';")
|
|
71 self.assertEqual(len(self.cur.fetchall()), 1)
|
|
72
|
|
73 self.ts.deleteStore(idStoreReturned)
|
|
74
|
|
75 print "Tests in database (after deleting id test)."
|
|
76 self.cur.execute("select friendly_name from stores where friendly_name ilike 'test\\_%';")
|
|
77 self.assertEqual(len(self.cur.fetchall()), 0)
|
|
78
|
|
79 def test_bad_user(self):
|
|
80 """Test making a store where a user isn't valid."""
|
|
81 self.cur.execute("select count(*) from acl_entry;")
|
|
82 aclCount = self.cur.fetchall()
|
|
83 badUserStore = TrustStoreClient.Store(owner=username, friendly_name="test_bad_user")
|
|
84 badUserStore.administrators.append(username)
|
|
85 badUserStore.readers.append("someone_not_real@gmail.com")
|
|
86 badUserStoreReturned = self.ts.createStore(store=badUserStore)
|
|
87 self.assertIsNone(badUserStoreReturned)
|
|
88 self.cur.execute("select friendly_name from stores where friendly_name ilike 'test\\_%';")
|
|
89 testStores = self.cur.fetchall()
|
|
90 self.assertEqual(len(testStores), 0)
|
|
91 self.cur.execute("select count(*) from acl_entry;")
|
|
92 self.assertEqual(self.cur.fetchall(), aclCount)
|
|
93
|
|
94 def test_rights_savekey(self):
|
|
95 """Test if we can set a key we (probably) don't own."""
|
|
96 self.ts._setKeyForFragment("test-key", self.ts._generateKey(), 1)
|
|
97 self.cur.execute("select codename, key, id from colabkeys where id = 1;")
|
|
98 self.assertEqual(len(self.cur.fetchall()), 0)
|
|
99
|
|
100 def test_nostore_savekey(self):
|
|
101 """Test if we can set a key for a deleted store."""
|
|
102 self.ts._setKeyForFragment("test-key-nostore", self.ts._generateKey(), 10)
|
|
103 self.cur.execute("select codename, key, id from colabkeys where id = %s;", [10])
|
|
104 self.assertEqual(len(self.cur.fetchall()), 0)
|
|
105
|
|
106 def test_rights_getkey(self):
|
|
107 """Test if we can get a key we don't own."""
|
|
108 key = self.ts._getKeyForFragment("d32fe69b-60c4-49c7-a160-44a5edaaf855", 49)
|
|
109 self.assertIsNone(key)
|
|
110
|
|
111 def test_good_savekey(self):
|
|
112 """Test if we can save & delete a key."""
|
|
113 storeid = self.ts.listStores()[0].id
|
|
114 frag = parts.Fragment()
|
|
115 frag.name = "test-key"
|
|
116 print "savekey"
|
|
117 self.ts._setKeyForFragment(frag.name, self.ts._generateKey(), storeid)
|
|
118 self.ts._deleteKeysForFragments([frag], storeid)
|
|
119 print "deleted key"
|
|
120
|
|
121
|
|
122 if __name__ == '__main__':
|
|
123 # parser = argparse.ArgumentParser(description="TrustStore Command Line Client (pyts)")
|
|
124 # parser.add_argument("user", help="TrustStore kms username")
|
|
125 # parser.add_argument("password", help="TrustStore kms password")
|
|
126 # parser.add_argument("provider", help="json file describing cloud service(s), including credentials")
|
|
127 # args = parser.parse_args()
|
|
128 # username = args.user
|
|
129 # password = args.password
|
|
130 # provider = args.provider
|
|
131 unittest.main()
|