91 lines
3.0 KiB
Python
91 lines
3.0 KiB
Python
import unittest
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
from ca_core import entity
|
|
from ca_core import property as prop # property.py
|
|
|
|
class TestProperty(unittest.TestCase):
|
|
"""Unit tests for property functions."""
|
|
|
|
def setUp(self):
|
|
self.conn = psycopg.connect("dbname=ca")
|
|
self.conn.autocommit = False
|
|
self.cur = self.conn.cursor(row_factory=dict_row)
|
|
|
|
# Clean tables before running
|
|
self.cur.execute("TRUNCATE TABLE property CASCADE;")
|
|
self.cur.execute("TRUNCATE TABLE entity CASCADE;")
|
|
|
|
# Insert a creator
|
|
self.creator_id = entity.insert_creator(self.cur, "Alice", "pubkeyA")
|
|
|
|
# Insert a regular person
|
|
self.person_id = entity.enroll_person(
|
|
self.cur, "Bob", "pubkeyB", self.creator_id
|
|
)
|
|
|
|
# Insert a group
|
|
self.group_id = entity.create_group(
|
|
self.cur, "Admins", "gpub", self.creator_id
|
|
)
|
|
|
|
def tearDown(self):
|
|
self.conn.rollback()
|
|
self.cur.close()
|
|
self.conn.close()
|
|
|
|
# -------------------------
|
|
# Set property
|
|
# -------------------------
|
|
def test_set_property(self):
|
|
prop.set_property(self.cur, self.person_id, "admin")
|
|
props = prop.get_properties(self.cur, self.person_id)
|
|
self.assertIn("admin", props)
|
|
|
|
# Setting same property again should not duplicate
|
|
prop.set_property(self.cur, self.person_id, "admin")
|
|
props = prop.get_properties(self.cur, self.person_id)
|
|
self.assertEqual(props.count("admin"), 1)
|
|
|
|
# -------------------------
|
|
# Delete property
|
|
# -------------------------
|
|
def test_delete_property(self):
|
|
prop.set_property(self.cur, self.person_id, "vip")
|
|
prop.delete_property(self.cur, self.person_id, "vip")
|
|
props = prop.get_properties(self.cur, self.person_id)
|
|
self.assertNotIn("vip", props)
|
|
|
|
def test_delete_nonexistent_property(self):
|
|
with self.assertRaises(ValueError):
|
|
prop.delete_property(self.cur, self.person_id, "nonexistent")
|
|
|
|
# -------------------------
|
|
# Get properties
|
|
# -------------------------
|
|
def test_get_properties_multiple(self):
|
|
prop.set_property(self.cur, self.person_id, "prop1")
|
|
prop.set_property(self.cur, self.person_id, "prop2")
|
|
prop.set_property(self.cur, self.person_id, "prop3")
|
|
|
|
props = prop.get_properties(self.cur, self.person_id)
|
|
self.assertCountEqual(props, ["prop1", "prop2", "prop3"])
|
|
|
|
def test_properties_independent_per_entity(self):
|
|
prop.set_property(self.cur, self.person_id, "p1")
|
|
prop.set_property(self.cur, self.group_id, "p2")
|
|
|
|
person_props = prop.get_properties(self.cur, self.person_id)
|
|
group_props = prop.get_properties(self.cur, self.group_id)
|
|
|
|
self.assertIn("p1", person_props)
|
|
self.assertNotIn("p2", person_props)
|
|
|
|
self.assertIn("p2", group_props)
|
|
self.assertNotIn("p1", group_props)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|
|
|