pki_ca/tests/test_entity.py

98 lines
3.4 KiB
Python

import unittest
import psycopg
from psycopg.rows import dict_row
from ca_core import entity
class TestEntity(unittest.TestCase):
def setUp(self):
self.conn = psycopg.connect("dbname=ca")
self.conn.autocommit = False
self.cur = self.conn.cursor(row_factory=dict_row)
# Clean table
self.cur.execute("TRUNCATE TABLE entity CASCADE;")
# Insert creator
self.creator_id = entity.insert_creator(self.cur, "Alice", "pubkeyA")
# Insert person
self.person_id = entity.enroll_person(self.cur, "Bob", "pubkeyB", self.creator_id)
# Insert 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()
# -------------------------
# Lookup
# -------------------------
def test_get_entity_by_id(self):
ent = entity.get_entity(self.cur, self.person_id)
self.assertEqual(ent["id"], self.person_id)
self.assertFalse(ent["group_p"])
self.assertEqual(ent["creator"], self.creator_id)
self.assertEqual(ent["name"], "Bob")
def test_get_entity_id_by_name(self):
eid = entity.get_entity_id(self.cur, "Bob")
self.assertEqual(eid, self.person_id)
# -------------------------
# Setters / Getters
# -------------------------
def test_set_entity_name_with_creator(self):
entity.set_entity_name(self.cur, self.person_id, "Robert", self.creator_id)
self.assertEqual(entity.get_entity_name(self.cur, self.person_id), "Robert")
def test_set_entity_name_wrong_creator(self):
with self.assertRaises(ValueError):
entity.set_entity_name(self.cur, self.person_id, "Robert", 999999)
def test_set_entity_keys(self):
entity.set_entity_keys(self.cur, self.person_id, "new_pub", self.creator_id)
self.assertEqual(entity.get_entity_public_key(self.cur, self.person_id), "new_pub")
# -------------------------
# Creator self-update
# -------------------------
def test_set_entity_name_creator_self(self):
entity.set_entity_name(self.cur, self.creator_id, "Alicia", self.creator_id)
self.assertEqual(entity.get_entity_name(self.cur, self.creator_id), "Alicia")
# -------------------------
# Aliases
# -------------------------
def test_create_alias(self):
alias_id = entity.create_alias(self.cur, self.person_id)
alias = entity.get_entity(self.cur, alias_id)
self.assertFalse(alias["group_p"])
self.assertEqual(alias["creator"], self.person_id)
self.assertNotEqual(alias["name"], "Bob")
self.assertEqual(alias["public_key"], "pubkeyB")
# -------------------------
# Deletion
# -------------------------
def test_delete_person(self):
entity.delete_person(self.cur, self.person_id, self.creator_id)
with self.assertRaises(ValueError):
entity.get_entity(self.cur, self.person_id)
def test_delete_group(self):
entity.delete_group(self.cur, self.group_id, self.creator_id)
with self.assertRaises(ValueError):
entity.get_entity(self.cur, self.group_id)
def test_delete_creator_with_dependents(self):
with self.assertRaises(ValueError):
entity.delete_creator(self.cur, self.creator_id, self.creator_id)
if __name__ == "__main__":
unittest.main()